blob: b93b561842cdf86a1ed52ba575151980b9d13d5f [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
Manfred Sprauld198cd62014-06-06 14:37:49 -0700588 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700589 *
590 * Returns 0 if the operation was possible.
591 * Returns 1 if the operation is impossible, the caller must sleep.
592 * Negative values are error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 */
Manfred Sprauld198cd62014-06-06 14:37:49 -0700594static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700596 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800598 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700599 struct sembuf *sops;
600 struct sem_undo *un;
601
602 sops = q->sops;
603 nsops = q->nsops;
604 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 for (sop = sops; sop < sops + nsops; sop++) {
607 curr = sma->sem_base + sop->sem_num;
608 sem_op = sop->sem_op;
609 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!sem_op && result)
612 goto would_block;
613
614 result += sem_op;
615 if (result < 0)
616 goto would_block;
617 if (result > SEMVMX)
618 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (sop->sem_flg & SEM_UNDO) {
621 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800622 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
624 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800625 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Petr Mladek78f50092014-01-27 17:07:00 -0800627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 curr->semval = result;
629 }
630
631 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700632 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 while (sop >= sops) {
634 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 sop--;
636 }
Petr Mladek78f50092014-01-27 17:07:00 -0800637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return 0;
639
640out_of_range:
641 result = -ERANGE;
642 goto undo;
643
644would_block:
645 if (sop->sem_flg & IPC_NOWAIT)
646 result = -EAGAIN;
647 else
648 result = 1;
649
650undo:
651 sop--;
652 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800653 sem_op = sop->sem_op;
654 sma->sem_base[sop->sem_num].semval -= sem_op;
655 if (sop->sem_flg & SEM_UNDO)
656 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 sop--;
658 }
659
660 return result;
661}
662
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700663/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
664 * @q: queue entry that must be signaled
665 * @error: Error value for the signal
666 *
667 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800668 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700669static void wake_up_sem_queue_prepare(struct list_head *pt,
670 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800671{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700672 if (list_empty(pt)) {
673 /*
674 * Hold preempt off so that we don't get preempted and have the
675 * wakee busy-wait until we're scheduled back on.
676 */
677 preempt_disable();
678 }
Nick Piggind4212092009-12-15 16:47:30 -0800679 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700680 q->pid = error;
681
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700682 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700683}
684
685/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800686 * wake_up_sem_queue_do - do the actual wake-up
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700687 * @pt: list of tasks to be woken up
688 *
689 * Do the actual wake-up.
690 * The function is called without any locks held, thus the semaphore array
691 * could be destroyed already and the tasks can disappear as soon as the
692 * status is set to the actual return code.
693 */
694static void wake_up_sem_queue_do(struct list_head *pt)
695{
696 struct sem_queue *q, *t;
697 int did_something;
698
699 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700700 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700701 wake_up_process(q->sleeper);
702 /* q can disappear immediately after writing q->status. */
703 smp_wmb();
704 q->status = q->pid;
705 }
706 if (did_something)
707 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800708}
709
Manfred Spraulb97e8202009-12-15 16:47:32 -0800710static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
711{
712 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700713 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800714 sma->complex_count--;
715}
716
Manfred Spraulfd5db422010-05-26 14:43:40 -0700717/** check_restart(sma, q)
718 * @sma: semaphore array
719 * @q: the operation that just completed
720 *
721 * update_queue is O(N^2) when it restarts scanning the whole queue of
722 * waiting operations. Therefore this function checks if the restart is
723 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700724 * modified the array.
725 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700726 */
727static int check_restart(struct sem_array *sma, struct sem_queue *q)
728{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700729 /* pending complex alter operations are too difficult to analyse */
730 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700731 return 1;
732
733 /* we were a sleeping complex operation. Too difficult */
734 if (q->nsops > 1)
735 return 1;
736
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700737 /* It is impossible that someone waits for the new value:
738 * - complex operations always restart.
739 * - wait-for-zero are handled seperately.
740 * - q is a previously sleeping simple operation that
741 * altered the array. It must be a decrement, because
742 * simple increments never sleep.
743 * - If there are older (higher priority) decrements
744 * in the queue, then they have observed the original
745 * semval value and couldn't proceed. The operation
746 * decremented to value - thus they won't proceed either.
747 */
748 return 0;
749}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700750
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700751/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800752 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700753 * @sma: semaphore array.
754 * @semnum: semaphore that was modified.
755 * @pt: list head for the tasks that must be woken up.
756 *
757 * wake_const_ops must be called after a semaphore in a semaphore array
758 * was set to 0. If complex const operations are pending, wake_const_ops must
759 * be called with semnum = -1, as well as with the number of each modified
760 * semaphore.
761 * The tasks that must be woken up are added to @pt. The return code
762 * is stored in q->pid.
763 * The function returns 1 if at least one operation was completed successfully.
764 */
765static int wake_const_ops(struct sem_array *sma, int semnum,
766 struct list_head *pt)
767{
768 struct sem_queue *q;
769 struct list_head *walk;
770 struct list_head *pending_list;
771 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700772
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700773 if (semnum == -1)
774 pending_list = &sma->pending_const;
775 else
776 pending_list = &sma->sem_base[semnum].pending_const;
777
778 walk = pending_list->next;
779 while (walk != pending_list) {
780 int error;
781
782 q = container_of(walk, struct sem_queue, list);
783 walk = walk->next;
784
Manfred Sprauld198cd62014-06-06 14:37:49 -0700785 error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700786
787 if (error <= 0) {
788 /* operation completed, remove from queue & wakeup */
789
790 unlink_queue(sma, q);
791
792 wake_up_sem_queue_prepare(pt, q, error);
793 if (error == 0)
794 semop_completed = 1;
795 }
796 }
797 return semop_completed;
798}
799
800/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800801 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700802 * @sma: semaphore array
803 * @sops: operations that were performed
804 * @nsops: number of operations
805 * @pt: list head of the tasks that must be woken up.
806 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800807 * Checks all required queue for wait-for-zero operations, based
808 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700809 * The function returns 1 if at least one operation was completed successfully.
810 */
811static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
812 int nsops, struct list_head *pt)
813{
814 int i;
815 int semop_completed = 0;
816 int got_zero = 0;
817
818 /* first: the per-semaphore queues, if known */
819 if (sops) {
820 for (i = 0; i < nsops; i++) {
821 int num = sops[i].sem_num;
822
823 if (sma->sem_base[num].semval == 0) {
824 got_zero = 1;
825 semop_completed |= wake_const_ops(sma, num, pt);
826 }
827 }
828 } else {
829 /*
830 * No sops means modified semaphores not known.
831 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700832 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700833 for (i = 0; i < sma->sem_nsems; i++) {
834 if (sma->sem_base[i].semval == 0) {
835 got_zero = 1;
836 semop_completed |= wake_const_ops(sma, i, pt);
837 }
838 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700839 }
840 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700841 * If one of the modified semaphores got 0,
842 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700843 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700844 if (got_zero)
845 semop_completed |= wake_const_ops(sma, -1, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700846
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700847 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700848}
849
Manfred Spraul636c6be2009-12-15 16:47:33 -0800850
851/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800852 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800853 * @sma: semaphore array.
854 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700855 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800856 *
857 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700858 * was modified. If multiple semaphores were modified, update_queue must
859 * be called with semnum = -1, as well as with the number of each modified
860 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700861 * The tasks that must be woken up are added to @pt. The return code
862 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700863 * The function internally checks if const operations can now succeed.
864 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700865 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700867static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800869 struct sem_queue *q;
870 struct list_head *walk;
871 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700872 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800873
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700874 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700875 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700876 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700877 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Nick Piggin9cad2002009-12-15 16:47:29 -0800879again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800880 walk = pending_list->next;
881 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700882 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800883
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700884 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800885 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800886
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800887 /* If we are scanning the single sop, per-semaphore list of
888 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700889 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800890 * that affect only one entry succeed immediately and cannot
891 * be in the per semaphore pending queue, and decrements
892 * cannot be successful if the value is already 0.
893 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700894 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800895 break;
896
Manfred Sprauld198cd62014-06-06 14:37:49 -0700897 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800900 if (error > 0)
901 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700902
Manfred Spraulb97e8202009-12-15 16:47:32 -0800903 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700904
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700905 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700906 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700907 } else {
908 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700909 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700910 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700911 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700912
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700913 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700914 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800915 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700917 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700920/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800921 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700922 * @sma: semaphore array
923 * @sops: operations that modified the array, may be NULL
924 *
925 * sem_otime is replicated to avoid cache line trashing.
926 * This function sets one instance to the current time.
927 */
928static void set_semotime(struct sem_array *sma, struct sembuf *sops)
929{
930 if (sops == NULL) {
931 sma->sem_base[0].sem_otime = get_seconds();
932 } else {
933 sma->sem_base[sops[0].sem_num].sem_otime =
934 get_seconds();
935 }
936}
937
938/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800939 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700940 * @sma: semaphore array
941 * @sops: operations that were performed
942 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700943 * @otime: force setting otime
944 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700945 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700946 * do_smart_update() does the required calls to update_queue and wakeup_zero,
947 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700948 * Note that the function does not do the actual wake-up: the caller is
949 * responsible for calling wake_up_sem_queue_do(@pt).
950 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700951 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700952static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
953 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700954{
955 int i;
956
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700957 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
958
Manfred Spraulf269f402013-07-08 16:01:24 -0700959 if (!list_empty(&sma->pending_alter)) {
960 /* semaphore array uses the global queue - just process it. */
961 otime |= update_queue(sma, -1, pt);
962 } else {
963 if (!sops) {
964 /*
965 * No sops, thus the modified semaphores are not
966 * known. Check all.
967 */
968 for (i = 0; i < sma->sem_nsems; i++)
969 otime |= update_queue(sma, i, pt);
970 } else {
971 /*
972 * Check the semaphores that were increased:
973 * - No complex ops, thus all sleeping ops are
974 * decrease.
975 * - if we decreased the value, then any sleeping
976 * semaphore ops wont be able to run: If the
977 * previous value was too small, then the new
978 * value will be too small, too.
979 */
980 for (i = 0; i < nsops; i++) {
981 if (sops[i].sem_op > 0) {
982 otime |= update_queue(sma,
983 sops[i].sem_num, pt);
984 }
Manfred Spraulab465df2013-05-26 11:08:52 +0200985 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700986 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700987 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700988 if (otime)
989 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700990}
991
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -0700992/*
993 * check_qop: Test how often a queued operation sleeps on the semaphore semnum
994 */
995static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
996 bool count_zero)
997{
998 struct sembuf *sops = q->sops;
999 int nsops = q->nsops;
1000 int i, semcnt;
1001
1002 semcnt = 0;
1003
1004 for (i = 0; i < nsops; i++) {
1005 if (sops[i].sem_num != semnum)
1006 continue;
1007 if (sops[i].sem_flg & IPC_NOWAIT)
1008 continue;
1009 if (count_zero && sops[i].sem_op == 0)
1010 semcnt++;
1011 if (!count_zero && sops[i].sem_op < 0)
1012 semcnt++;
1013 }
1014 return semcnt;
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/* The following counts are associated to each semaphore:
1018 * semncnt number of tasks waiting on semval being nonzero
1019 * semzcnt number of tasks waiting on semval being zero
1020 * This model assumes that a task waits on exactly one semaphore.
1021 * Since semaphore operations are to be performed atomically, tasks actually
1022 * wait on a whole sequence of semaphores simultaneously.
1023 * The counts we return here are a rough approximation, but still
1024 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
1025 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001026static int count_semcnt(struct sem_array *sma, ushort semnum,
1027 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001029 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001030 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001031 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001033 semcnt = 0;
1034 /* First: check the simple operations. They are easy to evaluate */
1035 if (count_zero)
1036 l = &sma->sem_base[semnum].pending_const;
1037 else
1038 l = &sma->sem_base[semnum].pending_alter;
1039
1040 list_for_each_entry(q, l, list) {
1041 /* all task on a per-semaphore list sleep on exactly
1042 * that semaphore
1043 */
1044 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001045 }
1046
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001047 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001048 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001049 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001051 if (count_zero) {
1052 list_for_each_entry(q, &sma->pending_const, list) {
1053 semcnt += check_qop(sma, semnum, q, count_zero);
1054 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001055 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001056 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001059/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1060 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001061 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001063static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001065 struct sem_undo *un, *tu;
1066 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001067 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001068 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001069 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Manfred Spraul380af1b2008-07-25 01:48:06 -07001071 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001072 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001073 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1074 list_del(&un->list_id);
1075 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001077 list_del_rcu(&un->list_proc);
1078 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001079 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001083 INIT_LIST_HEAD(&tasks);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001084 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1085 unlink_queue(sma, q);
1086 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1087 }
1088
1089 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001090 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001091 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001093 for (i = 0; i < sma->sem_nsems; i++) {
1094 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001095 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1096 unlink_queue(sma, q);
1097 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1098 }
1099 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001100 unlink_queue(sma, q);
1101 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1102 }
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001105 /* Remove the semaphore set from the IDR */
1106 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001107 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001108 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001110 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001111 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001112 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1116{
Manfred Spraul239521f2014-01-27 17:07:04 -08001117 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 case IPC_64:
1119 return copy_to_user(buf, in, sizeof(*in));
1120 case IPC_OLD:
1121 {
1122 struct semid_ds out;
1123
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001124 memset(&out, 0, sizeof(out));
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1127
1128 out.sem_otime = in->sem_otime;
1129 out.sem_ctime = in->sem_ctime;
1130 out.sem_nsems = in->sem_nsems;
1131
1132 return copy_to_user(buf, &out, sizeof(out));
1133 }
1134 default:
1135 return -EINVAL;
1136 }
1137}
1138
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001139static time_t get_semotime(struct sem_array *sma)
1140{
1141 int i;
1142 time_t res;
1143
1144 res = sma->sem_base[0].sem_otime;
1145 for (i = 1; i < sma->sem_nsems; i++) {
1146 time_t to = sma->sem_base[i].sem_otime;
1147
1148 if (to > res)
1149 res = to;
1150 }
1151 return res;
1152}
1153
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001154static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001155 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001157 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct sem_array *sma;
1159
Manfred Spraul239521f2014-01-27 17:07:04 -08001160 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 case IPC_INFO:
1162 case SEM_INFO:
1163 {
1164 struct seminfo seminfo;
1165 int max_id;
1166
1167 err = security_sem_semctl(NULL, cmd);
1168 if (err)
1169 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001170
Manfred Spraul239521f2014-01-27 17:07:04 -08001171 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001172 seminfo.semmni = ns->sc_semmni;
1173 seminfo.semmns = ns->sc_semmns;
1174 seminfo.semmsl = ns->sc_semmsl;
1175 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 seminfo.semvmx = SEMVMX;
1177 seminfo.semmnu = SEMMNU;
1178 seminfo.semmap = SEMMAP;
1179 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001180 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001182 seminfo.semusz = sem_ids(ns).in_use;
1183 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 } else {
1185 seminfo.semusz = SEMUSZ;
1186 seminfo.semaem = SEMAEM;
1187 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001188 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001189 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001190 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001192 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001194 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 case SEM_STAT:
1196 {
1197 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001198 int id = 0;
1199
1200 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Linus Torvalds941b0302013-05-04 11:04:29 -07001202 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001203 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001204 sma = sem_obtain_object(ns, semid);
1205 if (IS_ERR(sma)) {
1206 err = PTR_ERR(sma);
1207 goto out_unlock;
1208 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001209 id = sma->sem_perm.id;
1210 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001211 sma = sem_obtain_object_check(ns, semid);
1212 if (IS_ERR(sma)) {
1213 err = PTR_ERR(sma);
1214 goto out_unlock;
1215 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001219 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 goto out_unlock;
1221
1222 err = security_sem_semctl(sma, cmd);
1223 if (err)
1224 goto out_unlock;
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001227 tbuf.sem_otime = get_semotime(sma);
1228 tbuf.sem_ctime = sma->sem_ctime;
1229 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001230 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001231 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EFAULT;
1233 return id;
1234 }
1235 default:
1236 return -EINVAL;
1237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001239 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return err;
1241}
1242
Al Viroe1fd1f42013-03-05 15:04:55 -05001243static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1244 unsigned long arg)
1245{
1246 struct sem_undo *un;
1247 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001248 struct sem *curr;
Al Viroe1fd1f42013-03-05 15:04:55 -05001249 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001250 struct list_head tasks;
1251 int val;
1252#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1253 /* big-endian 64bit */
1254 val = arg >> 32;
1255#else
1256 /* 32bit or little-endian 64bit */
1257 val = arg;
1258#endif
1259
Rik van Riel6062a8d2013-04-30 19:15:44 -07001260 if (val > SEMVMX || val < 0)
1261 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001262
1263 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001264
Rik van Riel6062a8d2013-04-30 19:15:44 -07001265 rcu_read_lock();
1266 sma = sem_obtain_object_check(ns, semid);
1267 if (IS_ERR(sma)) {
1268 rcu_read_unlock();
1269 return PTR_ERR(sma);
1270 }
1271
1272 if (semnum < 0 || semnum >= sma->sem_nsems) {
1273 rcu_read_unlock();
1274 return -EINVAL;
1275 }
1276
1277
1278 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1279 rcu_read_unlock();
1280 return -EACCES;
1281 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001282
1283 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001284 if (err) {
1285 rcu_read_unlock();
1286 return -EACCES;
1287 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001288
Rik van Riel6062a8d2013-04-30 19:15:44 -07001289 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001290
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001291 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001292 sem_unlock(sma, -1);
1293 rcu_read_unlock();
1294 return -EIDRM;
1295 }
1296
Al Viroe1fd1f42013-03-05 15:04:55 -05001297 curr = &sma->sem_base[semnum];
1298
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001299 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001300 list_for_each_entry(un, &sma->list_id, list_id)
1301 un->semadj[semnum] = 0;
1302
1303 curr->semval = val;
1304 curr->sempid = task_tgid_vnr(current);
1305 sma->sem_ctime = get_seconds();
1306 /* maybe some queued-up processes were waiting for this */
1307 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001308 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001309 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001310 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001311 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001312}
1313
Kirill Korotaeve3893532006-10-02 02:18:22 -07001314static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001315 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001318 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001319 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001321 ushort *sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001322 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001324 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001325
1326 rcu_read_lock();
1327 sma = sem_obtain_object_check(ns, semid);
1328 if (IS_ERR(sma)) {
1329 rcu_read_unlock();
1330 return PTR_ERR(sma);
1331 }
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 nsems = sma->sem_nsems;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001336 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1337 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001340 if (err)
1341 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 err = -EACCES;
1344 switch (cmd) {
1345 case GETALL:
1346 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001347 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int i;
1349
Al Viroce857222013-05-03 00:30:49 +01001350 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001351 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001352 err = -EIDRM;
1353 goto out_unlock;
1354 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001355 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001356 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001357 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001358 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001359 }
1360 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001361 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001363 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001364 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return -ENOMEM;
1366 }
1367
Linus Torvalds4091fd92013-05-04 10:13:40 -07001368 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001369 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001370 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001372 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Al Viroce857222013-05-03 00:30:49 +01001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 for (i = 0; i < sma->sem_nsems; i++)
1376 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001377 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001378 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001380 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 err = -EFAULT;
1382 goto out_free;
1383 }
1384 case SETALL:
1385 {
1386 int i;
1387 struct sem_undo *un;
1388
Rik van Riel6062a8d2013-04-30 19:15:44 -07001389 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001390 err = -EIDRM;
1391 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001392 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001393 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Manfred Spraul239521f2014-01-27 17:07:04 -08001395 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001397 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001398 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return -ENOMEM;
1400 }
1401 }
1402
Manfred Spraul239521f2014-01-27 17:07:04 -08001403 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001404 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 err = -EFAULT;
1406 goto out_free;
1407 }
1408
1409 for (i = 0; i < nsems; i++) {
1410 if (sem_io[i] > SEMVMX) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001411 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 err = -ERANGE;
1413 goto out_free;
1414 }
1415 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001416 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001417 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001418 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001420 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422
1423 for (i = 0; i < nsems; i++)
1424 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001425
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001426 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001427 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 for (i = 0; i < nsems; i++)
1429 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 sma->sem_ctime = get_seconds();
1432 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001433 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 err = 0;
1435 goto out_unlock;
1436 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001437 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001440 if (semnum < 0 || semnum >= nsems)
1441 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Rik van Riel6062a8d2013-04-30 19:15:44 -07001443 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001444 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001445 err = -EIDRM;
1446 goto out_unlock;
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 curr = &sma->sem_base[semnum];
1449
1450 switch (cmd) {
1451 case GETVAL:
1452 err = curr->semval;
1453 goto out_unlock;
1454 case GETPID:
1455 err = curr->sempid;
1456 goto out_unlock;
1457 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001458 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 goto out_unlock;
1460 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001461 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001466 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001467out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001468 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001469 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001471 if (sem_io != fast_sem_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 ipc_free(sem_io, sizeof(ushort)*nsems);
1473 return err;
1474}
1475
Pierre Peiffer016d7132008-04-29 01:00:50 -07001476static inline unsigned long
1477copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Manfred Spraul239521f2014-01-27 17:07:04 -08001479 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001481 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 case IPC_OLD:
1485 {
1486 struct semid_ds tbuf_old;
1487
Manfred Spraul239521f2014-01-27 17:07:04 -08001488 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return -EFAULT;
1490
Pierre Peiffer016d7132008-04-29 01:00:50 -07001491 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1492 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1493 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 return 0;
1496 }
1497 default:
1498 return -EINVAL;
1499 }
1500}
1501
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001502/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001503 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001504 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001505 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001506 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001507static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001508 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 struct sem_array *sma;
1511 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001512 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 struct kern_ipc_perm *ipcp;
1514
Manfred Spraul239521f2014-01-27 17:07:04 -08001515 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001516 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001520 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001521 rcu_read_lock();
1522
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001523 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1524 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001525 if (IS_ERR(ipcp)) {
1526 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001527 goto out_unlock1;
1528 }
Steve Grubb073115d2006-04-02 17:07:33 -04001529
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001530 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001533 if (err)
1534 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001536 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001538 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001539 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001540 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001541 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001543 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001544 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1545 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001546 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 break;
1549 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001551 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001554out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001555 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001556out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001557 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001558out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001559 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return err;
1561}
1562
Al Viroe1fd1f42013-03-05 15:04:55 -05001563SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001566 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001567 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 if (semid < 0)
1570 return -EINVAL;
1571
1572 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001573 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Manfred Spraul239521f2014-01-27 17:07:04 -08001575 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 case IPC_INFO:
1577 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001578 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001580 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 case GETALL:
1582 case GETVAL:
1583 case GETPID:
1584 case GETNCNT:
1585 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001587 return semctl_main(ns, semid, semnum, cmd, p);
1588 case SETVAL:
1589 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 case IPC_RMID:
1591 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001592 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 default:
1594 return -EINVAL;
1595 }
1596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/* If the task doesn't already have a undo_list, then allocate one
1599 * here. We guarantee there is only one thread using this undo list,
1600 * and current is THE ONE
1601 *
1602 * If this allocation and assignment succeeds, but later
1603 * portions of this code fail, there is no need to free the sem_undo_list.
1604 * Just let it stay associated with the task, and it'll be freed later
1605 * at exit time.
1606 *
1607 * This can block, so callers must hold no locks.
1608 */
1609static inline int get_undo_list(struct sem_undo_list **undo_listp)
1610{
1611 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 undo_list = current->sysvsem.undo_list;
1614 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001615 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (undo_list == NULL)
1617 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001618 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001620 INIT_LIST_HEAD(&undo_list->list_proc);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 current->sysvsem.undo_list = undo_list;
1623 }
1624 *undo_listp = undo_list;
1625 return 0;
1626}
1627
Nick Pigginbf17bb72009-12-15 16:47:28 -08001628static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001630 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Nick Pigginbf17bb72009-12-15 16:47:28 -08001632 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1633 if (un->semid == semid)
1634 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001636 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Nick Pigginbf17bb72009-12-15 16:47:28 -08001639static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1640{
1641 struct sem_undo *un;
1642
Manfred Spraul239521f2014-01-27 17:07:04 -08001643 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001644
1645 un = __lookup_undo(ulp, semid);
1646 if (un) {
1647 list_del_rcu(&un->list_proc);
1648 list_add_rcu(&un->list_proc, &ulp->list_proc);
1649 }
1650 return un;
1651}
1652
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001653/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001654 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001655 * @ns: namespace
1656 * @semid: semaphore array id
1657 *
1658 * The function looks up (and if not present creates) the undo structure.
1659 * The size of the undo structure depends on the size of the semaphore
1660 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001661 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1662 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001663 */
1664static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 struct sem_array *sma;
1667 struct sem_undo_list *ulp;
1668 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001669 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 error = get_undo_list(&ulp);
1672 if (error)
1673 return ERR_PTR(error);
1674
Manfred Spraul380af1b2008-07-25 01:48:06 -07001675 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001676 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001678 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001679 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 goto out;
1681
1682 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001683 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001684 sma = sem_obtain_object_check(ns, semid);
1685 if (IS_ERR(sma)) {
1686 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001687 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001688 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001691 if (!ipc_rcu_getref(sma)) {
1692 rcu_read_unlock();
1693 un = ERR_PTR(-EIDRM);
1694 goto out;
1695 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001696 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001698 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001699 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (!new) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001701 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return ERR_PTR(-ENOMEM);
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Manfred Spraul380af1b2008-07-25 01:48:06 -07001705 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001706 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001707 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001708 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001709 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001710 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 kfree(new);
1712 un = ERR_PTR(-EIDRM);
1713 goto out;
1714 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001715 spin_lock(&ulp->lock);
1716
1717 /*
1718 * step 4: check for races: did someone else allocate the undo struct?
1719 */
1720 un = lookup_undo(ulp, semid);
1721 if (un) {
1722 kfree(new);
1723 goto success;
1724 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001725 /* step 5: initialize & link new undo structure */
1726 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001727 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001728 new->semid = semid;
1729 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001730 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001731 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001732 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001733 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001734
1735success:
1736 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001737 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738out:
1739 return un;
1740}
1741
Manfred Spraulc61284e2010-07-20 13:24:23 -07001742
1743/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001744 * get_queue_result - retrieve the result code from sem_queue
Manfred Spraulc61284e2010-07-20 13:24:23 -07001745 * @q: Pointer to queue structure
1746 *
1747 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1748 * q->status, then we must loop until the value is replaced with the final
1749 * value: This may happen if a task is woken up by an unrelated event (e.g.
1750 * signal) and in parallel the task is woken up by another task because it got
1751 * the requested semaphores.
1752 *
1753 * The function can be called with or without holding the semaphore spinlock.
1754 */
1755static int get_queue_result(struct sem_queue *q)
1756{
1757 int error;
1758
1759 error = q->status;
1760 while (unlikely(error == IN_WAKEUP)) {
1761 cpu_relax();
1762 error = q->status;
1763 }
1764
1765 return error;
1766}
1767
Heiko Carstensd5460c92009-01-14 14:14:27 +01001768SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1769 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
1771 int error = -EINVAL;
1772 struct sem_array *sma;
1773 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001774 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001776 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct sem_queue queue;
1778 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001779 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001780 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001781
1782 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (nsops < 1 || semid < 0)
1785 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001786 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001788 if (nsops > SEMOPM_FAST) {
1789 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1790 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -ENOMEM;
1792 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001793 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1794 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto out_free;
1796 }
1797 if (timeout) {
1798 struct timespec _timeout;
1799 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1800 error = -EFAULT;
1801 goto out_free;
1802 }
1803 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1804 _timeout.tv_nsec >= 1000000000L) {
1805 error = -EINVAL;
1806 goto out_free;
1807 }
1808 jiffies_left = timespec_to_jiffies(&_timeout);
1809 }
1810 max = 0;
1811 for (sop = sops; sop < sops + nsops; sop++) {
1812 if (sop->sem_num >= max)
1813 max = sop->sem_num;
1814 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001815 undos = 1;
1816 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 alter = 1;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Rik van Riel6062a8d2013-04-30 19:15:44 -07001820 INIT_LIST_HEAD(&tasks);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001823 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001824 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (IS_ERR(un)) {
1826 error = PTR_ERR(un);
1827 goto out_free;
1828 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001829 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001831 rcu_read_lock();
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001834 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001835 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001836 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001837 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001839 }
1840
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001841 error = -EFBIG;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001842 if (max >= sma->sem_nsems)
1843 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001844
1845 error = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001846 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1847 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001848
1849 error = security_sem_semop(sma, sops, nsops, alter);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001850 if (error)
1851 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001852
Manfred Spraul6e224f92013-10-16 13:46:45 -07001853 error = -EIDRM;
1854 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001855 /*
1856 * We eventually might perform the following check in a lockless
1857 * fashion, considering ipc_valid_object() locking constraints.
1858 * If nsops == 1 and there is no contention for sem_perm.lock, then
1859 * only a per-semaphore lock is held and it's OK to proceed with the
1860 * check below. More details on the fine grained locking scheme
1861 * entangled here and why it's RMID race safe on comments at sem_lock()
1862 */
1863 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001864 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001866 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001868 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001869 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001870 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001872 if (un && un->semid == -1)
1873 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001874
Manfred Sprauld198cd62014-06-06 14:37:49 -07001875 queue.sops = sops;
1876 queue.nsops = nsops;
1877 queue.undo = un;
1878 queue.pid = task_tgid_vnr(current);
1879 queue.alter = alter;
1880
1881 error = perform_atomic_semop(sma, &queue);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001882 if (error == 0) {
1883 /* If the operation was successful, then do
1884 * the required updates.
1885 */
1886 if (alter)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001887 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001888 else
1889 set_semotime(sma, sops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001891 if (error <= 0)
1892 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 /* We need to sleep on this operation, so we put the current
1895 * task into the pending queue and go to sleep.
1896 */
Paul McQuade46c0a8c2014-06-06 14:37:37 -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