blob: 6291257ee0498e9976e1d0bf5bcea250ee95f93d [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.
50 * - semncnt and semzcnt are calculated on demand in count_semncnt() and
51 * count_semzcnt()
52 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
57 * dropping all locks. (see wake_up_sem_queue_prepare(),
58 * wake_up_sem_queue_do())
59 * - All work is done by the waker, the woken up task does not have to do
60 * anything - not even acquiring a lock or dropping a refcount.
61 * - A woken up task may not even touch the semaphore array anymore, it may
62 * have been destroyed already by a semctl(RMID).
63 * - The synchronizations between wake-ups due to a timeout/signal and a
64 * wake-up due to a completed semaphore operation is achieved by using an
65 * intermediate state (IN_WAKEUP).
66 * - UNDO values are stored in an array (one per process and per
67 * semaphore array, lazily allocated). For backwards compatibility, multiple
68 * modes for the UNDO variables are supported (per process, per thread)
69 * (see copy_semundo, CLONE_SYSVSEM)
70 * - There are two lists of the pending operations: a per-array list
71 * and per-semaphore list (stored in the array). This allows to achieve FIFO
72 * ordering without always scanning all pending operations.
73 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/slab.h>
77#include <linux/spinlock.h>
78#include <linux/init.h>
79#include <linux/proc_fs.h>
80#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/security.h>
82#include <linux/syscalls.h>
83#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080084#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070085#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070086#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070087#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080088#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <asm/uaccess.h>
91#include "util.h"
92
Manfred Spraule57940d2011-11-02 13:38:54 -070093/* One semaphore structure for each semaphore in the system. */
94struct sem {
95 int semval; /* current value */
96 int sempid; /* pid of last operation */
Rik van Riel6062a8d2013-04-30 19:15:44 -070097 spinlock_t lock; /* spinlock for fine-grained semtimedop */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -070098 struct list_head pending_alter; /* pending single-sop operations */
99 /* that alter the semaphore */
100 struct list_head pending_const; /* pending single-sop operations */
101 /* that do not alter the semaphore*/
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/*
156 * linked list protection:
157 * sem_undo.id_next,
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700158 * sem_array.pending{_alter,_cont},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * sem_array.sem_undo: sem_lock() for read/write
160 * sem_undo.proc_next: only "current" is allowed to read/write that field.
161 *
162 */
163
Kirill Korotaeve3893532006-10-02 02:18:22 -0700164#define sc_semmsl sem_ctls[0]
165#define sc_semmns sem_ctls[1]
166#define sc_semopm sem_ctls[2]
167#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800169void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700170{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700171 ns->sc_semmsl = SEMMSL;
172 ns->sc_semmns = SEMMNS;
173 ns->sc_semopm = SEMOPM;
174 ns->sc_semmni = SEMMNI;
175 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800176 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700177}
178
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800179#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700180void sem_exit_ns(struct ipc_namespace *ns)
181{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800182 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800183 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700184}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187void __init sem_init (void)
188{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800189 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700190 ipc_init_proc_interface("sysvipc/sem",
191 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700192 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Manfred Spraulf269f402013-07-08 16:01:24 -0700195/**
196 * unmerge_queues - unmerge queues, if possible.
197 * @sma: semaphore array
198 *
199 * The function unmerges the wait queues if complex_count is 0.
200 * It must be called prior to dropping the global semaphore array lock.
201 */
202static void unmerge_queues(struct sem_array *sma)
203{
204 struct sem_queue *q, *tq;
205
206 /* complex operations still around? */
207 if (sma->complex_count)
208 return;
209 /*
210 * We will switch back to simple mode.
211 * Move all pending operation back into the per-semaphore
212 * queues.
213 */
214 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
215 struct sem *curr;
216 curr = &sma->sem_base[q->sops[0].sem_num];
217
218 list_add_tail(&q->list, &curr->pending_alter);
219 }
220 INIT_LIST_HEAD(&sma->pending_alter);
221}
222
223/**
224 * merge_queues - Merge single semop queues into global queue
225 * @sma: semaphore array
226 *
227 * This function merges all per-semaphore queues into the global queue.
228 * It is necessary to achieve FIFO ordering for the pending single-sop
229 * operations when a multi-semop operation must sleep.
230 * Only the alter operations must be moved, the const operations can stay.
231 */
232static void merge_queues(struct sem_array *sma)
233{
234 int i;
235 for (i = 0; i < sma->sem_nsems; i++) {
236 struct sem *sem = sma->sem_base + i;
237
238 list_splice_init(&sem->pending_alter, &sma->pending_alter);
239 }
240}
241
Nadia Derbey3e148c72007-10-18 23:40:54 -0700242/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700243 * If the request contains only one semaphore operation, and there are
244 * no complex transactions pending, lock only the semaphore involved.
245 * Otherwise, lock the entire semaphore array, since we either have
246 * multiple semaphores in our own semops, or we need to look at
247 * semaphores from other pending complex operations.
248 *
249 * Carefully guard against sma->complex_count changing between zero
250 * and non-zero while we are spinning for the lock. The value of
251 * sma->complex_count cannot change while we are holding the lock,
252 * so sem_unlock should be fine.
253 *
254 * The global lock path checks that all the local locks have been released,
255 * checking each local lock once. This means that the local lock paths
256 * cannot start their critical sections while the global lock is held.
257 */
258static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
259 int nsops)
260{
261 int locknum;
262 again:
263 if (nsops == 1 && !sma->complex_count) {
264 struct sem *sem = sma->sem_base + sops->sem_num;
265
266 /* Lock just the semaphore we are interested in. */
267 spin_lock(&sem->lock);
268
269 /*
270 * If sma->complex_count was set while we were spinning,
271 * we may need to look at things we did not lock here.
272 */
273 if (unlikely(sma->complex_count)) {
274 spin_unlock(&sem->lock);
275 goto lock_array;
276 }
277
278 /*
279 * Another process is holding the global lock on the
280 * sem_array; we cannot enter our critical section,
281 * but have to wait for the global lock to be released.
282 */
283 if (unlikely(spin_is_locked(&sma->sem_perm.lock))) {
284 spin_unlock(&sem->lock);
285 spin_unlock_wait(&sma->sem_perm.lock);
286 goto again;
287 }
288
289 locknum = sops->sem_num;
290 } else {
291 int i;
292 /*
293 * Lock the semaphore array, and wait for all of the
294 * individual semaphore locks to go away. The code
295 * above ensures no new single-lock holders will enter
296 * their critical section while the array lock is held.
297 */
298 lock_array:
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700299 ipc_lock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700300 for (i = 0; i < sma->sem_nsems; i++) {
301 struct sem *sem = sma->sem_base + i;
302 spin_unlock_wait(&sem->lock);
303 }
304 locknum = -1;
305 }
306 return locknum;
307}
308
309static inline void sem_unlock(struct sem_array *sma, int locknum)
310{
311 if (locknum == -1) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700312 unmerge_queues(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700313 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700314 } else {
315 struct sem *sem = sma->sem_base + locknum;
316 spin_unlock(&sem->lock);
317 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700318}
319
320/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700321 * sem_lock_(check_) routines are called in the paths where the rw_mutex
322 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700323 *
324 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700325 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700326static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
327 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700328{
Rik van Rielc460b662013-04-30 19:15:35 -0700329 struct kern_ipc_perm *ipcp;
330 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700331
Rik van Rielc460b662013-04-30 19:15:35 -0700332 ipcp = ipc_obtain_object(&sem_ids(ns), id);
Linus Torvalds321310c2013-05-04 10:47:57 -0700333 if (IS_ERR(ipcp))
334 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800335
Rik van Riel6062a8d2013-04-30 19:15:44 -0700336 sma = container_of(ipcp, struct sem_array, sem_perm);
337 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700338
339 /* ipc_rmid() may have already freed the ID while sem_lock
340 * was spinning: verify that the structure is still valid
341 */
342 if (!ipcp->deleted)
343 return container_of(ipcp, struct sem_array, sem_perm);
344
Rik van Riel6062a8d2013-04-30 19:15:44 -0700345 sem_unlock(sma, *locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -0700346 return ERR_PTR(-EINVAL);
Nadia Derbey023a5352007-10-18 23:40:51 -0700347}
348
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700349static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
350{
351 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
352
353 if (IS_ERR(ipcp))
354 return ERR_CAST(ipcp);
355
356 return container_of(ipcp, struct sem_array, sem_perm);
357}
358
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700359static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
360 int id)
361{
362 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
363
364 if (IS_ERR(ipcp))
365 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800366
Nadia Derbey03f02c72007-10-18 23:40:51 -0700367 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700368}
369
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700370static inline void sem_lock_and_putref(struct sem_array *sma)
371{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700372 sem_lock(sma, NULL, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700373 ipc_rcu_putref(sma);
374}
375
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700376static inline void sem_putref(struct sem_array *sma)
377{
Linus Torvalds73b29502013-05-03 15:22:00 -0700378 ipc_rcu_putref(sma);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700379}
380
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700381static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
382{
383 ipc_rmid(&sem_ids(ns), &s->sem_perm);
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
387 * Lockless wakeup algorithm:
388 * Without the check/retry algorithm a lockless wakeup is possible:
389 * - queue.status is initialized to -EINTR before blocking.
390 * - wakeup is performed by
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700391 * * unlinking the queue entry from the pending list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * * setting queue.status to IN_WAKEUP
393 * This is the notification for the blocked thread that a
394 * result value is imminent.
395 * * call wake_up_process
396 * * set queue.status to the final value.
397 * - the previously blocked thread checks queue.status:
398 * * if it's IN_WAKEUP, then it must wait until the value changes
399 * * if it's not -EINTR, then the operation was completed by
400 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800401 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 * * otherwise it must acquire the spinlock and check what's up.
403 *
404 * The two-stage algorithm is necessary to protect against the following
405 * races:
406 * - if queue.status is set after wake_up_process, then the woken up idle
407 * thread could race forward and try (and fail) to acquire sma->lock
408 * before update_queue had a chance to set queue.status
409 * - if queue.status is written before wake_up_process and if the
410 * blocked process is woken up by a signal between writing
411 * queue.status and the wake_up_process, then the woken up
412 * process could return from semtimedop and die by calling
413 * sys_exit before wake_up_process is called. Then wake_up_process
414 * will oops, because the task structure is already invalid.
415 * (yes, this happened on s390 with sysv msg).
416 *
417 */
418#define IN_WAKEUP 1
419
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700420/**
421 * newary - Create a new semaphore set
422 * @ns: namespace
423 * @params: ptr to the structure that contains key, semflg and nsems
424 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700425 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700426 */
427
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700428static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 int id;
431 int retval;
432 struct sem_array *sma;
433 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700434 key_t key = params->key;
435 int nsems = params->u.nsems;
436 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800437 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (!nsems)
440 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700441 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENOSPC;
443
444 size = sizeof (*sma) + nsems * sizeof (struct sem);
445 sma = ipc_rcu_alloc(size);
446 if (!sma) {
447 return -ENOMEM;
448 }
449 memset (sma, 0, size);
450
451 sma->sem_perm.mode = (semflg & S_IRWXUGO);
452 sma->sem_perm.key = key;
453
454 sma->sem_perm.security = NULL;
455 retval = security_sem_alloc(sma);
456 if (retval) {
457 ipc_rcu_putref(sma);
458 return retval;
459 }
460
Kirill Korotaeve3893532006-10-02 02:18:22 -0700461 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700462 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 security_sem_free(sma);
464 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700465 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700467 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800470
Rik van Riel6062a8d2013-04-30 19:15:44 -0700471 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700472 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
473 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700474 spin_lock_init(&sma->sem_base[i].lock);
475 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800476
477 sma->complex_count = 0;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700478 INIT_LIST_HEAD(&sma->pending_alter);
479 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700480 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sma->sem_nsems = nsems;
482 sma->sem_ctime = get_seconds();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700483 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700484 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700486 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700489
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700490/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700491 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700492 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700493static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700495 struct sem_array *sma;
496
497 sma = container_of(ipcp, struct sem_array, sem_perm);
498 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700499}
500
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700501/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700502 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700503 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700504static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
505 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700506{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700507 struct sem_array *sma;
508
509 sma = container_of(ipcp, struct sem_array, sem_perm);
510 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700511 return -EINVAL;
512
513 return 0;
514}
515
Heiko Carstensd5460c92009-01-14 14:14:27 +0100516SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700517{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700518 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700519 struct ipc_ops sem_ops;
520 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Kirill Korotaeve3893532006-10-02 02:18:22 -0700522 ns = current->nsproxy->ipc_ns;
523
524 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700526
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700527 sem_ops.getnew = newary;
528 sem_ops.associate = sem_security;
529 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700530
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700531 sem_params.key = key;
532 sem_params.flg = semflg;
533 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700534
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700535 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
539 * Determine whether a sequence of semaphore operations would succeed
540 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
541 */
542
543static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
544 int nsops, struct sem_undo *un, int pid)
545{
546 int result, sem_op;
547 struct sembuf *sop;
548 struct sem * curr;
549
550 for (sop = sops; sop < sops + nsops; sop++) {
551 curr = sma->sem_base + sop->sem_num;
552 sem_op = sop->sem_op;
553 result = curr->semval;
554
555 if (!sem_op && result)
556 goto would_block;
557
558 result += sem_op;
559 if (result < 0)
560 goto would_block;
561 if (result > SEMVMX)
562 goto out_of_range;
563 if (sop->sem_flg & SEM_UNDO) {
564 int undo = un->semadj[sop->sem_num] - sem_op;
565 /*
566 * Exceeding the undo range is an error.
567 */
568 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
569 goto out_of_range;
570 }
571 curr->semval = result;
572 }
573
574 sop--;
575 while (sop >= sops) {
576 sma->sem_base[sop->sem_num].sempid = pid;
577 if (sop->sem_flg & SEM_UNDO)
578 un->semadj[sop->sem_num] -= sop->sem_op;
579 sop--;
580 }
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return 0;
583
584out_of_range:
585 result = -ERANGE;
586 goto undo;
587
588would_block:
589 if (sop->sem_flg & IPC_NOWAIT)
590 result = -EAGAIN;
591 else
592 result = 1;
593
594undo:
595 sop--;
596 while (sop >= sops) {
597 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
598 sop--;
599 }
600
601 return result;
602}
603
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700604/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
605 * @q: queue entry that must be signaled
606 * @error: Error value for the signal
607 *
608 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800609 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700610static void wake_up_sem_queue_prepare(struct list_head *pt,
611 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800612{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700613 if (list_empty(pt)) {
614 /*
615 * Hold preempt off so that we don't get preempted and have the
616 * wakee busy-wait until we're scheduled back on.
617 */
618 preempt_disable();
619 }
Nick Piggind4212092009-12-15 16:47:30 -0800620 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700621 q->pid = error;
622
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700623 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700624}
625
626/**
627 * wake_up_sem_queue_do(pt) - do the actual wake-up
628 * @pt: list of tasks to be woken up
629 *
630 * Do the actual wake-up.
631 * The function is called without any locks held, thus the semaphore array
632 * could be destroyed already and the tasks can disappear as soon as the
633 * status is set to the actual return code.
634 */
635static void wake_up_sem_queue_do(struct list_head *pt)
636{
637 struct sem_queue *q, *t;
638 int did_something;
639
640 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700641 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700642 wake_up_process(q->sleeper);
643 /* q can disappear immediately after writing q->status. */
644 smp_wmb();
645 q->status = q->pid;
646 }
647 if (did_something)
648 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800649}
650
Manfred Spraulb97e8202009-12-15 16:47:32 -0800651static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
652{
653 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700654 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800655 sma->complex_count--;
656}
657
Manfred Spraulfd5db422010-05-26 14:43:40 -0700658/** check_restart(sma, q)
659 * @sma: semaphore array
660 * @q: the operation that just completed
661 *
662 * update_queue is O(N^2) when it restarts scanning the whole queue of
663 * waiting operations. Therefore this function checks if the restart is
664 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700665 * modified the array.
666 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700667 */
668static int check_restart(struct sem_array *sma, struct sem_queue *q)
669{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700670 /* pending complex alter operations are too difficult to analyse */
671 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700672 return 1;
673
674 /* we were a sleeping complex operation. Too difficult */
675 if (q->nsops > 1)
676 return 1;
677
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700678 /* It is impossible that someone waits for the new value:
679 * - complex operations always restart.
680 * - wait-for-zero are handled seperately.
681 * - q is a previously sleeping simple operation that
682 * altered the array. It must be a decrement, because
683 * simple increments never sleep.
684 * - If there are older (higher priority) decrements
685 * in the queue, then they have observed the original
686 * semval value and couldn't proceed. The operation
687 * decremented to value - thus they won't proceed either.
688 */
689 return 0;
690}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700691
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700692/**
693 * wake_const_ops(sma, semnum, pt) - Wake up non-alter tasks
694 * @sma: semaphore array.
695 * @semnum: semaphore that was modified.
696 * @pt: list head for the tasks that must be woken up.
697 *
698 * wake_const_ops must be called after a semaphore in a semaphore array
699 * was set to 0. If complex const operations are pending, wake_const_ops must
700 * be called with semnum = -1, as well as with the number of each modified
701 * semaphore.
702 * The tasks that must be woken up are added to @pt. The return code
703 * is stored in q->pid.
704 * The function returns 1 if at least one operation was completed successfully.
705 */
706static int wake_const_ops(struct sem_array *sma, int semnum,
707 struct list_head *pt)
708{
709 struct sem_queue *q;
710 struct list_head *walk;
711 struct list_head *pending_list;
712 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700713
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700714 if (semnum == -1)
715 pending_list = &sma->pending_const;
716 else
717 pending_list = &sma->sem_base[semnum].pending_const;
718
719 walk = pending_list->next;
720 while (walk != pending_list) {
721 int error;
722
723 q = container_of(walk, struct sem_queue, list);
724 walk = walk->next;
725
726 error = try_atomic_semop(sma, q->sops, q->nsops,
727 q->undo, q->pid);
728
729 if (error <= 0) {
730 /* operation completed, remove from queue & wakeup */
731
732 unlink_queue(sma, q);
733
734 wake_up_sem_queue_prepare(pt, q, error);
735 if (error == 0)
736 semop_completed = 1;
737 }
738 }
739 return semop_completed;
740}
741
742/**
743 * do_smart_wakeup_zero(sma, sops, nsops, pt) - wakeup all wait for zero tasks
744 * @sma: semaphore array
745 * @sops: operations that were performed
746 * @nsops: number of operations
747 * @pt: list head of the tasks that must be woken up.
748 *
749 * do_smart_wakeup_zero() checks all required queue for wait-for-zero
750 * operations, based on the actual changes that were performed on the
751 * semaphore array.
752 * The function returns 1 if at least one operation was completed successfully.
753 */
754static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
755 int nsops, struct list_head *pt)
756{
757 int i;
758 int semop_completed = 0;
759 int got_zero = 0;
760
761 /* first: the per-semaphore queues, if known */
762 if (sops) {
763 for (i = 0; i < nsops; i++) {
764 int num = sops[i].sem_num;
765
766 if (sma->sem_base[num].semval == 0) {
767 got_zero = 1;
768 semop_completed |= wake_const_ops(sma, num, pt);
769 }
770 }
771 } else {
772 /*
773 * No sops means modified semaphores not known.
774 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700775 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700776 for (i = 0; i < sma->sem_nsems; i++) {
777 if (sma->sem_base[i].semval == 0) {
778 got_zero = 1;
779 semop_completed |= wake_const_ops(sma, i, pt);
780 }
781 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700782 }
783 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700784 * If one of the modified semaphores got 0,
785 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700786 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700787 if (got_zero)
788 semop_completed |= wake_const_ops(sma, -1, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700789
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700790 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700791}
792
Manfred Spraul636c6be2009-12-15 16:47:33 -0800793
794/**
795 * update_queue(sma, semnum): Look for tasks that can be completed.
796 * @sma: semaphore array.
797 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700798 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800799 *
800 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700801 * was modified. If multiple semaphores were modified, update_queue must
802 * be called with semnum = -1, as well as with the number of each modified
803 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700804 * The tasks that must be woken up are added to @pt. The return code
805 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700806 * The function internally checks if const operations can now succeed.
807 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700808 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700810static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800812 struct sem_queue *q;
813 struct list_head *walk;
814 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700815 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800816
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700817 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700818 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700819 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700820 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Nick Piggin9cad2002009-12-15 16:47:29 -0800822again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800823 walk = pending_list->next;
824 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700825 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800826
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700827 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800828 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800829
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800830 /* If we are scanning the single sop, per-semaphore list of
831 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700832 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800833 * that affect only one entry succeed immediately and cannot
834 * be in the per semaphore pending queue, and decrements
835 * cannot be successful if the value is already 0.
836 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700837 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800838 break;
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 error = try_atomic_semop(sma, q->sops, q->nsops,
841 q->undo, q->pid);
842
843 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800844 if (error > 0)
845 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700846
Manfred Spraulb97e8202009-12-15 16:47:32 -0800847 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700848
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700849 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700850 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700851 } else {
852 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700853 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700854 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700855 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700856
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700857 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700858 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800859 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700861 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700864/**
865 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700866 * @sma: semaphore array
867 * @sops: operations that were performed
868 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700869 * @otime: force setting otime
870 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700871 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700872 * do_smart_update() does the required calls to update_queue and wakeup_zero,
873 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700874 * Note that the function does not do the actual wake-up: the caller is
875 * responsible for calling wake_up_sem_queue_do(@pt).
876 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700877 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700878static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
879 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700880{
881 int i;
882
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700883 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
884
Manfred Spraulf269f402013-07-08 16:01:24 -0700885 if (!list_empty(&sma->pending_alter)) {
886 /* semaphore array uses the global queue - just process it. */
887 otime |= update_queue(sma, -1, pt);
888 } else {
889 if (!sops) {
890 /*
891 * No sops, thus the modified semaphores are not
892 * known. Check all.
893 */
894 for (i = 0; i < sma->sem_nsems; i++)
895 otime |= update_queue(sma, i, pt);
896 } else {
897 /*
898 * Check the semaphores that were increased:
899 * - No complex ops, thus all sleeping ops are
900 * decrease.
901 * - if we decreased the value, then any sleeping
902 * semaphore ops wont be able to run: If the
903 * previous value was too small, then the new
904 * value will be too small, too.
905 */
906 for (i = 0; i < nsops; i++) {
907 if (sops[i].sem_op > 0) {
908 otime |= update_queue(sma,
909 sops[i].sem_num, pt);
910 }
Manfred Spraulab465df2013-05-26 11:08:52 +0200911 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700912 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700913 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700914 if (otime)
915 sma->sem_otime = get_seconds();
Manfred Spraulfd5db422010-05-26 14:43:40 -0700916}
917
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/* The following counts are associated to each semaphore:
920 * semncnt number of tasks waiting on semval being nonzero
921 * semzcnt number of tasks waiting on semval being zero
922 * This model assumes that a task waits on exactly one semaphore.
923 * Since semaphore operations are to be performed atomically, tasks actually
924 * wait on a whole sequence of semaphores simultaneously.
925 * The counts we return here are a rough approximation, but still
926 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
927 */
928static int count_semncnt (struct sem_array * sma, ushort semnum)
929{
930 int semncnt;
931 struct sem_queue * q;
932
933 semncnt = 0;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700934 list_for_each_entry(q, &sma->sem_base[semnum].pending_alter, list) {
Rik van Rielde2657f2013-05-09 16:59:59 -0400935 struct sembuf * sops = q->sops;
936 BUG_ON(sops->sem_num != semnum);
937 if ((sops->sem_op < 0) && !(sops->sem_flg & IPC_NOWAIT))
938 semncnt++;
939 }
940
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700941 list_for_each_entry(q, &sma->pending_alter, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct sembuf * sops = q->sops;
943 int nsops = q->nsops;
944 int i;
945 for (i = 0; i < nsops; i++)
946 if (sops[i].sem_num == semnum
947 && (sops[i].sem_op < 0)
948 && !(sops[i].sem_flg & IPC_NOWAIT))
949 semncnt++;
950 }
951 return semncnt;
952}
Manfred Spraula1193f82008-07-25 01:48:06 -0700953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954static int count_semzcnt (struct sem_array * sma, ushort semnum)
955{
956 int semzcnt;
957 struct sem_queue * q;
958
959 semzcnt = 0;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700960 list_for_each_entry(q, &sma->sem_base[semnum].pending_const, list) {
Rik van Rielebc2e5e2013-05-09 16:53:28 -0400961 struct sembuf * sops = q->sops;
962 BUG_ON(sops->sem_num != semnum);
963 if ((sops->sem_op == 0) && !(sops->sem_flg & IPC_NOWAIT))
964 semzcnt++;
965 }
966
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700967 list_for_each_entry(q, &sma->pending_const, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct sembuf * sops = q->sops;
969 int nsops = q->nsops;
970 int i;
971 for (i = 0; i < nsops; i++)
972 if (sops[i].sem_num == semnum
973 && (sops[i].sem_op == 0)
974 && !(sops[i].sem_flg & IPC_NOWAIT))
975 semzcnt++;
976 }
977 return semzcnt;
978}
979
Nadia Derbey3e148c72007-10-18 23:40:54 -0700980/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
981 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
982 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800984static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Manfred Spraul380af1b2008-07-25 01:48:06 -0700986 struct sem_undo *un, *tu;
987 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800988 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700989 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700990 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Manfred Spraul380af1b2008-07-25 01:48:06 -0700992 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700993 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700994 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
995 list_del(&un->list_id);
996 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -0700998 list_del_rcu(&un->list_proc);
999 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001000 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001004 INIT_LIST_HEAD(&tasks);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001005 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1006 unlink_queue(sma, q);
1007 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1008 }
1009
1010 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001011 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001012 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001014 for (i = 0; i < sma->sem_nsems; i++) {
1015 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001016 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1017 unlink_queue(sma, q);
1018 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1019 }
1020 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001021 unlink_queue(sma, q);
1022 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1023 }
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001026 /* Remove the semaphore set from the IDR */
1027 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001028 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001029 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001031 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001032 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 security_sem_free(sma);
1034 ipc_rcu_putref(sma);
1035}
1036
1037static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1038{
1039 switch(version) {
1040 case IPC_64:
1041 return copy_to_user(buf, in, sizeof(*in));
1042 case IPC_OLD:
1043 {
1044 struct semid_ds out;
1045
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001046 memset(&out, 0, sizeof(out));
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1049
1050 out.sem_otime = in->sem_otime;
1051 out.sem_ctime = in->sem_ctime;
1052 out.sem_nsems = in->sem_nsems;
1053
1054 return copy_to_user(buf, &out, sizeof(out));
1055 }
1056 default:
1057 return -EINVAL;
1058 }
1059}
1060
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001061static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001062 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001064 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 struct sem_array *sma;
1066
1067 switch(cmd) {
1068 case IPC_INFO:
1069 case SEM_INFO:
1070 {
1071 struct seminfo seminfo;
1072 int max_id;
1073
1074 err = security_sem_semctl(NULL, cmd);
1075 if (err)
1076 return err;
1077
1078 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001079 seminfo.semmni = ns->sc_semmni;
1080 seminfo.semmns = ns->sc_semmns;
1081 seminfo.semmsl = ns->sc_semmsl;
1082 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 seminfo.semvmx = SEMVMX;
1084 seminfo.semmnu = SEMMNU;
1085 seminfo.semmap = SEMMAP;
1086 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -07001087 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001089 seminfo.semusz = sem_ids(ns).in_use;
1090 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 } else {
1092 seminfo.semusz = SEMUSZ;
1093 seminfo.semaem = SEMAEM;
1094 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001095 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -07001096 up_read(&sem_ids(ns).rw_mutex);
Al Viroe1fd1f42013-03-05 15:04:55 -05001097 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EFAULT;
1099 return (max_id < 0) ? 0: max_id;
1100 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001101 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 case SEM_STAT:
1103 {
1104 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001105 int id = 0;
1106
1107 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linus Torvalds941b0302013-05-04 11:04:29 -07001109 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001110 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001111 sma = sem_obtain_object(ns, semid);
1112 if (IS_ERR(sma)) {
1113 err = PTR_ERR(sma);
1114 goto out_unlock;
1115 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001116 id = sma->sem_perm.id;
1117 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001118 sma = sem_obtain_object_check(ns, semid);
1119 if (IS_ERR(sma)) {
1120 err = PTR_ERR(sma);
1121 goto out_unlock;
1122 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001126 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 goto out_unlock;
1128
1129 err = security_sem_semctl(sma, cmd);
1130 if (err)
1131 goto out_unlock;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
1134 tbuf.sem_otime = sma->sem_otime;
1135 tbuf.sem_ctime = sma->sem_ctime;
1136 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001137 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001138 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -EFAULT;
1140 return id;
1141 }
1142 default:
1143 return -EINVAL;
1144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001146 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return err;
1148}
1149
Al Viroe1fd1f42013-03-05 15:04:55 -05001150static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1151 unsigned long arg)
1152{
1153 struct sem_undo *un;
1154 struct sem_array *sma;
1155 struct sem* curr;
1156 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001157 struct list_head tasks;
1158 int val;
1159#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1160 /* big-endian 64bit */
1161 val = arg >> 32;
1162#else
1163 /* 32bit or little-endian 64bit */
1164 val = arg;
1165#endif
1166
Rik van Riel6062a8d2013-04-30 19:15:44 -07001167 if (val > SEMVMX || val < 0)
1168 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001169
1170 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001171
Rik van Riel6062a8d2013-04-30 19:15:44 -07001172 rcu_read_lock();
1173 sma = sem_obtain_object_check(ns, semid);
1174 if (IS_ERR(sma)) {
1175 rcu_read_unlock();
1176 return PTR_ERR(sma);
1177 }
1178
1179 if (semnum < 0 || semnum >= sma->sem_nsems) {
1180 rcu_read_unlock();
1181 return -EINVAL;
1182 }
1183
1184
1185 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1186 rcu_read_unlock();
1187 return -EACCES;
1188 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001189
1190 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001191 if (err) {
1192 rcu_read_unlock();
1193 return -EACCES;
1194 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001195
Rik van Riel6062a8d2013-04-30 19:15:44 -07001196 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001197
1198 curr = &sma->sem_base[semnum];
1199
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001200 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001201 list_for_each_entry(un, &sma->list_id, list_id)
1202 un->semadj[semnum] = 0;
1203
1204 curr->semval = val;
1205 curr->sempid = task_tgid_vnr(current);
1206 sma->sem_ctime = get_seconds();
1207 /* maybe some queued-up processes were waiting for this */
1208 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001209 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001210 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001211 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001212 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001213}
1214
Kirill Korotaeve3893532006-10-02 02:18:22 -07001215static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001216 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct sem_array *sma;
1219 struct sem* curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001220 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ushort fast_sem_io[SEMMSL_FAST];
1222 ushort* sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001223 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001225 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001226
1227 rcu_read_lock();
1228 sma = sem_obtain_object_check(ns, semid);
1229 if (IS_ERR(sma)) {
1230 rcu_read_unlock();
1231 return PTR_ERR(sma);
1232 }
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 nsems = sma->sem_nsems;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001237 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1238 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001241 if (err)
1242 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 err = -EACCES;
1245 switch (cmd) {
1246 case GETALL:
1247 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001248 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int i;
1250
Al Viroce857222013-05-03 00:30:49 +01001251 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if(nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001253 if (!ipc_rcu_getref(sma)) {
1254 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001255 rcu_read_unlock();
Al Viroce857222013-05-03 00:30:49 +01001256 err = -EIDRM;
1257 goto out_free;
1258 }
1259 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001260 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1262 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001263 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -ENOMEM;
1265 }
1266
Linus Torvalds4091fd92013-05-04 10:13:40 -07001267 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001268 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001270 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 err = -EIDRM;
1273 goto out_free;
1274 }
Al Viroce857222013-05-03 00:30:49 +01001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 for (i = 0; i < sma->sem_nsems; i++)
1277 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001278 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 err = 0;
1281 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1282 err = -EFAULT;
1283 goto out_free;
1284 }
1285 case SETALL:
1286 {
1287 int i;
1288 struct sem_undo *un;
1289
Rik van Riel6062a8d2013-04-30 19:15:44 -07001290 if (!ipc_rcu_getref(sma)) {
1291 rcu_read_unlock();
1292 return -EIDRM;
1293 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 if(nsems > SEMMSL_FAST) {
1297 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1298 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001299 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -ENOMEM;
1301 }
1302 }
1303
Al Viroe1fd1f42013-03-05 15:04:55 -05001304 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001305 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 err = -EFAULT;
1307 goto out_free;
1308 }
1309
1310 for (i = 0; i < nsems; i++) {
1311 if (sem_io[i] > SEMVMX) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001312 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 err = -ERANGE;
1314 goto out_free;
1315 }
1316 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001317 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001318 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001320 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 err = -EIDRM;
1323 goto out_free;
1324 }
1325
1326 for (i = 0; i < nsems; i++)
1327 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001328
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001329 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001330 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 for (i = 0; i < nsems; i++)
1332 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 sma->sem_ctime = get_seconds();
1335 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001336 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 err = 0;
1338 goto out_unlock;
1339 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001340 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001343 if (semnum < 0 || semnum >= nsems)
1344 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Rik van Riel6062a8d2013-04-30 19:15:44 -07001346 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 curr = &sma->sem_base[semnum];
1348
1349 switch (cmd) {
1350 case GETVAL:
1351 err = curr->semval;
1352 goto out_unlock;
1353 case GETPID:
1354 err = curr->sempid;
1355 goto out_unlock;
1356 case GETNCNT:
1357 err = count_semncnt(sma,semnum);
1358 goto out_unlock;
1359 case GETZCNT:
1360 err = count_semzcnt(sma,semnum);
1361 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001365 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001366out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001367 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001368 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369out_free:
1370 if(sem_io != fast_sem_io)
1371 ipc_free(sem_io, sizeof(ushort)*nsems);
1372 return err;
1373}
1374
Pierre Peiffer016d7132008-04-29 01:00:50 -07001375static inline unsigned long
1376copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 switch(version) {
1379 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001380 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 case IPC_OLD:
1384 {
1385 struct semid_ds tbuf_old;
1386
1387 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1388 return -EFAULT;
1389
Pierre Peiffer016d7132008-04-29 01:00:50 -07001390 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1391 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1392 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 return 0;
1395 }
1396 default:
1397 return -EINVAL;
1398 }
1399}
1400
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001401/*
1402 * This function handles some semctl commands which require the rw_mutex
1403 * to be held in write mode.
1404 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1405 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001406static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001407 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct sem_array *sma;
1410 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001411 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct kern_ipc_perm *ipcp;
1413
1414 if(cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001415 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001419 down_write(&sem_ids(ns).rw_mutex);
1420 rcu_read_lock();
1421
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001422 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1423 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001424 if (IS_ERR(ipcp)) {
1425 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001426 goto out_unlock1;
1427 }
Steve Grubb073115d2006-04-02 17:07:33 -04001428
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001429 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001432 if (err)
1433 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001435 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001437 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001438 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001439 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001440 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001442 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001443 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1444 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001445 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 break;
1448 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001450 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001453out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001454 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001455out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001456 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001457out_up:
1458 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return err;
1460}
1461
Al Viroe1fd1f42013-03-05 15:04:55 -05001462SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001465 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001466 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 if (semid < 0)
1469 return -EINVAL;
1470
1471 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001472 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 switch(cmd) {
1475 case IPC_INFO:
1476 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001477 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001479 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 case GETALL:
1481 case GETVAL:
1482 case GETPID:
1483 case GETNCNT:
1484 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001486 return semctl_main(ns, semid, semnum, cmd, p);
1487 case SETVAL:
1488 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 case IPC_RMID:
1490 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001491 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 default:
1493 return -EINVAL;
1494 }
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497/* If the task doesn't already have a undo_list, then allocate one
1498 * here. We guarantee there is only one thread using this undo list,
1499 * and current is THE ONE
1500 *
1501 * If this allocation and assignment succeeds, but later
1502 * portions of this code fail, there is no need to free the sem_undo_list.
1503 * Just let it stay associated with the task, and it'll be freed later
1504 * at exit time.
1505 *
1506 * This can block, so callers must hold no locks.
1507 */
1508static inline int get_undo_list(struct sem_undo_list **undo_listp)
1509{
1510 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 undo_list = current->sysvsem.undo_list;
1513 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001514 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (undo_list == NULL)
1516 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001517 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001519 INIT_LIST_HEAD(&undo_list->list_proc);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 current->sysvsem.undo_list = undo_list;
1522 }
1523 *undo_listp = undo_list;
1524 return 0;
1525}
1526
Nick Pigginbf17bb72009-12-15 16:47:28 -08001527static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001529 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Nick Pigginbf17bb72009-12-15 16:47:28 -08001531 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1532 if (un->semid == semid)
1533 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001535 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Nick Pigginbf17bb72009-12-15 16:47:28 -08001538static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1539{
1540 struct sem_undo *un;
1541
1542 assert_spin_locked(&ulp->lock);
1543
1544 un = __lookup_undo(ulp, semid);
1545 if (un) {
1546 list_del_rcu(&un->list_proc);
1547 list_add_rcu(&un->list_proc, &ulp->list_proc);
1548 }
1549 return un;
1550}
1551
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001552/**
1553 * find_alloc_undo - Lookup (and if not present create) undo array
1554 * @ns: namespace
1555 * @semid: semaphore array id
1556 *
1557 * The function looks up (and if not present creates) the undo structure.
1558 * The size of the undo structure depends on the size of the semaphore
1559 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001560 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1561 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001562 */
1563static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 struct sem_array *sma;
1566 struct sem_undo_list *ulp;
1567 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001568 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 error = get_undo_list(&ulp);
1571 if (error)
1572 return ERR_PTR(error);
1573
Manfred Spraul380af1b2008-07-25 01:48:06 -07001574 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001575 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001577 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (likely(un!=NULL))
1579 goto out;
1580
1581 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001582 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001583 sma = sem_obtain_object_check(ns, semid);
1584 if (IS_ERR(sma)) {
1585 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001586 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001587 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001590 if (!ipc_rcu_getref(sma)) {
1591 rcu_read_unlock();
1592 un = ERR_PTR(-EIDRM);
1593 goto out;
1594 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001595 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001597 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001598 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if (!new) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001600 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 return ERR_PTR(-ENOMEM);
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Manfred Spraul380af1b2008-07-25 01:48:06 -07001604 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001605 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001606 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001608 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001609 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 kfree(new);
1611 un = ERR_PTR(-EIDRM);
1612 goto out;
1613 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001614 spin_lock(&ulp->lock);
1615
1616 /*
1617 * step 4: check for races: did someone else allocate the undo struct?
1618 */
1619 un = lookup_undo(ulp, semid);
1620 if (un) {
1621 kfree(new);
1622 goto success;
1623 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001624 /* step 5: initialize & link new undo structure */
1625 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001626 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001627 new->semid = semid;
1628 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001629 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001630 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001631 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001632 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001633
1634success:
1635 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001636 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637out:
1638 return un;
1639}
1640
Manfred Spraulc61284e2010-07-20 13:24:23 -07001641
1642/**
1643 * get_queue_result - Retrieve the result code from sem_queue
1644 * @q: Pointer to queue structure
1645 *
1646 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1647 * q->status, then we must loop until the value is replaced with the final
1648 * value: This may happen if a task is woken up by an unrelated event (e.g.
1649 * signal) and in parallel the task is woken up by another task because it got
1650 * the requested semaphores.
1651 *
1652 * The function can be called with or without holding the semaphore spinlock.
1653 */
1654static int get_queue_result(struct sem_queue *q)
1655{
1656 int error;
1657
1658 error = q->status;
1659 while (unlikely(error == IN_WAKEUP)) {
1660 cpu_relax();
1661 error = q->status;
1662 }
1663
1664 return error;
1665}
1666
1667
Heiko Carstensd5460c92009-01-14 14:14:27 +01001668SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1669 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 int error = -EINVAL;
1672 struct sem_array *sma;
1673 struct sembuf fast_sops[SEMOPM_FAST];
1674 struct sembuf* sops = fast_sops, *sop;
1675 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001676 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 struct sem_queue queue;
1678 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001679 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001680 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001681
1682 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (nsops < 1 || semid < 0)
1685 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001686 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return -E2BIG;
1688 if(nsops > SEMOPM_FAST) {
1689 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1690 if(sops==NULL)
1691 return -ENOMEM;
1692 }
1693 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1694 error=-EFAULT;
1695 goto out_free;
1696 }
1697 if (timeout) {
1698 struct timespec _timeout;
1699 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1700 error = -EFAULT;
1701 goto out_free;
1702 }
1703 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1704 _timeout.tv_nsec >= 1000000000L) {
1705 error = -EINVAL;
1706 goto out_free;
1707 }
1708 jiffies_left = timespec_to_jiffies(&_timeout);
1709 }
1710 max = 0;
1711 for (sop = sops; sop < sops + nsops; sop++) {
1712 if (sop->sem_num >= max)
1713 max = sop->sem_num;
1714 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001715 undos = 1;
1716 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 alter = 1;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Rik van Riel6062a8d2013-04-30 19:15:44 -07001720 INIT_LIST_HEAD(&tasks);
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001723 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001724 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (IS_ERR(un)) {
1726 error = PTR_ERR(un);
1727 goto out_free;
1728 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001729 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001731 rcu_read_lock();
1732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001734 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001735 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001736 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001737 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001739 }
1740
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001741 error = -EFBIG;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001742 if (max >= sma->sem_nsems)
1743 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001744
1745 error = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001746 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1747 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001748
1749 error = security_sem_semop(sma, sops, nsops, alter);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001750 if (error)
1751 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001754 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001756 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001757 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001758 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001760 error = -EIDRM;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001761 locknum = sem_lock(sma, sops, nsops);
1762 if (un && un->semid == -1)
1763 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001764
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001765 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (error <= 0) {
1767 if (alter && error == 0)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001768 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul636c6be2009-12-15 16:47:33 -08001769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto out_unlock_free;
1771 }
1772
1773 /* We need to sleep on this operation, so we put the current
1774 * task into the pending queue and go to sleep.
1775 */
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 queue.sops = sops;
1778 queue.nsops = nsops;
1779 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001780 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Manfred Spraulb97e8202009-12-15 16:47:32 -08001783 if (nsops == 1) {
1784 struct sem *curr;
1785 curr = &sma->sem_base[sops->sem_num];
1786
Manfred Spraulf269f402013-07-08 16:01:24 -07001787 if (alter) {
1788 if (sma->complex_count) {
1789 list_add_tail(&queue.list,
1790 &sma->pending_alter);
1791 } else {
1792
1793 list_add_tail(&queue.list,
1794 &curr->pending_alter);
1795 }
1796 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001797 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001798 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001799 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001800 if (!sma->complex_count)
1801 merge_queues(sma);
1802
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001803 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001804 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001805 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001806 list_add_tail(&queue.list, &sma->pending_const);
1807
Manfred Spraulb97e8202009-12-15 16:47:32 -08001808 sma->complex_count++;
1809 }
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 queue.status = -EINTR;
1812 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001813
1814sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 current->state = TASK_INTERRUPTIBLE;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001816 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001817 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (timeout)
1820 jiffies_left = schedule_timeout(jiffies_left);
1821 else
1822 schedule();
1823
Manfred Spraulc61284e2010-07-20 13:24:23 -07001824 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 if (error != -EINTR) {
1827 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001828 * resources.
1829 * Perform a smp_mb(): User space could assume that semop()
1830 * is a memory barrier: Without the mb(), the cpu could
1831 * speculatively read in user space stale data that was
1832 * overwritten by the previous owner of the semaphore.
1833 */
1834 smp_mb();
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto out_free;
1837 }
1838
Linus Torvalds321310c2013-05-04 10:47:57 -07001839 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001840 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001841
1842 /*
1843 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1844 */
1845 error = get_queue_result(&queue);
1846
1847 /*
1848 * Array removed? If yes, leave without sem_unlock().
1849 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001850 if (IS_ERR(sma)) {
Linus Torvalds321310c2013-05-04 10:47:57 -07001851 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out_free;
1853 }
1854
Manfred Spraulc61284e2010-07-20 13:24:23 -07001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001857 * If queue.status != -EINTR we are woken up by another process.
1858 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 */
Manfred Spraulc61284e2010-07-20 13:24:23 -07001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (error != -EINTR) {
1862 goto out_unlock_free;
1863 }
1864
1865 /*
1866 * If an interrupt occurred we have to clean up the queue
1867 */
1868 if (timeout && jiffies_left == 0)
1869 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001870
1871 /*
1872 * If the wakeup was spurious, just retry
1873 */
1874 if (error == -EINTR && !signal_pending(current))
1875 goto sleep_again;
1876
Manfred Spraulb97e8202009-12-15 16:47:32 -08001877 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001880 sem_unlock(sma, locknum);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001881out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001882 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001883 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884out_free:
1885 if(sops != fast_sops)
1886 kfree(sops);
1887 return error;
1888}
1889
Heiko Carstensd5460c92009-01-14 14:14:27 +01001890SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1891 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 return sys_semtimedop(semid, tsops, nsops, NULL);
1894}
1895
1896/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1897 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 */
1899
1900int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1901{
1902 struct sem_undo_list *undo_list;
1903 int error;
1904
1905 if (clone_flags & CLONE_SYSVSEM) {
1906 error = get_undo_list(&undo_list);
1907 if (error)
1908 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 atomic_inc(&undo_list->refcnt);
1910 tsk->sysvsem.undo_list = undo_list;
1911 } else
1912 tsk->sysvsem.undo_list = NULL;
1913
1914 return 0;
1915}
1916
1917/*
1918 * add semadj values to semaphores, free undo structures.
1919 * undo structures are not freed when semaphore arrays are destroyed
1920 * so some of them may be out of date.
1921 * IMPLEMENTATION NOTE: There is some confusion over whether the
1922 * set of adjustments that needs to be done should be done in an atomic
1923 * manner or not. That is, if we are attempting to decrement the semval
1924 * should we queue up and wait until we can do so legally?
1925 * The original implementation attempted to do this (queue and wait).
1926 * The current implementation does not do so. The POSIX standard
1927 * and SVID should be consulted to determine what behavior is mandated.
1928 */
1929void exit_sem(struct task_struct *tsk)
1930{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001931 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001933 ulp = tsk->sysvsem.undo_list;
1934 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07001936 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001938 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 return;
1940
Manfred Spraul380af1b2008-07-25 01:48:06 -07001941 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001943 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001944 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001945 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Manfred Spraul380af1b2008-07-25 01:48:06 -07001947 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02001948 un = list_entry_rcu(ulp->list_proc.next,
1949 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001950 if (&un->list_proc == &ulp->list_proc)
1951 semid = -1;
1952 else
1953 semid = un->semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001954
Rik van Riel6062a8d2013-04-30 19:15:44 -07001955 if (semid == -1) {
1956 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001957 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001958 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001959
Rik van Riel6062a8d2013-04-30 19:15:44 -07001960 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001961 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001962 if (IS_ERR(sma)) {
1963 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001964 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Rik van Riel6062a8d2013-04-30 19:15:44 -07001967 sem_lock(sma, NULL, -1);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001968 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001969 if (un == NULL) {
1970 /* exit_sem raced with IPC_RMID+semget() that created
1971 * exactly the same semid. Nothing to do.
1972 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001973 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001974 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001975 continue;
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Manfred Spraul380af1b2008-07-25 01:48:06 -07001978 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001979 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001980 list_del(&un->list_id);
1981
Manfred Spraul380af1b2008-07-25 01:48:06 -07001982 spin_lock(&ulp->lock);
1983 list_del_rcu(&un->list_proc);
1984 spin_unlock(&ulp->lock);
1985
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001986 /* perform adjustments registered in un */
1987 for (i = 0; i < sma->sem_nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001988 struct sem * semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001989 if (un->semadj[i]) {
1990 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /*
1992 * Range checks of the new semaphore value,
1993 * not defined by sus:
1994 * - Some unices ignore the undo entirely
1995 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1996 * - some cap the value (e.g. FreeBSD caps
1997 * at 0, but doesn't enforce SEMVMX)
1998 *
1999 * Linux caps the semaphore value, both at 0
2000 * and at SEMVMX.
2001 *
2002 * Manfred <manfred@colorfullife.com>
2003 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002004 if (semaphore->semval < 0)
2005 semaphore->semval = 0;
2006 if (semaphore->semval > SEMVMX)
2007 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002008 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002012 INIT_LIST_HEAD(&tasks);
2013 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002014 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002015 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002016 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002017
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002018 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002020 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002024static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002026 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002027 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Mike Waychison19b49462005-09-06 15:17:10 -07002029 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08002030 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07002031 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07002032 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07002033 sma->sem_perm.mode,
2034 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002035 from_kuid_munged(user_ns, sma->sem_perm.uid),
2036 from_kgid_munged(user_ns, sma->sem_perm.gid),
2037 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2038 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Mike Waychison19b49462005-09-06 15:17:10 -07002039 sma->sem_otime,
2040 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042#endif