blob: 4734e9c2a98a82d1bad227b68977ba8efcb51d46 [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
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700331static inline void sem_putref(struct sem_array *sma)
332{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700333 sem_lock_and_putref(sma);
334 sem_unlock(sma, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700335}
336
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700337static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
338{
339 ipc_rmid(&sem_ids(ns), &s->sem_perm);
340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*
343 * Lockless wakeup algorithm:
344 * Without the check/retry algorithm a lockless wakeup is possible:
345 * - queue.status is initialized to -EINTR before blocking.
346 * - wakeup is performed by
347 * * unlinking the queue entry from sma->sem_pending
348 * * setting queue.status to IN_WAKEUP
349 * This is the notification for the blocked thread that a
350 * result value is imminent.
351 * * call wake_up_process
352 * * set queue.status to the final value.
353 * - the previously blocked thread checks queue.status:
354 * * if it's IN_WAKEUP, then it must wait until the value changes
355 * * if it's not -EINTR, then the operation was completed by
356 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800357 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * * otherwise it must acquire the spinlock and check what's up.
359 *
360 * The two-stage algorithm is necessary to protect against the following
361 * races:
362 * - if queue.status is set after wake_up_process, then the woken up idle
363 * thread could race forward and try (and fail) to acquire sma->lock
364 * before update_queue had a chance to set queue.status
365 * - if queue.status is written before wake_up_process and if the
366 * blocked process is woken up by a signal between writing
367 * queue.status and the wake_up_process, then the woken up
368 * process could return from semtimedop and die by calling
369 * sys_exit before wake_up_process is called. Then wake_up_process
370 * will oops, because the task structure is already invalid.
371 * (yes, this happened on s390 with sysv msg).
372 *
373 */
374#define IN_WAKEUP 1
375
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700376/**
377 * newary - Create a new semaphore set
378 * @ns: namespace
379 * @params: ptr to the structure that contains key, semflg and nsems
380 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700381 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700382 */
383
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700384static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int id;
387 int retval;
388 struct sem_array *sma;
389 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700390 key_t key = params->key;
391 int nsems = params->u.nsems;
392 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800393 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!nsems)
396 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700397 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -ENOSPC;
399
400 size = sizeof (*sma) + nsems * sizeof (struct sem);
401 sma = ipc_rcu_alloc(size);
402 if (!sma) {
403 return -ENOMEM;
404 }
405 memset (sma, 0, size);
406
407 sma->sem_perm.mode = (semflg & S_IRWXUGO);
408 sma->sem_perm.key = key;
409
410 sma->sem_perm.security = NULL;
411 retval = security_sem_alloc(sma);
412 if (retval) {
413 ipc_rcu_putref(sma);
414 return retval;
415 }
416
Kirill Korotaeve3893532006-10-02 02:18:22 -0700417 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700418 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 security_sem_free(sma);
420 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700421 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700423 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800426
Rik van Riel6062a8d2013-04-30 19:15:44 -0700427 for (i = 0; i < nsems; i++) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800428 INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700429 spin_lock_init(&sma->sem_base[i].lock);
430 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800431
432 sma->complex_count = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700433 INIT_LIST_HEAD(&sma->sem_pending);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700434 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sma->sem_nsems = nsems;
436 sma->sem_ctime = get_seconds();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700437 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700439 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700442
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700443/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700444 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700445 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700446static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700448 struct sem_array *sma;
449
450 sma = container_of(ipcp, struct sem_array, sem_perm);
451 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700452}
453
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700454/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700455 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700456 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700457static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
458 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700459{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700460 struct sem_array *sma;
461
462 sma = container_of(ipcp, struct sem_array, sem_perm);
463 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700464 return -EINVAL;
465
466 return 0;
467}
468
Heiko Carstensd5460c92009-01-14 14:14:27 +0100469SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700470{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700471 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700472 struct ipc_ops sem_ops;
473 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Kirill Korotaeve3893532006-10-02 02:18:22 -0700475 ns = current->nsproxy->ipc_ns;
476
477 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700479
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700480 sem_ops.getnew = newary;
481 sem_ops.associate = sem_security;
482 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700483
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700484 sem_params.key = key;
485 sem_params.flg = semflg;
486 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700487
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700488 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/*
492 * Determine whether a sequence of semaphore operations would succeed
493 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
494 */
495
496static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
497 int nsops, struct sem_undo *un, int pid)
498{
499 int result, sem_op;
500 struct sembuf *sop;
501 struct sem * curr;
502
503 for (sop = sops; sop < sops + nsops; sop++) {
504 curr = sma->sem_base + sop->sem_num;
505 sem_op = sop->sem_op;
506 result = curr->semval;
507
508 if (!sem_op && result)
509 goto would_block;
510
511 result += sem_op;
512 if (result < 0)
513 goto would_block;
514 if (result > SEMVMX)
515 goto out_of_range;
516 if (sop->sem_flg & SEM_UNDO) {
517 int undo = un->semadj[sop->sem_num] - sem_op;
518 /*
519 * Exceeding the undo range is an error.
520 */
521 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
522 goto out_of_range;
523 }
524 curr->semval = result;
525 }
526
527 sop--;
528 while (sop >= sops) {
529 sma->sem_base[sop->sem_num].sempid = pid;
530 if (sop->sem_flg & SEM_UNDO)
531 un->semadj[sop->sem_num] -= sop->sem_op;
532 sop--;
533 }
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return 0;
536
537out_of_range:
538 result = -ERANGE;
539 goto undo;
540
541would_block:
542 if (sop->sem_flg & IPC_NOWAIT)
543 result = -EAGAIN;
544 else
545 result = 1;
546
547undo:
548 sop--;
549 while (sop >= sops) {
550 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
551 sop--;
552 }
553
554 return result;
555}
556
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700557/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
558 * @q: queue entry that must be signaled
559 * @error: Error value for the signal
560 *
561 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800562 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700563static void wake_up_sem_queue_prepare(struct list_head *pt,
564 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800565{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700566 if (list_empty(pt)) {
567 /*
568 * Hold preempt off so that we don't get preempted and have the
569 * wakee busy-wait until we're scheduled back on.
570 */
571 preempt_disable();
572 }
Nick Piggind4212092009-12-15 16:47:30 -0800573 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700574 q->pid = error;
575
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700576 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700577}
578
579/**
580 * wake_up_sem_queue_do(pt) - do the actual wake-up
581 * @pt: list of tasks to be woken up
582 *
583 * Do the actual wake-up.
584 * The function is called without any locks held, thus the semaphore array
585 * could be destroyed already and the tasks can disappear as soon as the
586 * status is set to the actual return code.
587 */
588static void wake_up_sem_queue_do(struct list_head *pt)
589{
590 struct sem_queue *q, *t;
591 int did_something;
592
593 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700594 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700595 wake_up_process(q->sleeper);
596 /* q can disappear immediately after writing q->status. */
597 smp_wmb();
598 q->status = q->pid;
599 }
600 if (did_something)
601 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800602}
603
Manfred Spraulb97e8202009-12-15 16:47:32 -0800604static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
605{
606 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700607 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800608 sma->complex_count--;
609}
610
Manfred Spraulfd5db422010-05-26 14:43:40 -0700611/** check_restart(sma, q)
612 * @sma: semaphore array
613 * @q: the operation that just completed
614 *
615 * update_queue is O(N^2) when it restarts scanning the whole queue of
616 * waiting operations. Therefore this function checks if the restart is
617 * really necessary. It is called after a previously waiting operation
618 * was completed.
619 */
620static int check_restart(struct sem_array *sma, struct sem_queue *q)
621{
622 struct sem *curr;
623 struct sem_queue *h;
624
625 /* if the operation didn't modify the array, then no restart */
626 if (q->alter == 0)
627 return 0;
628
629 /* pending complex operations are too difficult to analyse */
630 if (sma->complex_count)
631 return 1;
632
633 /* we were a sleeping complex operation. Too difficult */
634 if (q->nsops > 1)
635 return 1;
636
637 curr = sma->sem_base + q->sops[0].sem_num;
638
639 /* No-one waits on this queue */
640 if (list_empty(&curr->sem_pending))
641 return 0;
642
643 /* the new semaphore value */
644 if (curr->semval) {
645 /* It is impossible that someone waits for the new value:
646 * - q is a previously sleeping simple operation that
647 * altered the array. It must be a decrement, because
648 * simple increments never sleep.
649 * - The value is not 0, thus wait-for-zero won't proceed.
650 * - If there are older (higher priority) decrements
651 * in the queue, then they have observed the original
652 * semval value and couldn't proceed. The operation
653 * decremented to value - thus they won't proceed either.
654 */
655 BUG_ON(q->sops[0].sem_op >= 0);
656 return 0;
657 }
658 /*
659 * semval is 0. Check if there are wait-for-zero semops.
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700660 * They must be the first entries in the per-semaphore queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700661 */
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700662 h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700663 BUG_ON(h->nsops != 1);
664 BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
665
666 /* Yes, there is a wait-for-zero semop. Restart */
667 if (h->sops[0].sem_op == 0)
668 return 1;
669
670 /* Again - no-one is waiting for the new value. */
671 return 0;
672}
673
Manfred Spraul636c6be2009-12-15 16:47:33 -0800674
675/**
676 * update_queue(sma, semnum): Look for tasks that can be completed.
677 * @sma: semaphore array.
678 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700679 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800680 *
681 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700682 * was modified. If multiple semaphores were modified, update_queue must
683 * be called with semnum = -1, as well as with the number of each modified
684 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700685 * The tasks that must be woken up are added to @pt. The return code
686 * is stored in q->pid.
687 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700689static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800691 struct sem_queue *q;
692 struct list_head *walk;
693 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700694 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800695
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700696 if (semnum == -1)
Manfred Spraul636c6be2009-12-15 16:47:33 -0800697 pending_list = &sma->sem_pending;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700698 else
Manfred Spraul636c6be2009-12-15 16:47:33 -0800699 pending_list = &sma->sem_base[semnum].sem_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Nick Piggin9cad2002009-12-15 16:47:29 -0800701again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800702 walk = pending_list->next;
703 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700704 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800705
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700706 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800707 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800708
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800709 /* If we are scanning the single sop, per-semaphore list of
710 * one semaphore and that semaphore is 0, then it is not
711 * necessary to scan the "alter" entries: simple increments
712 * that affect only one entry succeed immediately and cannot
713 * be in the per semaphore pending queue, and decrements
714 * cannot be successful if the value is already 0.
715 */
716 if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
717 q->alter)
718 break;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 error = try_atomic_semop(sma, q->sops, q->nsops,
721 q->undo, q->pid);
722
723 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800724 if (error > 0)
725 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700726
Manfred Spraulb97e8202009-12-15 16:47:32 -0800727 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700728
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700729 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700730 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700731 } else {
732 semop_completed = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700733 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700734 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700735
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700736 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700737 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800738 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700740 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700743/**
744 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700745 * @sma: semaphore array
746 * @sops: operations that were performed
747 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700748 * @otime: force setting otime
749 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700750 *
751 * do_smart_update() does the required called to update_queue, based on the
752 * actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700753 * Note that the function does not do the actual wake-up: the caller is
754 * responsible for calling wake_up_sem_queue_do(@pt).
755 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700756 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700757static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
758 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700759{
760 int i;
761
762 if (sma->complex_count || sops == NULL) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700763 if (update_queue(sma, -1, pt))
764 otime = 1;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700765 }
766
767 if (!sops) {
768 /* No semops; something special is going on. */
769 for (i = 0; i < sma->sem_nsems; i++) {
770 if (update_queue(sma, i, pt))
771 otime = 1;
772 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700773 goto done;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700774 }
775
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700776 /* Check the semaphores that were modified. */
Manfred Spraulfd5db422010-05-26 14:43:40 -0700777 for (i = 0; i < nsops; i++) {
778 if (sops[i].sem_op > 0 ||
779 (sops[i].sem_op < 0 &&
780 sma->sem_base[sops[i].sem_num].semval == 0))
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700781 if (update_queue(sma, sops[i].sem_num, pt))
782 otime = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700783 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700784done:
785 if (otime)
786 sma->sem_otime = get_seconds();
Manfred Spraulfd5db422010-05-26 14:43:40 -0700787}
788
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/* The following counts are associated to each semaphore:
791 * semncnt number of tasks waiting on semval being nonzero
792 * semzcnt number of tasks waiting on semval being zero
793 * This model assumes that a task waits on exactly one semaphore.
794 * Since semaphore operations are to be performed atomically, tasks actually
795 * wait on a whole sequence of semaphores simultaneously.
796 * The counts we return here are a rough approximation, but still
797 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
798 */
799static int count_semncnt (struct sem_array * sma, ushort semnum)
800{
801 int semncnt;
802 struct sem_queue * q;
803
804 semncnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700805 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct sembuf * sops = q->sops;
807 int nsops = q->nsops;
808 int i;
809 for (i = 0; i < nsops; i++)
810 if (sops[i].sem_num == semnum
811 && (sops[i].sem_op < 0)
812 && !(sops[i].sem_flg & IPC_NOWAIT))
813 semncnt++;
814 }
815 return semncnt;
816}
Manfred Spraula1193f82008-07-25 01:48:06 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818static int count_semzcnt (struct sem_array * sma, ushort semnum)
819{
820 int semzcnt;
821 struct sem_queue * q;
822
823 semzcnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700824 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct sembuf * sops = q->sops;
826 int nsops = q->nsops;
827 int i;
828 for (i = 0; i < nsops; i++)
829 if (sops[i].sem_num == semnum
830 && (sops[i].sem_op == 0)
831 && !(sops[i].sem_flg & IPC_NOWAIT))
832 semzcnt++;
833 }
834 return semzcnt;
835}
836
Nadia Derbey3e148c72007-10-18 23:40:54 -0700837/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
838 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
839 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800841static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Manfred Spraul380af1b2008-07-25 01:48:06 -0700843 struct sem_undo *un, *tu;
844 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800845 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700846 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700847 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Manfred Spraul380af1b2008-07-25 01:48:06 -0700849 /* Free the existing undo structures for this semaphore set. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700850 assert_spin_locked(&sma->sem_perm.lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700851 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
852 list_del(&un->list_id);
853 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -0700855 list_del_rcu(&un->list_proc);
856 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +0800857 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700861 INIT_LIST_HEAD(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700862 list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800863 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700864 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700866 for (i = 0; i < sma->sem_nsems; i++) {
867 struct sem *sem = sma->sem_base + i;
868 list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
869 unlink_queue(sma, q);
870 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
871 }
872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700874 /* Remove the semaphore set from the IDR */
875 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700876 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700878 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700879 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 security_sem_free(sma);
881 ipc_rcu_putref(sma);
882}
883
884static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
885{
886 switch(version) {
887 case IPC_64:
888 return copy_to_user(buf, in, sizeof(*in));
889 case IPC_OLD:
890 {
891 struct semid_ds out;
892
Dan Rosenberg982f7c22010-09-30 15:15:31 -0700893 memset(&out, 0, sizeof(out));
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
896
897 out.sem_otime = in->sem_otime;
898 out.sem_ctime = in->sem_ctime;
899 out.sem_nsems = in->sem_nsems;
900
901 return copy_to_user(buf, &out, sizeof(out));
902 }
903 default:
904 return -EINVAL;
905 }
906}
907
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800908static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -0500909 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Amerigo Wange5cc9c72009-12-15 16:47:35 -0800911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct sem_array *sma;
913
914 switch(cmd) {
915 case IPC_INFO:
916 case SEM_INFO:
917 {
918 struct seminfo seminfo;
919 int max_id;
920
921 err = security_sem_semctl(NULL, cmd);
922 if (err)
923 return err;
924
925 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700926 seminfo.semmni = ns->sc_semmni;
927 seminfo.semmns = ns->sc_semmns;
928 seminfo.semmsl = ns->sc_semmsl;
929 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 seminfo.semvmx = SEMVMX;
931 seminfo.semmnu = SEMMNU;
932 seminfo.semmap = SEMMAP;
933 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700934 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700936 seminfo.semusz = sem_ids(ns).in_use;
937 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 } else {
939 seminfo.semusz = SEMUSZ;
940 seminfo.semaem = SEMAEM;
941 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700942 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700943 up_read(&sem_ids(ns).rw_mutex);
Al Viroe1fd1f42013-03-05 15:04:55 -0500944 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EFAULT;
946 return (max_id < 0) ? 0: max_id;
947 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800948 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 case SEM_STAT:
950 {
951 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700952 int id = 0;
953
954 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800956 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700957 rcu_read_lock();
958 sma = sem_obtain_object(ns, semid);
959 if (IS_ERR(sma)) {
960 err = PTR_ERR(sma);
961 goto out_unlock;
962 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800963 id = sma->sem_perm.id;
964 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700965 rcu_read_lock();
966 sma = sem_obtain_object_check(ns, semid);
967 if (IS_ERR(sma)) {
968 err = PTR_ERR(sma);
969 goto out_unlock;
970 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700974 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto out_unlock;
976
977 err = security_sem_semctl(sma, cmd);
978 if (err)
979 goto out_unlock;
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
982 tbuf.sem_otime = sma->sem_otime;
983 tbuf.sem_ctime = sma->sem_ctime;
984 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700985 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -0500986 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 return -EFAULT;
988 return id;
989 }
990 default:
991 return -EINVAL;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700994 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return err;
996}
997
Al Viroe1fd1f42013-03-05 15:04:55 -0500998static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
999 unsigned long arg)
1000{
1001 struct sem_undo *un;
1002 struct sem_array *sma;
1003 struct sem* curr;
1004 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001005 struct list_head tasks;
1006 int val;
1007#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1008 /* big-endian 64bit */
1009 val = arg >> 32;
1010#else
1011 /* 32bit or little-endian 64bit */
1012 val = arg;
1013#endif
1014
Rik van Riel6062a8d2013-04-30 19:15:44 -07001015 if (val > SEMVMX || val < 0)
1016 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001017
1018 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001019
Rik van Riel6062a8d2013-04-30 19:15:44 -07001020 rcu_read_lock();
1021 sma = sem_obtain_object_check(ns, semid);
1022 if (IS_ERR(sma)) {
1023 rcu_read_unlock();
1024 return PTR_ERR(sma);
1025 }
1026
1027 if (semnum < 0 || semnum >= sma->sem_nsems) {
1028 rcu_read_unlock();
1029 return -EINVAL;
1030 }
1031
1032
1033 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1034 rcu_read_unlock();
1035 return -EACCES;
1036 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001037
1038 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001039 if (err) {
1040 rcu_read_unlock();
1041 return -EACCES;
1042 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001043
Rik van Riel6062a8d2013-04-30 19:15:44 -07001044 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001045
1046 curr = &sma->sem_base[semnum];
1047
Al Viroe1fd1f42013-03-05 15:04:55 -05001048 assert_spin_locked(&sma->sem_perm.lock);
1049 list_for_each_entry(un, &sma->list_id, list_id)
1050 un->semadj[semnum] = 0;
1051
1052 curr->semval = val;
1053 curr->sempid = task_tgid_vnr(current);
1054 sma->sem_ctime = get_seconds();
1055 /* maybe some queued-up processes were waiting for this */
1056 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001057 sem_unlock(sma, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001058 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001059 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001060}
1061
Kirill Korotaeve3893532006-10-02 02:18:22 -07001062static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001063 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct sem_array *sma;
1066 struct sem* curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001067 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ushort fast_sem_io[SEMMSL_FAST];
1069 ushort* sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001070 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001072 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001073
1074 rcu_read_lock();
1075 sma = sem_obtain_object_check(ns, semid);
1076 if (IS_ERR(sma)) {
1077 rcu_read_unlock();
1078 return PTR_ERR(sma);
1079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 nsems = sma->sem_nsems;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001084 if (ipcperms(ns, &sma->sem_perm,
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001085 cmd == SETALL ? S_IWUGO : S_IRUGO)) {
1086 rcu_read_unlock();
1087 goto out_wakeup;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001091 if (err) {
1092 rcu_read_unlock();
1093 goto out_wakeup;
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 err = -EACCES;
1097 switch (cmd) {
1098 case GETALL:
1099 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001100 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int i;
1102
Al Viroce857222013-05-03 00:30:49 +01001103 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if(nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001105 if (!ipc_rcu_getref(sma)) {
1106 sem_unlock(sma, -1);
1107 err = -EIDRM;
1108 goto out_free;
1109 }
1110 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1112 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001113 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return -ENOMEM;
1115 }
1116
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001117 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001119 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 err = -EIDRM;
1121 goto out_free;
1122 }
Al Viroce857222013-05-03 00:30:49 +01001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 for (i = 0; i < sma->sem_nsems; i++)
1125 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001126 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 err = 0;
1128 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1129 err = -EFAULT;
1130 goto out_free;
1131 }
1132 case SETALL:
1133 {
1134 int i;
1135 struct sem_undo *un;
1136
Rik van Riel6062a8d2013-04-30 19:15:44 -07001137 if (!ipc_rcu_getref(sma)) {
1138 rcu_read_unlock();
1139 return -EIDRM;
1140 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if(nsems > SEMMSL_FAST) {
1144 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1145 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001146 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -ENOMEM;
1148 }
1149 }
1150
Al Viroe1fd1f42013-03-05 15:04:55 -05001151 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001152 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 err = -EFAULT;
1154 goto out_free;
1155 }
1156
1157 for (i = 0; i < nsems; i++) {
1158 if (sem_io[i] > SEMVMX) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001159 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 err = -ERANGE;
1161 goto out_free;
1162 }
1163 }
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001164 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001166 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 err = -EIDRM;
1168 goto out_free;
1169 }
1170
1171 for (i = 0; i < nsems; i++)
1172 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001173
1174 assert_spin_locked(&sma->sem_perm.lock);
1175 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 for (i = 0; i < nsems; i++)
1177 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 sma->sem_ctime = get_seconds();
1180 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001181 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 err = 0;
1183 goto out_unlock;
1184 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001185 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
1187 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001188 if (semnum < 0 || semnum >= nsems) {
1189 rcu_read_unlock();
1190 goto out_wakeup;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Rik van Riel6062a8d2013-04-30 19:15:44 -07001193 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 curr = &sma->sem_base[semnum];
1195
1196 switch (cmd) {
1197 case GETVAL:
1198 err = curr->semval;
1199 goto out_unlock;
1200 case GETPID:
1201 err = curr->sempid;
1202 goto out_unlock;
1203 case GETNCNT:
1204 err = count_semncnt(sma,semnum);
1205 goto out_unlock;
1206 case GETZCNT:
1207 err = count_semzcnt(sma,semnum);
1208 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001212 sem_unlock(sma, -1);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001213out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001214 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215out_free:
1216 if(sem_io != fast_sem_io)
1217 ipc_free(sem_io, sizeof(ushort)*nsems);
1218 return err;
1219}
1220
Pierre Peiffer016d7132008-04-29 01:00:50 -07001221static inline unsigned long
1222copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 switch(version) {
1225 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001226 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 case IPC_OLD:
1230 {
1231 struct semid_ds tbuf_old;
1232
1233 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1234 return -EFAULT;
1235
Pierre Peiffer016d7132008-04-29 01:00:50 -07001236 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1237 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1238 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 return 0;
1241 }
1242 default:
1243 return -EINVAL;
1244 }
1245}
1246
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001247/*
1248 * This function handles some semctl commands which require the rw_mutex
1249 * to be held in write mode.
1250 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1251 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001252static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001253 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
1255 struct sem_array *sma;
1256 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001257 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 struct kern_ipc_perm *ipcp;
1259
1260 if(cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001261 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001265 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1266 &semid64.sem_perm, 0);
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001267 if (IS_ERR(ipcp))
1268 return PTR_ERR(ipcp);
Steve Grubb073115d2006-04-02 17:07:33 -04001269
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001270 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001273 if (err) {
1274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 goto out_unlock;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 switch(cmd){
1279 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001280 sem_lock(sma, NULL, -1);
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001281 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001282 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001284 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001285 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1286 if (err)
1287 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 default:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001291 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001293 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001297 sem_unlock(sma, -1);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001298out_up:
1299 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return err;
1301}
1302
Al Viroe1fd1f42013-03-05 15:04:55 -05001303SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001306 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001307 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 if (semid < 0)
1310 return -EINVAL;
1311
1312 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001313 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 switch(cmd) {
1316 case IPC_INFO:
1317 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001318 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001320 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case GETALL:
1322 case GETVAL:
1323 case GETPID:
1324 case GETNCNT:
1325 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001327 return semctl_main(ns, semid, semnum, cmd, p);
1328 case SETVAL:
1329 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case IPC_RMID:
1331 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001332 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 default:
1334 return -EINVAL;
1335 }
1336}
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338/* If the task doesn't already have a undo_list, then allocate one
1339 * here. We guarantee there is only one thread using this undo list,
1340 * and current is THE ONE
1341 *
1342 * If this allocation and assignment succeeds, but later
1343 * portions of this code fail, there is no need to free the sem_undo_list.
1344 * Just let it stay associated with the task, and it'll be freed later
1345 * at exit time.
1346 *
1347 * This can block, so callers must hold no locks.
1348 */
1349static inline int get_undo_list(struct sem_undo_list **undo_listp)
1350{
1351 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 undo_list = current->sysvsem.undo_list;
1354 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001355 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (undo_list == NULL)
1357 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001358 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001360 INIT_LIST_HEAD(&undo_list->list_proc);
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 current->sysvsem.undo_list = undo_list;
1363 }
1364 *undo_listp = undo_list;
1365 return 0;
1366}
1367
Nick Pigginbf17bb72009-12-15 16:47:28 -08001368static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001370 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Nick Pigginbf17bb72009-12-15 16:47:28 -08001372 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1373 if (un->semid == semid)
1374 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001376 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
Nick Pigginbf17bb72009-12-15 16:47:28 -08001379static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1380{
1381 struct sem_undo *un;
1382
1383 assert_spin_locked(&ulp->lock);
1384
1385 un = __lookup_undo(ulp, semid);
1386 if (un) {
1387 list_del_rcu(&un->list_proc);
1388 list_add_rcu(&un->list_proc, &ulp->list_proc);
1389 }
1390 return un;
1391}
1392
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001393/**
1394 * find_alloc_undo - Lookup (and if not present create) undo array
1395 * @ns: namespace
1396 * @semid: semaphore array id
1397 *
1398 * The function looks up (and if not present creates) the undo structure.
1399 * The size of the undo structure depends on the size of the semaphore
1400 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001401 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1402 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001403 */
1404static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 struct sem_array *sma;
1407 struct sem_undo_list *ulp;
1408 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001409 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 error = get_undo_list(&ulp);
1412 if (error)
1413 return ERR_PTR(error);
1414
Manfred Spraul380af1b2008-07-25 01:48:06 -07001415 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001416 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001418 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (likely(un!=NULL))
1420 goto out;
1421
1422 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001423 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001424 sma = sem_obtain_object_check(ns, semid);
1425 if (IS_ERR(sma)) {
1426 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001427 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001428 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001431 if (!ipc_rcu_getref(sma)) {
1432 rcu_read_unlock();
1433 un = ERR_PTR(-EIDRM);
1434 goto out;
1435 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001436 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001438 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001439 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!new) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001441 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return ERR_PTR(-ENOMEM);
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Manfred Spraul380af1b2008-07-25 01:48:06 -07001445 /* step 3: Acquire the lock on semaphore array */
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001446 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001448 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 kfree(new);
1450 un = ERR_PTR(-EIDRM);
1451 goto out;
1452 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001453 spin_lock(&ulp->lock);
1454
1455 /*
1456 * step 4: check for races: did someone else allocate the undo struct?
1457 */
1458 un = lookup_undo(ulp, semid);
1459 if (un) {
1460 kfree(new);
1461 goto success;
1462 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001463 /* step 5: initialize & link new undo structure */
1464 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001465 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001466 new->semid = semid;
1467 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001468 list_add_rcu(&new->list_proc, &ulp->list_proc);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001469 assert_spin_locked(&sma->sem_perm.lock);
1470 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001471 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001472
1473success:
1474 spin_unlock(&ulp->lock);
1475 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001476 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477out:
1478 return un;
1479}
1480
Manfred Spraulc61284e2010-07-20 13:24:23 -07001481
1482/**
1483 * get_queue_result - Retrieve the result code from sem_queue
1484 * @q: Pointer to queue structure
1485 *
1486 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1487 * q->status, then we must loop until the value is replaced with the final
1488 * value: This may happen if a task is woken up by an unrelated event (e.g.
1489 * signal) and in parallel the task is woken up by another task because it got
1490 * the requested semaphores.
1491 *
1492 * The function can be called with or without holding the semaphore spinlock.
1493 */
1494static int get_queue_result(struct sem_queue *q)
1495{
1496 int error;
1497
1498 error = q->status;
1499 while (unlikely(error == IN_WAKEUP)) {
1500 cpu_relax();
1501 error = q->status;
1502 }
1503
1504 return error;
1505}
1506
1507
Heiko Carstensd5460c92009-01-14 14:14:27 +01001508SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1509 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
1511 int error = -EINVAL;
1512 struct sem_array *sma;
1513 struct sembuf fast_sops[SEMOPM_FAST];
1514 struct sembuf* sops = fast_sops, *sop;
1515 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001516 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct sem_queue queue;
1518 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001519 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001520 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001521
1522 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (nsops < 1 || semid < 0)
1525 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001526 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return -E2BIG;
1528 if(nsops > SEMOPM_FAST) {
1529 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1530 if(sops==NULL)
1531 return -ENOMEM;
1532 }
1533 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1534 error=-EFAULT;
1535 goto out_free;
1536 }
1537 if (timeout) {
1538 struct timespec _timeout;
1539 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1540 error = -EFAULT;
1541 goto out_free;
1542 }
1543 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1544 _timeout.tv_nsec >= 1000000000L) {
1545 error = -EINVAL;
1546 goto out_free;
1547 }
1548 jiffies_left = timespec_to_jiffies(&_timeout);
1549 }
1550 max = 0;
1551 for (sop = sops; sop < sops + nsops; sop++) {
1552 if (sop->sem_num >= max)
1553 max = sop->sem_num;
1554 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001555 undos = 1;
1556 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 alter = 1;
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Rik van Riel6062a8d2013-04-30 19:15:44 -07001560 INIT_LIST_HEAD(&tasks);
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001563 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001564 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (IS_ERR(un)) {
1566 error = PTR_ERR(un);
1567 goto out_free;
1568 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001569 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001571 rcu_read_lock();
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001574 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001575 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001576 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001577 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001579 }
1580
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001581 error = -EFBIG;
1582 if (max >= sma->sem_nsems) {
1583 rcu_read_unlock();
1584 goto out_wakeup;
1585 }
1586
1587 error = -EACCES;
1588 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1589 rcu_read_unlock();
1590 goto out_wakeup;
1591 }
1592
1593 error = security_sem_semop(sma, sops, nsops, alter);
1594 if (error) {
1595 rcu_read_unlock();
1596 goto out_wakeup;
1597 }
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001600 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001602 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001603 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001604 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001606 error = -EIDRM;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001607 locknum = sem_lock(sma, sops, nsops);
1608 if (un && un->semid == -1)
1609 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001610
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001611 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (error <= 0) {
1613 if (alter && error == 0)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001614 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul636c6be2009-12-15 16:47:33 -08001615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto out_unlock_free;
1617 }
1618
1619 /* We need to sleep on this operation, so we put the current
1620 * task into the pending queue and go to sleep.
1621 */
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 queue.sops = sops;
1624 queue.nsops = nsops;
1625 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001626 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Manfred Spraulb97e8202009-12-15 16:47:32 -08001629 if (nsops == 1) {
1630 struct sem *curr;
1631 curr = &sma->sem_base[sops->sem_num];
1632
1633 if (alter)
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001634 list_add_tail(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001635 else
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001636 list_add(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001637 } else {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001638 if (alter)
1639 list_add_tail(&queue.list, &sma->sem_pending);
1640 else
1641 list_add(&queue.list, &sma->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001642 sma->complex_count++;
1643 }
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 queue.status = -EINTR;
1646 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001647
1648sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 current->state = TASK_INTERRUPTIBLE;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001650 sem_unlock(sma, locknum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (timeout)
1653 jiffies_left = schedule_timeout(jiffies_left);
1654 else
1655 schedule();
1656
Manfred Spraulc61284e2010-07-20 13:24:23 -07001657 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (error != -EINTR) {
1660 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001661 * resources.
1662 * Perform a smp_mb(): User space could assume that semop()
1663 * is a memory barrier: Without the mb(), the cpu could
1664 * speculatively read in user space stale data that was
1665 * overwritten by the previous owner of the semaphore.
1666 */
1667 smp_mb();
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto out_free;
1670 }
1671
Rik van Riel6062a8d2013-04-30 19:15:44 -07001672 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001673
1674 /*
1675 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1676 */
1677 error = get_queue_result(&queue);
1678
1679 /*
1680 * Array removed? If yes, leave without sem_unlock().
1681 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001682 if (IS_ERR(sma)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto out_free;
1684 }
1685
Manfred Spraulc61284e2010-07-20 13:24:23 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001688 * If queue.status != -EINTR we are woken up by another process.
1689 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 */
Manfred Spraulc61284e2010-07-20 13:24:23 -07001691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (error != -EINTR) {
1693 goto out_unlock_free;
1694 }
1695
1696 /*
1697 * If an interrupt occurred we have to clean up the queue
1698 */
1699 if (timeout && jiffies_left == 0)
1700 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001701
1702 /*
1703 * If the wakeup was spurious, just retry
1704 */
1705 if (error == -EINTR && !signal_pending(current))
1706 goto sleep_again;
1707
Manfred Spraulb97e8202009-12-15 16:47:32 -08001708 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001711 sem_unlock(sma, locknum);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001712out_wakeup:
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001713 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714out_free:
1715 if(sops != fast_sops)
1716 kfree(sops);
1717 return error;
1718}
1719
Heiko Carstensd5460c92009-01-14 14:14:27 +01001720SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1721 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 return sys_semtimedop(semid, tsops, nsops, NULL);
1724}
1725
1726/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1727 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
1729
1730int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1731{
1732 struct sem_undo_list *undo_list;
1733 int error;
1734
1735 if (clone_flags & CLONE_SYSVSEM) {
1736 error = get_undo_list(&undo_list);
1737 if (error)
1738 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 atomic_inc(&undo_list->refcnt);
1740 tsk->sysvsem.undo_list = undo_list;
1741 } else
1742 tsk->sysvsem.undo_list = NULL;
1743
1744 return 0;
1745}
1746
1747/*
1748 * add semadj values to semaphores, free undo structures.
1749 * undo structures are not freed when semaphore arrays are destroyed
1750 * so some of them may be out of date.
1751 * IMPLEMENTATION NOTE: There is some confusion over whether the
1752 * set of adjustments that needs to be done should be done in an atomic
1753 * manner or not. That is, if we are attempting to decrement the semval
1754 * should we queue up and wait until we can do so legally?
1755 * The original implementation attempted to do this (queue and wait).
1756 * The current implementation does not do so. The POSIX standard
1757 * and SVID should be consulted to determine what behavior is mandated.
1758 */
1759void exit_sem(struct task_struct *tsk)
1760{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001761 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001763 ulp = tsk->sysvsem.undo_list;
1764 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07001766 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001768 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return;
1770
Manfred Spraul380af1b2008-07-25 01:48:06 -07001771 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001773 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001774 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001775 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Manfred Spraul380af1b2008-07-25 01:48:06 -07001777 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02001778 un = list_entry_rcu(ulp->list_proc.next,
1779 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001780 if (&un->list_proc == &ulp->list_proc)
1781 semid = -1;
1782 else
1783 semid = un->semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001784
Rik van Riel6062a8d2013-04-30 19:15:44 -07001785 if (semid == -1) {
1786 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001787 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001788 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001789
Rik van Riel6062a8d2013-04-30 19:15:44 -07001790 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001791 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001792 if (IS_ERR(sma)) {
1793 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001794 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Rik van Riel6062a8d2013-04-30 19:15:44 -07001797 sem_lock(sma, NULL, -1);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001798 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001799 if (un == NULL) {
1800 /* exit_sem raced with IPC_RMID+semget() that created
1801 * exactly the same semid. Nothing to do.
1802 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001803 sem_unlock(sma, -1);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001804 continue;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Manfred Spraul380af1b2008-07-25 01:48:06 -07001807 /* remove un from the linked lists */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001808 assert_spin_locked(&sma->sem_perm.lock);
1809 list_del(&un->list_id);
1810
Manfred Spraul380af1b2008-07-25 01:48:06 -07001811 spin_lock(&ulp->lock);
1812 list_del_rcu(&un->list_proc);
1813 spin_unlock(&ulp->lock);
1814
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001815 /* perform adjustments registered in un */
1816 for (i = 0; i < sma->sem_nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001817 struct sem * semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001818 if (un->semadj[i]) {
1819 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * Range checks of the new semaphore value,
1822 * not defined by sus:
1823 * - Some unices ignore the undo entirely
1824 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1825 * - some cap the value (e.g. FreeBSD caps
1826 * at 0, but doesn't enforce SEMVMX)
1827 *
1828 * Linux caps the semaphore value, both at 0
1829 * and at SEMVMX.
1830 *
1831 * Manfred <manfred@colorfullife.com>
1832 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001833 if (semaphore->semval < 0)
1834 semaphore->semval = 0;
1835 if (semaphore->semval > SEMVMX)
1836 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001837 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001841 INIT_LIST_HEAD(&tasks);
1842 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001843 sem_unlock(sma, -1);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001844 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001845
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001846 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001848 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
1851#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001852static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001854 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001855 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Mike Waychison19b49462005-09-06 15:17:10 -07001857 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08001858 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07001859 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001860 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001861 sma->sem_perm.mode,
1862 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001863 from_kuid_munged(user_ns, sma->sem_perm.uid),
1864 from_kgid_munged(user_ns, sma->sem_perm.gid),
1865 from_kuid_munged(user_ns, sma->sem_perm.cuid),
1866 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Mike Waychison19b49462005-09-06 15:17:10 -07001867 sma->sem_otime,
1868 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870#endif