blob: e78ee3186d1fba04f90e5d81179557f58d674e4b [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 Spraule57940d2011-11-02 13:38:54 -070098 struct list_head sem_pending; /* pending single-sop operations */
99};
100
101/* One queue for each sleeping process in the system. */
102struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700103 struct list_head list; /* queue of pending operations */
104 struct task_struct *sleeper; /* this process */
105 struct sem_undo *undo; /* undo structure */
106 int pid; /* process id of requesting process */
107 int status; /* completion status of operation */
108 struct sembuf *sops; /* array of pending operations */
109 int nsops; /* number of operations */
110 int alter; /* does *sops alter the array? */
111};
112
113/* Each task has a list of undo requests. They are executed automatically
114 * when the process exits.
115 */
116struct sem_undo {
117 struct list_head list_proc; /* per-process list: *
118 * all undos from one process
119 * rcu protected */
120 struct rcu_head rcu; /* rcu struct for sem_undo */
121 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
122 struct list_head list_id; /* per semaphore array list:
123 * all undos for one array */
124 int semid; /* semaphore set identifier */
125 short *semadj; /* array of adjustments */
126 /* one per semaphore */
127};
128
129/* sem_undo_list controls shared access to the list of sem_undo structures
130 * that may be shared among all a CLONE_SYSVSEM task group.
131 */
132struct sem_undo_list {
133 atomic_t refcnt;
134 spinlock_t lock;
135 struct list_head list_proc;
136};
137
138
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800139#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Nadia Derbey1b531f22007-10-18 23:40:55 -0700141#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700143static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800144static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700146static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147#endif
148
149#define SEMMSL_FAST 256 /* 512 bytes on stack */
150#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
151
152/*
153 * linked list protection:
154 * sem_undo.id_next,
155 * sem_array.sem_pending{,last},
156 * sem_array.sem_undo: sem_lock() for read/write
157 * sem_undo.proc_next: only "current" is allowed to read/write that field.
158 *
159 */
160
Kirill Korotaeve3893532006-10-02 02:18:22 -0700161#define sc_semmsl sem_ctls[0]
162#define sc_semmns sem_ctls[1]
163#define sc_semopm sem_ctls[2]
164#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800166void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700167{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700168 ns->sc_semmsl = SEMMSL;
169 ns->sc_semmns = SEMMNS;
170 ns->sc_semopm = SEMOPM;
171 ns->sc_semmni = SEMMNI;
172 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800173 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700174}
175
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800176#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700177void sem_exit_ns(struct ipc_namespace *ns)
178{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800179 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800180 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700181}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800182#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184void __init sem_init (void)
185{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800186 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700187 ipc_init_proc_interface("sysvipc/sem",
188 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700189 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Nadia Derbey3e148c72007-10-18 23:40:54 -0700192/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700193 * If the request contains only one semaphore operation, and there are
194 * no complex transactions pending, lock only the semaphore involved.
195 * Otherwise, lock the entire semaphore array, since we either have
196 * multiple semaphores in our own semops, or we need to look at
197 * semaphores from other pending complex operations.
198 *
199 * Carefully guard against sma->complex_count changing between zero
200 * and non-zero while we are spinning for the lock. The value of
201 * sma->complex_count cannot change while we are holding the lock,
202 * so sem_unlock should be fine.
203 *
204 * The global lock path checks that all the local locks have been released,
205 * checking each local lock once. This means that the local lock paths
206 * cannot start their critical sections while the global lock is held.
207 */
208static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
209 int nsops)
210{
211 int locknum;
212 again:
213 if (nsops == 1 && !sma->complex_count) {
214 struct sem *sem = sma->sem_base + sops->sem_num;
215
216 /* Lock just the semaphore we are interested in. */
217 spin_lock(&sem->lock);
218
219 /*
220 * If sma->complex_count was set while we were spinning,
221 * we may need to look at things we did not lock here.
222 */
223 if (unlikely(sma->complex_count)) {
224 spin_unlock(&sem->lock);
225 goto lock_array;
226 }
227
228 /*
229 * Another process is holding the global lock on the
230 * sem_array; we cannot enter our critical section,
231 * but have to wait for the global lock to be released.
232 */
233 if (unlikely(spin_is_locked(&sma->sem_perm.lock))) {
234 spin_unlock(&sem->lock);
235 spin_unlock_wait(&sma->sem_perm.lock);
236 goto again;
237 }
238
239 locknum = sops->sem_num;
240 } else {
241 int i;
242 /*
243 * Lock the semaphore array, and wait for all of the
244 * individual semaphore locks to go away. The code
245 * above ensures no new single-lock holders will enter
246 * their critical section while the array lock is held.
247 */
248 lock_array:
249 spin_lock(&sma->sem_perm.lock);
250 for (i = 0; i < sma->sem_nsems; i++) {
251 struct sem *sem = sma->sem_base + i;
252 spin_unlock_wait(&sem->lock);
253 }
254 locknum = -1;
255 }
256 return locknum;
257}
258
259static inline void sem_unlock(struct sem_array *sma, int locknum)
260{
261 if (locknum == -1) {
262 spin_unlock(&sma->sem_perm.lock);
263 } else {
264 struct sem *sem = sma->sem_base + locknum;
265 spin_unlock(&sem->lock);
266 }
267 rcu_read_unlock();
268}
269
270/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700271 * sem_lock_(check_) routines are called in the paths where the rw_mutex
272 * is not held.
273 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700274static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
275 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700276{
Rik van Rielc460b662013-04-30 19:15:35 -0700277 struct kern_ipc_perm *ipcp;
278 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700279
Rik van Rielc460b662013-04-30 19:15:35 -0700280 rcu_read_lock();
281 ipcp = ipc_obtain_object(&sem_ids(ns), id);
282 if (IS_ERR(ipcp)) {
283 sma = ERR_CAST(ipcp);
284 goto err;
285 }
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800286
Rik van Riel6062a8d2013-04-30 19:15:44 -0700287 sma = container_of(ipcp, struct sem_array, sem_perm);
288 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700289
290 /* ipc_rmid() may have already freed the ID while sem_lock
291 * was spinning: verify that the structure is still valid
292 */
293 if (!ipcp->deleted)
294 return container_of(ipcp, struct sem_array, sem_perm);
295
Rik van Riel6062a8d2013-04-30 19:15:44 -0700296 sem_unlock(sma, *locknum);
Rik van Rielc460b662013-04-30 19:15:35 -0700297 sma = ERR_PTR(-EINVAL);
298err:
299 rcu_read_unlock();
300 return sma;
Nadia Derbey023a5352007-10-18 23:40:51 -0700301}
302
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700303static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
304{
305 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
306
307 if (IS_ERR(ipcp))
308 return ERR_CAST(ipcp);
309
310 return container_of(ipcp, struct sem_array, sem_perm);
311}
312
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700313static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
314 int id)
315{
316 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
317
318 if (IS_ERR(ipcp))
319 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800320
Nadia Derbey03f02c72007-10-18 23:40:51 -0700321 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700322}
323
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700324static inline void sem_lock_and_putref(struct sem_array *sma)
325{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700326 rcu_read_lock();
327 sem_lock(sma, NULL, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700328 ipc_rcu_putref(sma);
329}
330
331static inline void sem_getref_and_unlock(struct sem_array *sma)
332{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700333 WARN_ON_ONCE(!ipc_rcu_getref(sma));
334 sem_unlock(sma, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700335}
336
337static inline void sem_putref(struct sem_array *sma)
338{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700339 sem_lock_and_putref(sma);
340 sem_unlock(sma, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700341}
342
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700343/*
344 * Call inside the rcu read section.
345 */
346static inline void sem_getref(struct sem_array *sma)
347{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700348 sem_lock(sma, NULL, -1);
349 WARN_ON_ONCE(!ipc_rcu_getref(sma));
350 sem_unlock(sma, -1);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700351}
352
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700353static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
354{
355 ipc_rmid(&sem_ids(ns), &s->sem_perm);
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/*
359 * Lockless wakeup algorithm:
360 * Without the check/retry algorithm a lockless wakeup is possible:
361 * - queue.status is initialized to -EINTR before blocking.
362 * - wakeup is performed by
363 * * unlinking the queue entry from sma->sem_pending
364 * * setting queue.status to IN_WAKEUP
365 * This is the notification for the blocked thread that a
366 * result value is imminent.
367 * * call wake_up_process
368 * * set queue.status to the final value.
369 * - the previously blocked thread checks queue.status:
370 * * if it's IN_WAKEUP, then it must wait until the value changes
371 * * if it's not -EINTR, then the operation was completed by
372 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800373 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * * otherwise it must acquire the spinlock and check what's up.
375 *
376 * The two-stage algorithm is necessary to protect against the following
377 * races:
378 * - if queue.status is set after wake_up_process, then the woken up idle
379 * thread could race forward and try (and fail) to acquire sma->lock
380 * before update_queue had a chance to set queue.status
381 * - if queue.status is written before wake_up_process and if the
382 * blocked process is woken up by a signal between writing
383 * queue.status and the wake_up_process, then the woken up
384 * process could return from semtimedop and die by calling
385 * sys_exit before wake_up_process is called. Then wake_up_process
386 * will oops, because the task structure is already invalid.
387 * (yes, this happened on s390 with sysv msg).
388 *
389 */
390#define IN_WAKEUP 1
391
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700392/**
393 * newary - Create a new semaphore set
394 * @ns: namespace
395 * @params: ptr to the structure that contains key, semflg and nsems
396 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700397 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700398 */
399
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700400static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int id;
403 int retval;
404 struct sem_array *sma;
405 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700406 key_t key = params->key;
407 int nsems = params->u.nsems;
408 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800409 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (!nsems)
412 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700413 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return -ENOSPC;
415
416 size = sizeof (*sma) + nsems * sizeof (struct sem);
417 sma = ipc_rcu_alloc(size);
418 if (!sma) {
419 return -ENOMEM;
420 }
421 memset (sma, 0, size);
422
423 sma->sem_perm.mode = (semflg & S_IRWXUGO);
424 sma->sem_perm.key = key;
425
426 sma->sem_perm.security = NULL;
427 retval = security_sem_alloc(sma);
428 if (retval) {
429 ipc_rcu_putref(sma);
430 return retval;
431 }
432
Kirill Korotaeve3893532006-10-02 02:18:22 -0700433 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700434 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 security_sem_free(sma);
436 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700437 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700439 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800442
Rik van Riel6062a8d2013-04-30 19:15:44 -0700443 for (i = 0; i < nsems; i++) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800444 INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700445 spin_lock_init(&sma->sem_base[i].lock);
446 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800447
448 sma->complex_count = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700449 INIT_LIST_HEAD(&sma->sem_pending);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700450 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sma->sem_nsems = nsems;
452 sma->sem_ctime = get_seconds();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700453 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700455 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700458
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700459/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700460 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700461 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700462static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700464 struct sem_array *sma;
465
466 sma = container_of(ipcp, struct sem_array, sem_perm);
467 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700468}
469
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700470/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700471 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700472 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700473static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
474 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700475{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700476 struct sem_array *sma;
477
478 sma = container_of(ipcp, struct sem_array, sem_perm);
479 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700480 return -EINVAL;
481
482 return 0;
483}
484
Heiko Carstensd5460c92009-01-14 14:14:27 +0100485SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700486{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700487 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700488 struct ipc_ops sem_ops;
489 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Kirill Korotaeve3893532006-10-02 02:18:22 -0700491 ns = current->nsproxy->ipc_ns;
492
493 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700495
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700496 sem_ops.getnew = newary;
497 sem_ops.associate = sem_security;
498 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700499
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700500 sem_params.key = key;
501 sem_params.flg = semflg;
502 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700503
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700504 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/*
508 * Determine whether a sequence of semaphore operations would succeed
509 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
510 */
511
512static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
513 int nsops, struct sem_undo *un, int pid)
514{
515 int result, sem_op;
516 struct sembuf *sop;
517 struct sem * curr;
518
519 for (sop = sops; sop < sops + nsops; sop++) {
520 curr = sma->sem_base + sop->sem_num;
521 sem_op = sop->sem_op;
522 result = curr->semval;
523
524 if (!sem_op && result)
525 goto would_block;
526
527 result += sem_op;
528 if (result < 0)
529 goto would_block;
530 if (result > SEMVMX)
531 goto out_of_range;
532 if (sop->sem_flg & SEM_UNDO) {
533 int undo = un->semadj[sop->sem_num] - sem_op;
534 /*
535 * Exceeding the undo range is an error.
536 */
537 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
538 goto out_of_range;
539 }
540 curr->semval = result;
541 }
542
543 sop--;
544 while (sop >= sops) {
545 sma->sem_base[sop->sem_num].sempid = pid;
546 if (sop->sem_flg & SEM_UNDO)
547 un->semadj[sop->sem_num] -= sop->sem_op;
548 sop--;
549 }
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 0;
552
553out_of_range:
554 result = -ERANGE;
555 goto undo;
556
557would_block:
558 if (sop->sem_flg & IPC_NOWAIT)
559 result = -EAGAIN;
560 else
561 result = 1;
562
563undo:
564 sop--;
565 while (sop >= sops) {
566 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
567 sop--;
568 }
569
570 return result;
571}
572
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700573/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
574 * @q: queue entry that must be signaled
575 * @error: Error value for the signal
576 *
577 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800578 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700579static void wake_up_sem_queue_prepare(struct list_head *pt,
580 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800581{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700582 if (list_empty(pt)) {
583 /*
584 * Hold preempt off so that we don't get preempted and have the
585 * wakee busy-wait until we're scheduled back on.
586 */
587 preempt_disable();
588 }
Nick Piggind4212092009-12-15 16:47:30 -0800589 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700590 q->pid = error;
591
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700592 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700593}
594
595/**
596 * wake_up_sem_queue_do(pt) - do the actual wake-up
597 * @pt: list of tasks to be woken up
598 *
599 * Do the actual wake-up.
600 * The function is called without any locks held, thus the semaphore array
601 * could be destroyed already and the tasks can disappear as soon as the
602 * status is set to the actual return code.
603 */
604static void wake_up_sem_queue_do(struct list_head *pt)
605{
606 struct sem_queue *q, *t;
607 int did_something;
608
609 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700610 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700611 wake_up_process(q->sleeper);
612 /* q can disappear immediately after writing q->status. */
613 smp_wmb();
614 q->status = q->pid;
615 }
616 if (did_something)
617 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800618}
619
Manfred Spraulb97e8202009-12-15 16:47:32 -0800620static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
621{
622 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700623 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800624 sma->complex_count--;
625}
626
Manfred Spraulfd5db422010-05-26 14:43:40 -0700627/** check_restart(sma, q)
628 * @sma: semaphore array
629 * @q: the operation that just completed
630 *
631 * update_queue is O(N^2) when it restarts scanning the whole queue of
632 * waiting operations. Therefore this function checks if the restart is
633 * really necessary. It is called after a previously waiting operation
634 * was completed.
635 */
636static int check_restart(struct sem_array *sma, struct sem_queue *q)
637{
638 struct sem *curr;
639 struct sem_queue *h;
640
641 /* if the operation didn't modify the array, then no restart */
642 if (q->alter == 0)
643 return 0;
644
645 /* pending complex operations are too difficult to analyse */
646 if (sma->complex_count)
647 return 1;
648
649 /* we were a sleeping complex operation. Too difficult */
650 if (q->nsops > 1)
651 return 1;
652
653 curr = sma->sem_base + q->sops[0].sem_num;
654
655 /* No-one waits on this queue */
656 if (list_empty(&curr->sem_pending))
657 return 0;
658
659 /* the new semaphore value */
660 if (curr->semval) {
661 /* It is impossible that someone waits for the new value:
662 * - q is a previously sleeping simple operation that
663 * altered the array. It must be a decrement, because
664 * simple increments never sleep.
665 * - The value is not 0, thus wait-for-zero won't proceed.
666 * - If there are older (higher priority) decrements
667 * in the queue, then they have observed the original
668 * semval value and couldn't proceed. The operation
669 * decremented to value - thus they won't proceed either.
670 */
671 BUG_ON(q->sops[0].sem_op >= 0);
672 return 0;
673 }
674 /*
675 * semval is 0. Check if there are wait-for-zero semops.
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700676 * They must be the first entries in the per-semaphore queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700677 */
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700678 h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700679 BUG_ON(h->nsops != 1);
680 BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
681
682 /* Yes, there is a wait-for-zero semop. Restart */
683 if (h->sops[0].sem_op == 0)
684 return 1;
685
686 /* Again - no-one is waiting for the new value. */
687 return 0;
688}
689
Manfred Spraul636c6be2009-12-15 16:47:33 -0800690
691/**
692 * update_queue(sma, semnum): Look for tasks that can be completed.
693 * @sma: semaphore array.
694 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700695 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800696 *
697 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700698 * was modified. If multiple semaphores were modified, update_queue must
699 * be called with semnum = -1, as well as with the number of each modified
700 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700701 * The tasks that must be woken up are added to @pt. The return code
702 * is stored in q->pid.
703 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700705static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800707 struct sem_queue *q;
708 struct list_head *walk;
709 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700710 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800711
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700712 if (semnum == -1)
Manfred Spraul636c6be2009-12-15 16:47:33 -0800713 pending_list = &sma->sem_pending;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700714 else
Manfred Spraul636c6be2009-12-15 16:47:33 -0800715 pending_list = &sma->sem_base[semnum].sem_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Nick Piggin9cad2002009-12-15 16:47:29 -0800717again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800718 walk = pending_list->next;
719 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700720 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800721
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700722 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800723 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800724
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800725 /* If we are scanning the single sop, per-semaphore list of
726 * one semaphore and that semaphore is 0, then it is not
727 * necessary to scan the "alter" entries: simple increments
728 * that affect only one entry succeed immediately and cannot
729 * be in the per semaphore pending queue, and decrements
730 * cannot be successful if the value is already 0.
731 */
732 if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
733 q->alter)
734 break;
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 error = try_atomic_semop(sma, q->sops, q->nsops,
737 q->undo, q->pid);
738
739 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800740 if (error > 0)
741 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700742
Manfred Spraulb97e8202009-12-15 16:47:32 -0800743 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700744
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700745 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700746 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700747 } else {
748 semop_completed = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700749 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700750 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700751
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700752 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700753 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800754 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700756 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700759/**
760 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700761 * @sma: semaphore array
762 * @sops: operations that were performed
763 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700764 * @otime: force setting otime
765 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700766 *
767 * do_smart_update() does the required called to update_queue, based on the
768 * actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700769 * Note that the function does not do the actual wake-up: the caller is
770 * responsible for calling wake_up_sem_queue_do(@pt).
771 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700772 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700773static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
774 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700775{
776 int i;
777
778 if (sma->complex_count || sops == NULL) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700779 if (update_queue(sma, -1, pt))
780 otime = 1;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700781 }
782
783 if (!sops) {
784 /* No semops; something special is going on. */
785 for (i = 0; i < sma->sem_nsems; i++) {
786 if (update_queue(sma, i, pt))
787 otime = 1;
788 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700789 goto done;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700790 }
791
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700792 /* Check the semaphores that were modified. */
Manfred Spraulfd5db422010-05-26 14:43:40 -0700793 for (i = 0; i < nsops; i++) {
794 if (sops[i].sem_op > 0 ||
795 (sops[i].sem_op < 0 &&
796 sma->sem_base[sops[i].sem_num].semval == 0))
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700797 if (update_queue(sma, sops[i].sem_num, pt))
798 otime = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700799 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700800done:
801 if (otime)
802 sma->sem_otime = get_seconds();
Manfred Spraulfd5db422010-05-26 14:43:40 -0700803}
804
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806/* The following counts are associated to each semaphore:
807 * semncnt number of tasks waiting on semval being nonzero
808 * semzcnt number of tasks waiting on semval being zero
809 * This model assumes that a task waits on exactly one semaphore.
810 * Since semaphore operations are to be performed atomically, tasks actually
811 * wait on a whole sequence of semaphores simultaneously.
812 * The counts we return here are a rough approximation, but still
813 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
814 */
815static int count_semncnt (struct sem_array * sma, ushort semnum)
816{
817 int semncnt;
818 struct sem_queue * q;
819
820 semncnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700821 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct sembuf * sops = q->sops;
823 int nsops = q->nsops;
824 int i;
825 for (i = 0; i < nsops; i++)
826 if (sops[i].sem_num == semnum
827 && (sops[i].sem_op < 0)
828 && !(sops[i].sem_flg & IPC_NOWAIT))
829 semncnt++;
830 }
831 return semncnt;
832}
Manfred Spraula1193f82008-07-25 01:48:06 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int count_semzcnt (struct sem_array * sma, ushort semnum)
835{
836 int semzcnt;
837 struct sem_queue * q;
838
839 semzcnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700840 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct sembuf * sops = q->sops;
842 int nsops = q->nsops;
843 int i;
844 for (i = 0; i < nsops; i++)
845 if (sops[i].sem_num == semnum
846 && (sops[i].sem_op == 0)
847 && !(sops[i].sem_flg & IPC_NOWAIT))
848 semzcnt++;
849 }
850 return semzcnt;
851}
852
Nadia Derbey3e148c72007-10-18 23:40:54 -0700853/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
854 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
855 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800857static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Manfred Spraul380af1b2008-07-25 01:48:06 -0700859 struct sem_undo *un, *tu;
860 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800861 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700862 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700863 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Manfred Spraul380af1b2008-07-25 01:48:06 -0700865 /* Free the existing undo structures for this semaphore set. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700866 assert_spin_locked(&sma->sem_perm.lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700867 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
868 list_del(&un->list_id);
869 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -0700871 list_del_rcu(&un->list_proc);
872 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +0800873 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700877 INIT_LIST_HEAD(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700878 list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800879 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700880 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700882 for (i = 0; i < sma->sem_nsems; i++) {
883 struct sem *sem = sma->sem_base + i;
884 list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
885 unlink_queue(sma, q);
886 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
887 }
888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700890 /* Remove the semaphore set from the IDR */
891 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700892 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700894 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700895 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 security_sem_free(sma);
897 ipc_rcu_putref(sma);
898}
899
900static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
901{
902 switch(version) {
903 case IPC_64:
904 return copy_to_user(buf, in, sizeof(*in));
905 case IPC_OLD:
906 {
907 struct semid_ds out;
908
Dan Rosenberg982f7c22010-09-30 15:15:31 -0700909 memset(&out, 0, sizeof(out));
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
912
913 out.sem_otime = in->sem_otime;
914 out.sem_ctime = in->sem_ctime;
915 out.sem_nsems = in->sem_nsems;
916
917 return copy_to_user(buf, &out, sizeof(out));
918 }
919 default:
920 return -EINVAL;
921 }
922}
923
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800924static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -0500925 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Amerigo Wange5cc9c72009-12-15 16:47:35 -0800927 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct sem_array *sma;
929
930 switch(cmd) {
931 case IPC_INFO:
932 case SEM_INFO:
933 {
934 struct seminfo seminfo;
935 int max_id;
936
937 err = security_sem_semctl(NULL, cmd);
938 if (err)
939 return err;
940
941 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700942 seminfo.semmni = ns->sc_semmni;
943 seminfo.semmns = ns->sc_semmns;
944 seminfo.semmsl = ns->sc_semmsl;
945 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 seminfo.semvmx = SEMVMX;
947 seminfo.semmnu = SEMMNU;
948 seminfo.semmap = SEMMAP;
949 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700950 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700952 seminfo.semusz = sem_ids(ns).in_use;
953 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 } else {
955 seminfo.semusz = SEMUSZ;
956 seminfo.semaem = SEMAEM;
957 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700958 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700959 up_read(&sem_ids(ns).rw_mutex);
Al Viroe1fd1f42013-03-05 15:04:55 -0500960 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EFAULT;
962 return (max_id < 0) ? 0: max_id;
963 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800964 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 case SEM_STAT:
966 {
967 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700968 int id = 0;
969
970 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800972 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700973 rcu_read_lock();
974 sma = sem_obtain_object(ns, semid);
975 if (IS_ERR(sma)) {
976 err = PTR_ERR(sma);
977 goto out_unlock;
978 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800979 id = sma->sem_perm.id;
980 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700981 rcu_read_lock();
982 sma = sem_obtain_object_check(ns, semid);
983 if (IS_ERR(sma)) {
984 err = PTR_ERR(sma);
985 goto out_unlock;
986 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700990 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto out_unlock;
992
993 err = security_sem_semctl(sma, cmd);
994 if (err)
995 goto out_unlock;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
998 tbuf.sem_otime = sma->sem_otime;
999 tbuf.sem_ctime = sma->sem_ctime;
1000 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001001 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001002 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EFAULT;
1004 return id;
1005 }
1006 default:
1007 return -EINVAL;
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001010 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return err;
1012}
1013
Al Viroe1fd1f42013-03-05 15:04:55 -05001014static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1015 unsigned long arg)
1016{
1017 struct sem_undo *un;
1018 struct sem_array *sma;
1019 struct sem* curr;
1020 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001021 struct list_head tasks;
1022 int val;
1023#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1024 /* big-endian 64bit */
1025 val = arg >> 32;
1026#else
1027 /* 32bit or little-endian 64bit */
1028 val = arg;
1029#endif
1030
Rik van Riel6062a8d2013-04-30 19:15:44 -07001031 if (val > SEMVMX || val < 0)
1032 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001033
1034 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001035
Rik van Riel6062a8d2013-04-30 19:15:44 -07001036 rcu_read_lock();
1037 sma = sem_obtain_object_check(ns, semid);
1038 if (IS_ERR(sma)) {
1039 rcu_read_unlock();
1040 return PTR_ERR(sma);
1041 }
1042
1043 if (semnum < 0 || semnum >= sma->sem_nsems) {
1044 rcu_read_unlock();
1045 return -EINVAL;
1046 }
1047
1048
1049 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1050 rcu_read_unlock();
1051 return -EACCES;
1052 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001053
1054 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001055 if (err) {
1056 rcu_read_unlock();
1057 return -EACCES;
1058 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001059
Rik van Riel6062a8d2013-04-30 19:15:44 -07001060 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001061
1062 curr = &sma->sem_base[semnum];
1063
Al Viroe1fd1f42013-03-05 15:04:55 -05001064 assert_spin_locked(&sma->sem_perm.lock);
1065 list_for_each_entry(un, &sma->list_id, list_id)
1066 un->semadj[semnum] = 0;
1067
1068 curr->semval = val;
1069 curr->sempid = task_tgid_vnr(current);
1070 sma->sem_ctime = get_seconds();
1071 /* maybe some queued-up processes were waiting for this */
1072 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001073 sem_unlock(sma, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001074 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001075 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001076}
1077
Kirill Korotaeve3893532006-10-02 02:18:22 -07001078static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001079 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct sem_array *sma;
1082 struct sem* curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001083 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 ushort fast_sem_io[SEMMSL_FAST];
1085 ushort* sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001086 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001088 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001089
1090 rcu_read_lock();
1091 sma = sem_obtain_object_check(ns, semid);
1092 if (IS_ERR(sma)) {
1093 rcu_read_unlock();
1094 return PTR_ERR(sma);
1095 }
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 nsems = sma->sem_nsems;
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001100 if (ipcperms(ns, &sma->sem_perm,
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001101 cmd == SETALL ? S_IWUGO : S_IRUGO)) {
1102 rcu_read_unlock();
1103 goto out_wakeup;
1104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001107 if (err) {
1108 rcu_read_unlock();
1109 goto out_wakeup;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 err = -EACCES;
1113 switch (cmd) {
1114 case GETALL:
1115 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001116 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int i;
1118
1119 if(nsems > SEMMSL_FAST) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001120 sem_getref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1123 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001124 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -ENOMEM;
1126 }
1127
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001128 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001130 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 err = -EIDRM;
1132 goto out_free;
1133 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001134 } else
1135 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 for (i = 0; i < sma->sem_nsems; i++)
1138 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001139 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 err = 0;
1141 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1142 err = -EFAULT;
1143 goto out_free;
1144 }
1145 case SETALL:
1146 {
1147 int i;
1148 struct sem_undo *un;
1149
Rik van Riel6062a8d2013-04-30 19:15:44 -07001150 if (!ipc_rcu_getref(sma)) {
1151 rcu_read_unlock();
1152 return -EIDRM;
1153 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001154 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if(nsems > SEMMSL_FAST) {
1157 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1158 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001159 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return -ENOMEM;
1161 }
1162 }
1163
Al Viroe1fd1f42013-03-05 15:04:55 -05001164 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001165 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 err = -EFAULT;
1167 goto out_free;
1168 }
1169
1170 for (i = 0; i < nsems; i++) {
1171 if (sem_io[i] > SEMVMX) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001172 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 err = -ERANGE;
1174 goto out_free;
1175 }
1176 }
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001177 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001179 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 err = -EIDRM;
1181 goto out_free;
1182 }
1183
1184 for (i = 0; i < nsems; i++)
1185 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001186
1187 assert_spin_locked(&sma->sem_perm.lock);
1188 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 for (i = 0; i < nsems; i++)
1190 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sma->sem_ctime = get_seconds();
1193 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001194 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 err = 0;
1196 goto out_unlock;
1197 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001198 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001201 if (semnum < 0 || semnum >= nsems) {
1202 rcu_read_unlock();
1203 goto out_wakeup;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Rik van Riel6062a8d2013-04-30 19:15:44 -07001206 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 curr = &sma->sem_base[semnum];
1208
1209 switch (cmd) {
1210 case GETVAL:
1211 err = curr->semval;
1212 goto out_unlock;
1213 case GETPID:
1214 err = curr->sempid;
1215 goto out_unlock;
1216 case GETNCNT:
1217 err = count_semncnt(sma,semnum);
1218 goto out_unlock;
1219 case GETZCNT:
1220 err = count_semzcnt(sma,semnum);
1221 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001225 sem_unlock(sma, -1);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001226out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001227 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228out_free:
1229 if(sem_io != fast_sem_io)
1230 ipc_free(sem_io, sizeof(ushort)*nsems);
1231 return err;
1232}
1233
Pierre Peiffer016d7132008-04-29 01:00:50 -07001234static inline unsigned long
1235copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 switch(version) {
1238 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001239 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 case IPC_OLD:
1243 {
1244 struct semid_ds tbuf_old;
1245
1246 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1247 return -EFAULT;
1248
Pierre Peiffer016d7132008-04-29 01:00:50 -07001249 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1250 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1251 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 return 0;
1254 }
1255 default:
1256 return -EINVAL;
1257 }
1258}
1259
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001260/*
1261 * This function handles some semctl commands which require the rw_mutex
1262 * to be held in write mode.
1263 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1264 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001265static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001266 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct sem_array *sma;
1269 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001270 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct kern_ipc_perm *ipcp;
1272
1273 if(cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001274 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001278 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1279 &semid64.sem_perm, 0);
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001280 if (IS_ERR(ipcp))
1281 return PTR_ERR(ipcp);
Steve Grubb073115d2006-04-02 17:07:33 -04001282
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001283 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001286 if (err) {
1287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 goto out_unlock;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 switch(cmd){
1292 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001293 sem_lock(sma, NULL, -1);
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001294 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001295 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001297 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001298 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1299 if (err)
1300 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303 default:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001304 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001306 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001310 sem_unlock(sma, -1);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001311out_up:
1312 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return err;
1314}
1315
Al Viroe1fd1f42013-03-05 15:04:55 -05001316SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001319 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001320 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 if (semid < 0)
1323 return -EINVAL;
1324
1325 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001326 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 switch(cmd) {
1329 case IPC_INFO:
1330 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001331 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001333 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 case GETALL:
1335 case GETVAL:
1336 case GETPID:
1337 case GETNCNT:
1338 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001340 return semctl_main(ns, semid, semnum, cmd, p);
1341 case SETVAL:
1342 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case IPC_RMID:
1344 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001345 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 default:
1347 return -EINVAL;
1348 }
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/* If the task doesn't already have a undo_list, then allocate one
1352 * here. We guarantee there is only one thread using this undo list,
1353 * and current is THE ONE
1354 *
1355 * If this allocation and assignment succeeds, but later
1356 * portions of this code fail, there is no need to free the sem_undo_list.
1357 * Just let it stay associated with the task, and it'll be freed later
1358 * at exit time.
1359 *
1360 * This can block, so callers must hold no locks.
1361 */
1362static inline int get_undo_list(struct sem_undo_list **undo_listp)
1363{
1364 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 undo_list = current->sysvsem.undo_list;
1367 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001368 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (undo_list == NULL)
1370 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001371 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001373 INIT_LIST_HEAD(&undo_list->list_proc);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 current->sysvsem.undo_list = undo_list;
1376 }
1377 *undo_listp = undo_list;
1378 return 0;
1379}
1380
Nick Pigginbf17bb72009-12-15 16:47:28 -08001381static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001383 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Nick Pigginbf17bb72009-12-15 16:47:28 -08001385 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1386 if (un->semid == semid)
1387 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001389 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Nick Pigginbf17bb72009-12-15 16:47:28 -08001392static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1393{
1394 struct sem_undo *un;
1395
1396 assert_spin_locked(&ulp->lock);
1397
1398 un = __lookup_undo(ulp, semid);
1399 if (un) {
1400 list_del_rcu(&un->list_proc);
1401 list_add_rcu(&un->list_proc, &ulp->list_proc);
1402 }
1403 return un;
1404}
1405
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001406/**
1407 * find_alloc_undo - Lookup (and if not present create) undo array
1408 * @ns: namespace
1409 * @semid: semaphore array id
1410 *
1411 * The function looks up (and if not present creates) the undo structure.
1412 * The size of the undo structure depends on the size of the semaphore
1413 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001414 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1415 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001416 */
1417static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
1419 struct sem_array *sma;
1420 struct sem_undo_list *ulp;
1421 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001422 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 error = get_undo_list(&ulp);
1425 if (error)
1426 return ERR_PTR(error);
1427
Manfred Spraul380af1b2008-07-25 01:48:06 -07001428 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001429 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001431 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (likely(un!=NULL))
1433 goto out;
1434
1435 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001436 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001437 sma = sem_obtain_object_check(ns, semid);
1438 if (IS_ERR(sma)) {
1439 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001440 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001441 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001444 if (!ipc_rcu_getref(sma)) {
1445 rcu_read_unlock();
1446 un = ERR_PTR(-EIDRM);
1447 goto out;
1448 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001449 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001451 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001452 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!new) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001454 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return ERR_PTR(-ENOMEM);
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Manfred Spraul380af1b2008-07-25 01:48:06 -07001458 /* step 3: Acquire the lock on semaphore array */
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001459 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001461 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 kfree(new);
1463 un = ERR_PTR(-EIDRM);
1464 goto out;
1465 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001466 spin_lock(&ulp->lock);
1467
1468 /*
1469 * step 4: check for races: did someone else allocate the undo struct?
1470 */
1471 un = lookup_undo(ulp, semid);
1472 if (un) {
1473 kfree(new);
1474 goto success;
1475 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001476 /* step 5: initialize & link new undo structure */
1477 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001478 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001479 new->semid = semid;
1480 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001481 list_add_rcu(&new->list_proc, &ulp->list_proc);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001482 assert_spin_locked(&sma->sem_perm.lock);
1483 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001484 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001485
1486success:
1487 spin_unlock(&ulp->lock);
1488 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001489 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490out:
1491 return un;
1492}
1493
Manfred Spraulc61284e2010-07-20 13:24:23 -07001494
1495/**
1496 * get_queue_result - Retrieve the result code from sem_queue
1497 * @q: Pointer to queue structure
1498 *
1499 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1500 * q->status, then we must loop until the value is replaced with the final
1501 * value: This may happen if a task is woken up by an unrelated event (e.g.
1502 * signal) and in parallel the task is woken up by another task because it got
1503 * the requested semaphores.
1504 *
1505 * The function can be called with or without holding the semaphore spinlock.
1506 */
1507static int get_queue_result(struct sem_queue *q)
1508{
1509 int error;
1510
1511 error = q->status;
1512 while (unlikely(error == IN_WAKEUP)) {
1513 cpu_relax();
1514 error = q->status;
1515 }
1516
1517 return error;
1518}
1519
1520
Heiko Carstensd5460c92009-01-14 14:14:27 +01001521SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1522 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int error = -EINVAL;
1525 struct sem_array *sma;
1526 struct sembuf fast_sops[SEMOPM_FAST];
1527 struct sembuf* sops = fast_sops, *sop;
1528 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001529 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct sem_queue queue;
1531 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001532 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001533 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001534
1535 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 if (nsops < 1 || semid < 0)
1538 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001539 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -E2BIG;
1541 if(nsops > SEMOPM_FAST) {
1542 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1543 if(sops==NULL)
1544 return -ENOMEM;
1545 }
1546 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1547 error=-EFAULT;
1548 goto out_free;
1549 }
1550 if (timeout) {
1551 struct timespec _timeout;
1552 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1553 error = -EFAULT;
1554 goto out_free;
1555 }
1556 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1557 _timeout.tv_nsec >= 1000000000L) {
1558 error = -EINVAL;
1559 goto out_free;
1560 }
1561 jiffies_left = timespec_to_jiffies(&_timeout);
1562 }
1563 max = 0;
1564 for (sop = sops; sop < sops + nsops; sop++) {
1565 if (sop->sem_num >= max)
1566 max = sop->sem_num;
1567 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001568 undos = 1;
1569 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 alter = 1;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Rik van Riel6062a8d2013-04-30 19:15:44 -07001573 INIT_LIST_HEAD(&tasks);
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001576 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001577 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (IS_ERR(un)) {
1579 error = PTR_ERR(un);
1580 goto out_free;
1581 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001584 rcu_read_lock();
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001587 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001588 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001589 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001590 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001592 }
1593
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001594 error = -EFBIG;
1595 if (max >= sma->sem_nsems) {
1596 rcu_read_unlock();
1597 goto out_wakeup;
1598 }
1599
1600 error = -EACCES;
1601 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1602 rcu_read_unlock();
1603 goto out_wakeup;
1604 }
1605
1606 error = security_sem_semop(sma, sops, nsops, alter);
1607 if (error) {
1608 rcu_read_unlock();
1609 goto out_wakeup;
1610 }
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001613 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001615 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001616 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001617 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001619 error = -EIDRM;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001620 locknum = sem_lock(sma, sops, nsops);
1621 if (un && un->semid == -1)
1622 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001623
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001624 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 if (error <= 0) {
1626 if (alter && error == 0)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001627 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul636c6be2009-12-15 16:47:33 -08001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 goto out_unlock_free;
1630 }
1631
1632 /* We need to sleep on this operation, so we put the current
1633 * task into the pending queue and go to sleep.
1634 */
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 queue.sops = sops;
1637 queue.nsops = nsops;
1638 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001639 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Manfred Spraulb97e8202009-12-15 16:47:32 -08001642 if (nsops == 1) {
1643 struct sem *curr;
1644 curr = &sma->sem_base[sops->sem_num];
1645
1646 if (alter)
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001647 list_add_tail(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001648 else
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001649 list_add(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001650 } else {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001651 if (alter)
1652 list_add_tail(&queue.list, &sma->sem_pending);
1653 else
1654 list_add(&queue.list, &sma->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001655 sma->complex_count++;
1656 }
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 queue.status = -EINTR;
1659 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001660
1661sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 current->state = TASK_INTERRUPTIBLE;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001663 sem_unlock(sma, locknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 if (timeout)
1666 jiffies_left = schedule_timeout(jiffies_left);
1667 else
1668 schedule();
1669
Manfred Spraulc61284e2010-07-20 13:24:23 -07001670 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (error != -EINTR) {
1673 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001674 * resources.
1675 * Perform a smp_mb(): User space could assume that semop()
1676 * is a memory barrier: Without the mb(), the cpu could
1677 * speculatively read in user space stale data that was
1678 * overwritten by the previous owner of the semaphore.
1679 */
1680 smp_mb();
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 goto out_free;
1683 }
1684
Rik van Riel6062a8d2013-04-30 19:15:44 -07001685 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001686
1687 /*
1688 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1689 */
1690 error = get_queue_result(&queue);
1691
1692 /*
1693 * Array removed? If yes, leave without sem_unlock().
1694 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001695 if (IS_ERR(sma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto out_free;
1697 }
1698
Manfred Spraulc61284e2010-07-20 13:24:23 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001701 * If queue.status != -EINTR we are woken up by another process.
1702 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 */
Manfred Spraulc61284e2010-07-20 13:24:23 -07001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (error != -EINTR) {
1706 goto out_unlock_free;
1707 }
1708
1709 /*
1710 * If an interrupt occurred we have to clean up the queue
1711 */
1712 if (timeout && jiffies_left == 0)
1713 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001714
1715 /*
1716 * If the wakeup was spurious, just retry
1717 */
1718 if (error == -EINTR && !signal_pending(current))
1719 goto sleep_again;
1720
Manfred Spraulb97e8202009-12-15 16:47:32 -08001721 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001724 sem_unlock(sma, locknum);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001725out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001726 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727out_free:
1728 if(sops != fast_sops)
1729 kfree(sops);
1730 return error;
1731}
1732
Heiko Carstensd5460c92009-01-14 14:14:27 +01001733SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1734 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 return sys_semtimedop(semid, tsops, nsops, NULL);
1737}
1738
1739/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1740 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
1742
1743int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1744{
1745 struct sem_undo_list *undo_list;
1746 int error;
1747
1748 if (clone_flags & CLONE_SYSVSEM) {
1749 error = get_undo_list(&undo_list);
1750 if (error)
1751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 atomic_inc(&undo_list->refcnt);
1753 tsk->sysvsem.undo_list = undo_list;
1754 } else
1755 tsk->sysvsem.undo_list = NULL;
1756
1757 return 0;
1758}
1759
1760/*
1761 * add semadj values to semaphores, free undo structures.
1762 * undo structures are not freed when semaphore arrays are destroyed
1763 * so some of them may be out of date.
1764 * IMPLEMENTATION NOTE: There is some confusion over whether the
1765 * set of adjustments that needs to be done should be done in an atomic
1766 * manner or not. That is, if we are attempting to decrement the semval
1767 * should we queue up and wait until we can do so legally?
1768 * The original implementation attempted to do this (queue and wait).
1769 * The current implementation does not do so. The POSIX standard
1770 * and SVID should be consulted to determine what behavior is mandated.
1771 */
1772void exit_sem(struct task_struct *tsk)
1773{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001774 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001776 ulp = tsk->sysvsem.undo_list;
1777 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07001779 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001781 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return;
1783
Manfred Spraul380af1b2008-07-25 01:48:06 -07001784 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001786 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001787 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001788 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Manfred Spraul380af1b2008-07-25 01:48:06 -07001790 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02001791 un = list_entry_rcu(ulp->list_proc.next,
1792 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001793 if (&un->list_proc == &ulp->list_proc)
1794 semid = -1;
1795 else
1796 semid = un->semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001797
Rik van Riel6062a8d2013-04-30 19:15:44 -07001798 if (semid == -1) {
1799 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001800 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001801 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001802
Rik van Riel6062a8d2013-04-30 19:15:44 -07001803 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001804 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001805 if (IS_ERR(sma)) {
1806 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001807 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Rik van Riel6062a8d2013-04-30 19:15:44 -07001810 sem_lock(sma, NULL, -1);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001811 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001812 if (un == NULL) {
1813 /* exit_sem raced with IPC_RMID+semget() that created
1814 * exactly the same semid. Nothing to do.
1815 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001816 sem_unlock(sma, -1);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001817 continue;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Manfred Spraul380af1b2008-07-25 01:48:06 -07001820 /* remove un from the linked lists */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001821 assert_spin_locked(&sma->sem_perm.lock);
1822 list_del(&un->list_id);
1823
Manfred Spraul380af1b2008-07-25 01:48:06 -07001824 spin_lock(&ulp->lock);
1825 list_del_rcu(&un->list_proc);
1826 spin_unlock(&ulp->lock);
1827
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001828 /* perform adjustments registered in un */
1829 for (i = 0; i < sma->sem_nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001830 struct sem * semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001831 if (un->semadj[i]) {
1832 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Range checks of the new semaphore value,
1835 * not defined by sus:
1836 * - Some unices ignore the undo entirely
1837 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1838 * - some cap the value (e.g. FreeBSD caps
1839 * at 0, but doesn't enforce SEMVMX)
1840 *
1841 * Linux caps the semaphore value, both at 0
1842 * and at SEMVMX.
1843 *
1844 * Manfred <manfred@colorfullife.com>
1845 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001846 if (semaphore->semval < 0)
1847 semaphore->semval = 0;
1848 if (semaphore->semval > SEMVMX)
1849 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001850 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001854 INIT_LIST_HEAD(&tasks);
1855 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001856 sem_unlock(sma, -1);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001857 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001858
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001859 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001861 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
1864#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001865static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001867 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001868 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Mike Waychison19b49462005-09-06 15:17:10 -07001870 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08001871 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07001872 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001873 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001874 sma->sem_perm.mode,
1875 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001876 from_kuid_munged(user_ns, sma->sem_perm.uid),
1877 from_kgid_munged(user_ns, sma->sem_perm.gid),
1878 from_kuid_munged(user_ns, sma->sem_perm.cuid),
1879 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Mike Waychison19b49462005-09-06 15:17:10 -07001880 sma->sem_otime,
1881 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883#endif