blob: 899b598b63be30bb70f1182edb2b9b91a5e50bd9 [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 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700267}
268
269/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700270 * sem_lock_(check_) routines are called in the paths where the rw_mutex
271 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700272 *
273 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700274 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700275static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
276 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700277{
Rik van Rielc460b662013-04-30 19:15:35 -0700278 struct kern_ipc_perm *ipcp;
279 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700280
Rik van Rielc460b662013-04-30 19:15:35 -0700281 ipcp = ipc_obtain_object(&sem_ids(ns), id);
Linus Torvalds321310c2013-05-04 10:47:57 -0700282 if (IS_ERR(ipcp))
283 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800284
Rik van Riel6062a8d2013-04-30 19:15:44 -0700285 sma = container_of(ipcp, struct sem_array, sem_perm);
286 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700287
288 /* ipc_rmid() may have already freed the ID while sem_lock
289 * was spinning: verify that the structure is still valid
290 */
291 if (!ipcp->deleted)
292 return container_of(ipcp, struct sem_array, sem_perm);
293
Rik van Riel6062a8d2013-04-30 19:15:44 -0700294 sem_unlock(sma, *locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -0700295 return ERR_PTR(-EINVAL);
Nadia Derbey023a5352007-10-18 23:40:51 -0700296}
297
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700298static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
299{
300 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
301
302 if (IS_ERR(ipcp))
303 return ERR_CAST(ipcp);
304
305 return container_of(ipcp, struct sem_array, sem_perm);
306}
307
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700308static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
309 int id)
310{
311 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
312
313 if (IS_ERR(ipcp))
314 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800315
Nadia Derbey03f02c72007-10-18 23:40:51 -0700316 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700317}
318
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700319static inline void sem_lock_and_putref(struct sem_array *sma)
320{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700321 sem_lock(sma, NULL, -1);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700322 ipc_rcu_putref(sma);
323}
324
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700325static inline void sem_putref(struct sem_array *sma)
326{
Linus Torvalds73b29502013-05-03 15:22:00 -0700327 ipc_rcu_putref(sma);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700328}
329
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700330static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
331{
332 ipc_rmid(&sem_ids(ns), &s->sem_perm);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * Lockless wakeup algorithm:
337 * Without the check/retry algorithm a lockless wakeup is possible:
338 * - queue.status is initialized to -EINTR before blocking.
339 * - wakeup is performed by
340 * * unlinking the queue entry from sma->sem_pending
341 * * setting queue.status to IN_WAKEUP
342 * This is the notification for the blocked thread that a
343 * result value is imminent.
344 * * call wake_up_process
345 * * set queue.status to the final value.
346 * - the previously blocked thread checks queue.status:
347 * * if it's IN_WAKEUP, then it must wait until the value changes
348 * * if it's not -EINTR, then the operation was completed by
349 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800350 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * * otherwise it must acquire the spinlock and check what's up.
352 *
353 * The two-stage algorithm is necessary to protect against the following
354 * races:
355 * - if queue.status is set after wake_up_process, then the woken up idle
356 * thread could race forward and try (and fail) to acquire sma->lock
357 * before update_queue had a chance to set queue.status
358 * - if queue.status is written before wake_up_process and if the
359 * blocked process is woken up by a signal between writing
360 * queue.status and the wake_up_process, then the woken up
361 * process could return from semtimedop and die by calling
362 * sys_exit before wake_up_process is called. Then wake_up_process
363 * will oops, because the task structure is already invalid.
364 * (yes, this happened on s390 with sysv msg).
365 *
366 */
367#define IN_WAKEUP 1
368
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700369/**
370 * newary - Create a new semaphore set
371 * @ns: namespace
372 * @params: ptr to the structure that contains key, semflg and nsems
373 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700374 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700375 */
376
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700377static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 int id;
380 int retval;
381 struct sem_array *sma;
382 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700383 key_t key = params->key;
384 int nsems = params->u.nsems;
385 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800386 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (!nsems)
389 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700390 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -ENOSPC;
392
393 size = sizeof (*sma) + nsems * sizeof (struct sem);
394 sma = ipc_rcu_alloc(size);
395 if (!sma) {
396 return -ENOMEM;
397 }
398 memset (sma, 0, size);
399
400 sma->sem_perm.mode = (semflg & S_IRWXUGO);
401 sma->sem_perm.key = key;
402
403 sma->sem_perm.security = NULL;
404 retval = security_sem_alloc(sma);
405 if (retval) {
406 ipc_rcu_putref(sma);
407 return retval;
408 }
409
Kirill Korotaeve3893532006-10-02 02:18:22 -0700410 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700411 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 security_sem_free(sma);
413 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700414 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700416 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800419
Rik van Riel6062a8d2013-04-30 19:15:44 -0700420 for (i = 0; i < nsems; i++) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800421 INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700422 spin_lock_init(&sma->sem_base[i].lock);
423 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800424
425 sma->complex_count = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700426 INIT_LIST_HEAD(&sma->sem_pending);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700427 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 sma->sem_nsems = nsems;
429 sma->sem_ctime = get_seconds();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700430 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700431 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700433 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700436
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700437/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700438 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700439 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700440static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700442 struct sem_array *sma;
443
444 sma = container_of(ipcp, struct sem_array, sem_perm);
445 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700446}
447
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700448/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700449 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700450 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700451static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
452 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700453{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700454 struct sem_array *sma;
455
456 sma = container_of(ipcp, struct sem_array, sem_perm);
457 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700458 return -EINVAL;
459
460 return 0;
461}
462
Heiko Carstensd5460c92009-01-14 14:14:27 +0100463SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700464{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700465 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700466 struct ipc_ops sem_ops;
467 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Kirill Korotaeve3893532006-10-02 02:18:22 -0700469 ns = current->nsproxy->ipc_ns;
470
471 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700473
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700474 sem_ops.getnew = newary;
475 sem_ops.associate = sem_security;
476 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700477
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700478 sem_params.key = key;
479 sem_params.flg = semflg;
480 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700481
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700482 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
486 * Determine whether a sequence of semaphore operations would succeed
487 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
488 */
489
490static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
491 int nsops, struct sem_undo *un, int pid)
492{
493 int result, sem_op;
494 struct sembuf *sop;
495 struct sem * curr;
496
497 for (sop = sops; sop < sops + nsops; sop++) {
498 curr = sma->sem_base + sop->sem_num;
499 sem_op = sop->sem_op;
500 result = curr->semval;
501
502 if (!sem_op && result)
503 goto would_block;
504
505 result += sem_op;
506 if (result < 0)
507 goto would_block;
508 if (result > SEMVMX)
509 goto out_of_range;
510 if (sop->sem_flg & SEM_UNDO) {
511 int undo = un->semadj[sop->sem_num] - sem_op;
512 /*
513 * Exceeding the undo range is an error.
514 */
515 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
516 goto out_of_range;
517 }
518 curr->semval = result;
519 }
520
521 sop--;
522 while (sop >= sops) {
523 sma->sem_base[sop->sem_num].sempid = pid;
524 if (sop->sem_flg & SEM_UNDO)
525 un->semadj[sop->sem_num] -= sop->sem_op;
526 sop--;
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 0;
530
531out_of_range:
532 result = -ERANGE;
533 goto undo;
534
535would_block:
536 if (sop->sem_flg & IPC_NOWAIT)
537 result = -EAGAIN;
538 else
539 result = 1;
540
541undo:
542 sop--;
543 while (sop >= sops) {
544 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
545 sop--;
546 }
547
548 return result;
549}
550
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700551/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
552 * @q: queue entry that must be signaled
553 * @error: Error value for the signal
554 *
555 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800556 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700557static void wake_up_sem_queue_prepare(struct list_head *pt,
558 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800559{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700560 if (list_empty(pt)) {
561 /*
562 * Hold preempt off so that we don't get preempted and have the
563 * wakee busy-wait until we're scheduled back on.
564 */
565 preempt_disable();
566 }
Nick Piggind4212092009-12-15 16:47:30 -0800567 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700568 q->pid = error;
569
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700570 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700571}
572
573/**
574 * wake_up_sem_queue_do(pt) - do the actual wake-up
575 * @pt: list of tasks to be woken up
576 *
577 * Do the actual wake-up.
578 * The function is called without any locks held, thus the semaphore array
579 * could be destroyed already and the tasks can disappear as soon as the
580 * status is set to the actual return code.
581 */
582static void wake_up_sem_queue_do(struct list_head *pt)
583{
584 struct sem_queue *q, *t;
585 int did_something;
586
587 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700588 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700589 wake_up_process(q->sleeper);
590 /* q can disappear immediately after writing q->status. */
591 smp_wmb();
592 q->status = q->pid;
593 }
594 if (did_something)
595 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800596}
597
Manfred Spraulb97e8202009-12-15 16:47:32 -0800598static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
599{
600 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700601 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800602 sma->complex_count--;
603}
604
Manfred Spraulfd5db422010-05-26 14:43:40 -0700605/** check_restart(sma, q)
606 * @sma: semaphore array
607 * @q: the operation that just completed
608 *
609 * update_queue is O(N^2) when it restarts scanning the whole queue of
610 * waiting operations. Therefore this function checks if the restart is
611 * really necessary. It is called after a previously waiting operation
612 * was completed.
613 */
614static int check_restart(struct sem_array *sma, struct sem_queue *q)
615{
616 struct sem *curr;
617 struct sem_queue *h;
618
619 /* if the operation didn't modify the array, then no restart */
620 if (q->alter == 0)
621 return 0;
622
623 /* pending complex operations are too difficult to analyse */
624 if (sma->complex_count)
625 return 1;
626
627 /* we were a sleeping complex operation. Too difficult */
628 if (q->nsops > 1)
629 return 1;
630
631 curr = sma->sem_base + q->sops[0].sem_num;
632
633 /* No-one waits on this queue */
634 if (list_empty(&curr->sem_pending))
635 return 0;
636
637 /* the new semaphore value */
638 if (curr->semval) {
639 /* It is impossible that someone waits for the new value:
640 * - q is a previously sleeping simple operation that
641 * altered the array. It must be a decrement, because
642 * simple increments never sleep.
643 * - The value is not 0, thus wait-for-zero won't proceed.
644 * - If there are older (higher priority) decrements
645 * in the queue, then they have observed the original
646 * semval value and couldn't proceed. The operation
647 * decremented to value - thus they won't proceed either.
648 */
649 BUG_ON(q->sops[0].sem_op >= 0);
650 return 0;
651 }
652 /*
653 * semval is 0. Check if there are wait-for-zero semops.
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700654 * They must be the first entries in the per-semaphore queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700655 */
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700656 h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700657 BUG_ON(h->nsops != 1);
658 BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
659
660 /* Yes, there is a wait-for-zero semop. Restart */
661 if (h->sops[0].sem_op == 0)
662 return 1;
663
664 /* Again - no-one is waiting for the new value. */
665 return 0;
666}
667
Manfred Spraul636c6be2009-12-15 16:47:33 -0800668
669/**
670 * update_queue(sma, semnum): Look for tasks that can be completed.
671 * @sma: semaphore array.
672 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700673 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800674 *
675 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700676 * was modified. If multiple semaphores were modified, update_queue must
677 * be called with semnum = -1, as well as with the number of each modified
678 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700679 * The tasks that must be woken up are added to @pt. The return code
680 * is stored in q->pid.
681 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700683static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800685 struct sem_queue *q;
686 struct list_head *walk;
687 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700688 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800689
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700690 if (semnum == -1)
Manfred Spraul636c6be2009-12-15 16:47:33 -0800691 pending_list = &sma->sem_pending;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700692 else
Manfred Spraul636c6be2009-12-15 16:47:33 -0800693 pending_list = &sma->sem_base[semnum].sem_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Nick Piggin9cad2002009-12-15 16:47:29 -0800695again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800696 walk = pending_list->next;
697 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700698 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800699
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700700 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800701 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800702
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800703 /* If we are scanning the single sop, per-semaphore list of
704 * one semaphore and that semaphore is 0, then it is not
705 * necessary to scan the "alter" entries: simple increments
706 * that affect only one entry succeed immediately and cannot
707 * be in the per semaphore pending queue, and decrements
708 * cannot be successful if the value is already 0.
709 */
710 if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
711 q->alter)
712 break;
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 error = try_atomic_semop(sma, q->sops, q->nsops,
715 q->undo, q->pid);
716
717 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800718 if (error > 0)
719 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700720
Manfred Spraulb97e8202009-12-15 16:47:32 -0800721 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700722
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700723 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700724 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700725 } else {
726 semop_completed = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700727 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700728 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700729
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700730 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700731 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800732 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700734 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700737/**
738 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700739 * @sma: semaphore array
740 * @sops: operations that were performed
741 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700742 * @otime: force setting otime
743 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700744 *
745 * do_smart_update() does the required called to update_queue, based on the
746 * actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700747 * Note that the function does not do the actual wake-up: the caller is
748 * responsible for calling wake_up_sem_queue_do(@pt).
749 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700750 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700751static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
752 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700753{
754 int i;
755
756 if (sma->complex_count || sops == NULL) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700757 if (update_queue(sma, -1, pt))
758 otime = 1;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700759 }
760
761 if (!sops) {
762 /* No semops; something special is going on. */
763 for (i = 0; i < sma->sem_nsems; i++) {
764 if (update_queue(sma, i, pt))
765 otime = 1;
766 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700767 goto done;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700768 }
769
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700770 /* Check the semaphores that were modified. */
Manfred Spraulfd5db422010-05-26 14:43:40 -0700771 for (i = 0; i < nsops; i++) {
772 if (sops[i].sem_op > 0 ||
773 (sops[i].sem_op < 0 &&
774 sma->sem_base[sops[i].sem_num].semval == 0))
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700775 if (update_queue(sma, sops[i].sem_num, pt))
776 otime = 1;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700777 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700778done:
779 if (otime)
780 sma->sem_otime = get_seconds();
Manfred Spraulfd5db422010-05-26 14:43:40 -0700781}
782
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/* The following counts are associated to each semaphore:
785 * semncnt number of tasks waiting on semval being nonzero
786 * semzcnt number of tasks waiting on semval being zero
787 * This model assumes that a task waits on exactly one semaphore.
788 * Since semaphore operations are to be performed atomically, tasks actually
789 * wait on a whole sequence of semaphores simultaneously.
790 * The counts we return here are a rough approximation, but still
791 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
792 */
793static int count_semncnt (struct sem_array * sma, ushort semnum)
794{
795 int semncnt;
796 struct sem_queue * q;
797
798 semncnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700799 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct sembuf * sops = q->sops;
801 int nsops = q->nsops;
802 int i;
803 for (i = 0; i < nsops; i++)
804 if (sops[i].sem_num == semnum
805 && (sops[i].sem_op < 0)
806 && !(sops[i].sem_flg & IPC_NOWAIT))
807 semncnt++;
808 }
809 return semncnt;
810}
Manfred Spraula1193f82008-07-25 01:48:06 -0700811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812static int count_semzcnt (struct sem_array * sma, ushort semnum)
813{
814 int semzcnt;
815 struct sem_queue * q;
816
817 semzcnt = 0;
Manfred Spraula1193f82008-07-25 01:48:06 -0700818 list_for_each_entry(q, &sma->sem_pending, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct sembuf * sops = q->sops;
820 int nsops = q->nsops;
821 int i;
822 for (i = 0; i < nsops; i++)
823 if (sops[i].sem_num == semnum
824 && (sops[i].sem_op == 0)
825 && !(sops[i].sem_flg & IPC_NOWAIT))
826 semzcnt++;
827 }
828 return semzcnt;
829}
830
Nadia Derbey3e148c72007-10-18 23:40:54 -0700831/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
832 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
833 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800835static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Manfred Spraul380af1b2008-07-25 01:48:06 -0700837 struct sem_undo *un, *tu;
838 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800839 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700840 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700841 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Manfred Spraul380af1b2008-07-25 01:48:06 -0700843 /* Free the existing undo structures for this semaphore set. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700844 assert_spin_locked(&sma->sem_perm.lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700845 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
846 list_del(&un->list_id);
847 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -0700849 list_del_rcu(&un->list_proc);
850 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +0800851 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700855 INIT_LIST_HEAD(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -0700856 list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -0800857 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700858 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700860 for (i = 0; i < sma->sem_nsems; i++) {
861 struct sem *sem = sma->sem_base + i;
862 list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
863 unlink_queue(sma, q);
864 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
865 }
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700868 /* Remove the semaphore set from the IDR */
869 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700870 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700871 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700873 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700874 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 security_sem_free(sma);
876 ipc_rcu_putref(sma);
877}
878
879static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
880{
881 switch(version) {
882 case IPC_64:
883 return copy_to_user(buf, in, sizeof(*in));
884 case IPC_OLD:
885 {
886 struct semid_ds out;
887
Dan Rosenberg982f7c22010-09-30 15:15:31 -0700888 memset(&out, 0, sizeof(out));
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
891
892 out.sem_otime = in->sem_otime;
893 out.sem_ctime = in->sem_ctime;
894 out.sem_nsems = in->sem_nsems;
895
896 return copy_to_user(buf, &out, sizeof(out));
897 }
898 default:
899 return -EINVAL;
900 }
901}
902
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800903static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -0500904 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Amerigo Wange5cc9c72009-12-15 16:47:35 -0800906 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct sem_array *sma;
908
909 switch(cmd) {
910 case IPC_INFO:
911 case SEM_INFO:
912 {
913 struct seminfo seminfo;
914 int max_id;
915
916 err = security_sem_semctl(NULL, cmd);
917 if (err)
918 return err;
919
920 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700921 seminfo.semmni = ns->sc_semmni;
922 seminfo.semmns = ns->sc_semmns;
923 seminfo.semmsl = ns->sc_semmsl;
924 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 seminfo.semvmx = SEMVMX;
926 seminfo.semmnu = SEMMNU;
927 seminfo.semmap = SEMMAP;
928 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700929 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700931 seminfo.semusz = sem_ids(ns).in_use;
932 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else {
934 seminfo.semusz = SEMUSZ;
935 seminfo.semaem = SEMAEM;
936 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700937 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700938 up_read(&sem_ids(ns).rw_mutex);
Al Viroe1fd1f42013-03-05 15:04:55 -0500939 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EFAULT;
941 return (max_id < 0) ? 0: max_id;
942 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800943 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case SEM_STAT:
945 {
946 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700947 int id = 0;
948
949 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Linus Torvalds941b0302013-05-04 11:04:29 -0700951 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800952 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700953 sma = sem_obtain_object(ns, semid);
954 if (IS_ERR(sma)) {
955 err = PTR_ERR(sma);
956 goto out_unlock;
957 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800958 id = sma->sem_perm.id;
959 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700960 sma = sem_obtain_object_check(ns, semid);
961 if (IS_ERR(sma)) {
962 err = PTR_ERR(sma);
963 goto out_unlock;
964 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700968 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 goto out_unlock;
970
971 err = security_sem_semctl(sma, cmd);
972 if (err)
973 goto out_unlock;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
976 tbuf.sem_otime = sma->sem_otime;
977 tbuf.sem_ctime = sma->sem_ctime;
978 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700979 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -0500980 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return -EFAULT;
982 return id;
983 }
984 default:
985 return -EINVAL;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return err;
990}
991
Al Viroe1fd1f42013-03-05 15:04:55 -0500992static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
993 unsigned long arg)
994{
995 struct sem_undo *un;
996 struct sem_array *sma;
997 struct sem* curr;
998 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -0500999 struct list_head tasks;
1000 int val;
1001#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1002 /* big-endian 64bit */
1003 val = arg >> 32;
1004#else
1005 /* 32bit or little-endian 64bit */
1006 val = arg;
1007#endif
1008
Rik van Riel6062a8d2013-04-30 19:15:44 -07001009 if (val > SEMVMX || val < 0)
1010 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001011
1012 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001013
Rik van Riel6062a8d2013-04-30 19:15:44 -07001014 rcu_read_lock();
1015 sma = sem_obtain_object_check(ns, semid);
1016 if (IS_ERR(sma)) {
1017 rcu_read_unlock();
1018 return PTR_ERR(sma);
1019 }
1020
1021 if (semnum < 0 || semnum >= sma->sem_nsems) {
1022 rcu_read_unlock();
1023 return -EINVAL;
1024 }
1025
1026
1027 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1028 rcu_read_unlock();
1029 return -EACCES;
1030 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001031
1032 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001033 if (err) {
1034 rcu_read_unlock();
1035 return -EACCES;
1036 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001037
Rik van Riel6062a8d2013-04-30 19:15:44 -07001038 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001039
1040 curr = &sma->sem_base[semnum];
1041
Al Viroe1fd1f42013-03-05 15:04:55 -05001042 assert_spin_locked(&sma->sem_perm.lock);
1043 list_for_each_entry(un, &sma->list_id, list_id)
1044 un->semadj[semnum] = 0;
1045
1046 curr->semval = val;
1047 curr->sempid = task_tgid_vnr(current);
1048 sma->sem_ctime = get_seconds();
1049 /* maybe some queued-up processes were waiting for this */
1050 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001051 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001052 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001053 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001054 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001055}
1056
Kirill Korotaeve3893532006-10-02 02:18:22 -07001057static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001058 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 struct sem_array *sma;
1061 struct sem* curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001062 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ushort fast_sem_io[SEMMSL_FAST];
1064 ushort* sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001065 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001067 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001068
1069 rcu_read_lock();
1070 sma = sem_obtain_object_check(ns, semid);
1071 if (IS_ERR(sma)) {
1072 rcu_read_unlock();
1073 return PTR_ERR(sma);
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 nsems = sma->sem_nsems;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001079 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1080 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001083 if (err)
1084 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 err = -EACCES;
1087 switch (cmd) {
1088 case GETALL:
1089 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001090 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int i;
1092
Al Viroce857222013-05-03 00:30:49 +01001093 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if(nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001095 if (!ipc_rcu_getref(sma)) {
1096 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001097 rcu_read_unlock();
Al Viroce857222013-05-03 00:30:49 +01001098 err = -EIDRM;
1099 goto out_free;
1100 }
1101 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001102 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1104 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001105 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -ENOMEM;
1107 }
1108
Linus Torvalds4091fd92013-05-04 10:13:40 -07001109 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001110 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001112 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001113 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 err = -EIDRM;
1115 goto out_free;
1116 }
Al Viroce857222013-05-03 00:30:49 +01001117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 for (i = 0; i < sma->sem_nsems; i++)
1119 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001120 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001121 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 err = 0;
1123 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1124 err = -EFAULT;
1125 goto out_free;
1126 }
1127 case SETALL:
1128 {
1129 int i;
1130 struct sem_undo *un;
1131
Rik van Riel6062a8d2013-04-30 19:15:44 -07001132 if (!ipc_rcu_getref(sma)) {
1133 rcu_read_unlock();
1134 return -EIDRM;
1135 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001136 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 if(nsems > SEMMSL_FAST) {
1139 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1140 if(sem_io == NULL) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001141 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return -ENOMEM;
1143 }
1144 }
1145
Al Viroe1fd1f42013-03-05 15:04:55 -05001146 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001147 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 err = -EFAULT;
1149 goto out_free;
1150 }
1151
1152 for (i = 0; i < nsems; i++) {
1153 if (sem_io[i] > SEMVMX) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001154 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 err = -ERANGE;
1156 goto out_free;
1157 }
1158 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001159 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001160 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001162 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001163 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 err = -EIDRM;
1165 goto out_free;
1166 }
1167
1168 for (i = 0; i < nsems; i++)
1169 sma->sem_base[i].semval = sem_io[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001170
1171 assert_spin_locked(&sma->sem_perm.lock);
1172 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 for (i = 0; i < nsems; i++)
1174 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 sma->sem_ctime = get_seconds();
1177 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001178 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 err = 0;
1180 goto out_unlock;
1181 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001182 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001185 if (semnum < 0 || semnum >= nsems)
1186 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Rik van Riel6062a8d2013-04-30 19:15:44 -07001188 sem_lock(sma, NULL, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 curr = &sma->sem_base[semnum];
1190
1191 switch (cmd) {
1192 case GETVAL:
1193 err = curr->semval;
1194 goto out_unlock;
1195 case GETPID:
1196 err = curr->sempid;
1197 goto out_unlock;
1198 case GETNCNT:
1199 err = count_semncnt(sma,semnum);
1200 goto out_unlock;
1201 case GETZCNT:
1202 err = count_semzcnt(sma,semnum);
1203 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001207 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001208out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001209 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001210 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211out_free:
1212 if(sem_io != fast_sem_io)
1213 ipc_free(sem_io, sizeof(ushort)*nsems);
1214 return err;
1215}
1216
Pierre Peiffer016d7132008-04-29 01:00:50 -07001217static inline unsigned long
1218copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 switch(version) {
1221 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001222 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case IPC_OLD:
1226 {
1227 struct semid_ds tbuf_old;
1228
1229 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1230 return -EFAULT;
1231
Pierre Peiffer016d7132008-04-29 01:00:50 -07001232 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1233 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1234 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 return 0;
1237 }
1238 default:
1239 return -EINVAL;
1240 }
1241}
1242
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001243/*
1244 * This function handles some semctl commands which require the rw_mutex
1245 * to be held in write mode.
1246 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1247 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001248static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001249 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct sem_array *sma;
1252 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001253 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 struct kern_ipc_perm *ipcp;
1255
1256 if(cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001257 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001261 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1262 &semid64.sem_perm, 0);
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001263 if (IS_ERR(ipcp))
1264 return PTR_ERR(ipcp);
Steve Grubb073115d2006-04-02 17:07:33 -04001265
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001266 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001269 if (err) {
1270 rcu_read_unlock();
Linus Torvaldsfbfd1d22013-05-04 10:25:11 -07001271 goto out_up;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 switch(cmd){
1275 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001276 sem_lock(sma, NULL, -1);
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001277 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001278 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001280 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001281 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1282 if (err)
1283 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286 default:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 err = -EINVAL;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001289 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001293 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001294 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001295out_up:
1296 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return err;
1298}
1299
Al Viroe1fd1f42013-03-05 15:04:55 -05001300SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001303 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001304 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (semid < 0)
1307 return -EINVAL;
1308
1309 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001310 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 switch(cmd) {
1313 case IPC_INFO:
1314 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001315 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001317 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case GETALL:
1319 case GETVAL:
1320 case GETPID:
1321 case GETNCNT:
1322 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001324 return semctl_main(ns, semid, semnum, cmd, p);
1325 case SETVAL:
1326 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case IPC_RMID:
1328 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001329 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 default:
1331 return -EINVAL;
1332 }
1333}
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335/* If the task doesn't already have a undo_list, then allocate one
1336 * here. We guarantee there is only one thread using this undo list,
1337 * and current is THE ONE
1338 *
1339 * If this allocation and assignment succeeds, but later
1340 * portions of this code fail, there is no need to free the sem_undo_list.
1341 * Just let it stay associated with the task, and it'll be freed later
1342 * at exit time.
1343 *
1344 * This can block, so callers must hold no locks.
1345 */
1346static inline int get_undo_list(struct sem_undo_list **undo_listp)
1347{
1348 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 undo_list = current->sysvsem.undo_list;
1351 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001352 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (undo_list == NULL)
1354 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001355 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001357 INIT_LIST_HEAD(&undo_list->list_proc);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 current->sysvsem.undo_list = undo_list;
1360 }
1361 *undo_listp = undo_list;
1362 return 0;
1363}
1364
Nick Pigginbf17bb72009-12-15 16:47:28 -08001365static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001367 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Nick Pigginbf17bb72009-12-15 16:47:28 -08001369 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1370 if (un->semid == semid)
1371 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001373 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Nick Pigginbf17bb72009-12-15 16:47:28 -08001376static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1377{
1378 struct sem_undo *un;
1379
1380 assert_spin_locked(&ulp->lock);
1381
1382 un = __lookup_undo(ulp, semid);
1383 if (un) {
1384 list_del_rcu(&un->list_proc);
1385 list_add_rcu(&un->list_proc, &ulp->list_proc);
1386 }
1387 return un;
1388}
1389
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001390/**
1391 * find_alloc_undo - Lookup (and if not present create) undo array
1392 * @ns: namespace
1393 * @semid: semaphore array id
1394 *
1395 * The function looks up (and if not present creates) the undo structure.
1396 * The size of the undo structure depends on the size of the semaphore
1397 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001398 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1399 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001400 */
1401static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 struct sem_array *sma;
1404 struct sem_undo_list *ulp;
1405 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001406 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 error = get_undo_list(&ulp);
1409 if (error)
1410 return ERR_PTR(error);
1411
Manfred Spraul380af1b2008-07-25 01:48:06 -07001412 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001413 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001415 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (likely(un!=NULL))
1417 goto out;
1418
1419 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001420 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001421 sma = sem_obtain_object_check(ns, semid);
1422 if (IS_ERR(sma)) {
1423 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001424 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001425 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001428 if (!ipc_rcu_getref(sma)) {
1429 rcu_read_unlock();
1430 un = ERR_PTR(-EIDRM);
1431 goto out;
1432 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001435 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001436 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (!new) {
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001438 sem_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 return ERR_PTR(-ENOMEM);
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Manfred Spraul380af1b2008-07-25 01:48:06 -07001442 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001443 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001444 sem_lock_and_putref(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (sma->sem_perm.deleted) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001446 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001447 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 kfree(new);
1449 un = ERR_PTR(-EIDRM);
1450 goto out;
1451 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001452 spin_lock(&ulp->lock);
1453
1454 /*
1455 * step 4: check for races: did someone else allocate the undo struct?
1456 */
1457 un = lookup_undo(ulp, semid);
1458 if (un) {
1459 kfree(new);
1460 goto success;
1461 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001462 /* step 5: initialize & link new undo structure */
1463 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001464 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001465 new->semid = semid;
1466 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001467 list_add_rcu(&new->list_proc, &ulp->list_proc);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001468 assert_spin_locked(&sma->sem_perm.lock);
1469 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001470 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001471
1472success:
1473 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001474 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475out:
1476 return un;
1477}
1478
Manfred Spraulc61284e2010-07-20 13:24:23 -07001479
1480/**
1481 * get_queue_result - Retrieve the result code from sem_queue
1482 * @q: Pointer to queue structure
1483 *
1484 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1485 * q->status, then we must loop until the value is replaced with the final
1486 * value: This may happen if a task is woken up by an unrelated event (e.g.
1487 * signal) and in parallel the task is woken up by another task because it got
1488 * the requested semaphores.
1489 *
1490 * The function can be called with or without holding the semaphore spinlock.
1491 */
1492static int get_queue_result(struct sem_queue *q)
1493{
1494 int error;
1495
1496 error = q->status;
1497 while (unlikely(error == IN_WAKEUP)) {
1498 cpu_relax();
1499 error = q->status;
1500 }
1501
1502 return error;
1503}
1504
1505
Heiko Carstensd5460c92009-01-14 14:14:27 +01001506SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1507 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 int error = -EINVAL;
1510 struct sem_array *sma;
1511 struct sembuf fast_sops[SEMOPM_FAST];
1512 struct sembuf* sops = fast_sops, *sop;
1513 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001514 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct sem_queue queue;
1516 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001517 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001518 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001519
1520 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (nsops < 1 || semid < 0)
1523 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001524 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return -E2BIG;
1526 if(nsops > SEMOPM_FAST) {
1527 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1528 if(sops==NULL)
1529 return -ENOMEM;
1530 }
1531 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1532 error=-EFAULT;
1533 goto out_free;
1534 }
1535 if (timeout) {
1536 struct timespec _timeout;
1537 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1538 error = -EFAULT;
1539 goto out_free;
1540 }
1541 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1542 _timeout.tv_nsec >= 1000000000L) {
1543 error = -EINVAL;
1544 goto out_free;
1545 }
1546 jiffies_left = timespec_to_jiffies(&_timeout);
1547 }
1548 max = 0;
1549 for (sop = sops; sop < sops + nsops; sop++) {
1550 if (sop->sem_num >= max)
1551 max = sop->sem_num;
1552 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001553 undos = 1;
1554 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 alter = 1;
1556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Rik van Riel6062a8d2013-04-30 19:15:44 -07001558 INIT_LIST_HEAD(&tasks);
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001561 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001562 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (IS_ERR(un)) {
1564 error = PTR_ERR(un);
1565 goto out_free;
1566 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001567 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001569 rcu_read_lock();
1570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001572 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001573 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001574 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001575 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001577 }
1578
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001579 error = -EFBIG;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001580 if (max >= sma->sem_nsems)
1581 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001582
1583 error = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001584 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1585 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001586
1587 error = security_sem_semop(sma, sops, nsops, alter);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001588 if (error)
1589 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001592 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001594 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001595 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001596 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001598 error = -EIDRM;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001599 locknum = sem_lock(sma, sops, nsops);
1600 if (un && un->semid == -1)
1601 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001602
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001603 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (error <= 0) {
1605 if (alter && error == 0)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001606 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul636c6be2009-12-15 16:47:33 -08001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 goto out_unlock_free;
1609 }
1610
1611 /* We need to sleep on this operation, so we put the current
1612 * task into the pending queue and go to sleep.
1613 */
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 queue.sops = sops;
1616 queue.nsops = nsops;
1617 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001618 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 queue.alter = alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Manfred Spraulb97e8202009-12-15 16:47:32 -08001621 if (nsops == 1) {
1622 struct sem *curr;
1623 curr = &sma->sem_base[sops->sem_num];
1624
1625 if (alter)
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001626 list_add_tail(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001627 else
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001628 list_add(&queue.list, &curr->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001629 } else {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001630 if (alter)
1631 list_add_tail(&queue.list, &sma->sem_pending);
1632 else
1633 list_add(&queue.list, &sma->sem_pending);
Manfred Spraulb97e8202009-12-15 16:47:32 -08001634 sma->complex_count++;
1635 }
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 queue.status = -EINTR;
1638 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001639
1640sleep_again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 current->state = TASK_INTERRUPTIBLE;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001642 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001643 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (timeout)
1646 jiffies_left = schedule_timeout(jiffies_left);
1647 else
1648 schedule();
1649
Manfred Spraulc61284e2010-07-20 13:24:23 -07001650 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (error != -EINTR) {
1653 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001654 * resources.
1655 * Perform a smp_mb(): User space could assume that semop()
1656 * is a memory barrier: Without the mb(), the cpu could
1657 * speculatively read in user space stale data that was
1658 * overwritten by the previous owner of the semaphore.
1659 */
1660 smp_mb();
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto out_free;
1663 }
1664
Linus Torvalds321310c2013-05-04 10:47:57 -07001665 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001666 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001667
1668 /*
1669 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1670 */
1671 error = get_queue_result(&queue);
1672
1673 /*
1674 * Array removed? If yes, leave without sem_unlock().
1675 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001676 if (IS_ERR(sma)) {
Linus Torvalds321310c2013-05-04 10:47:57 -07001677 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 goto out_free;
1679 }
1680
Manfred Spraulc61284e2010-07-20 13:24:23 -07001681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001683 * If queue.status != -EINTR we are woken up by another process.
1684 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
Manfred Spraulc61284e2010-07-20 13:24:23 -07001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (error != -EINTR) {
1688 goto out_unlock_free;
1689 }
1690
1691 /*
1692 * If an interrupt occurred we have to clean up the queue
1693 */
1694 if (timeout && jiffies_left == 0)
1695 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001696
1697 /*
1698 * If the wakeup was spurious, just retry
1699 */
1700 if (error == -EINTR && !signal_pending(current))
1701 goto sleep_again;
1702
Manfred Spraulb97e8202009-12-15 16:47:32 -08001703 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001706 sem_unlock(sma, locknum);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001707out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001708 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001709 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710out_free:
1711 if(sops != fast_sops)
1712 kfree(sops);
1713 return error;
1714}
1715
Heiko Carstensd5460c92009-01-14 14:14:27 +01001716SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1717 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 return sys_semtimedop(semid, tsops, nsops, NULL);
1720}
1721
1722/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1723 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
1725
1726int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1727{
1728 struct sem_undo_list *undo_list;
1729 int error;
1730
1731 if (clone_flags & CLONE_SYSVSEM) {
1732 error = get_undo_list(&undo_list);
1733 if (error)
1734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 atomic_inc(&undo_list->refcnt);
1736 tsk->sysvsem.undo_list = undo_list;
1737 } else
1738 tsk->sysvsem.undo_list = NULL;
1739
1740 return 0;
1741}
1742
1743/*
1744 * add semadj values to semaphores, free undo structures.
1745 * undo structures are not freed when semaphore arrays are destroyed
1746 * so some of them may be out of date.
1747 * IMPLEMENTATION NOTE: There is some confusion over whether the
1748 * set of adjustments that needs to be done should be done in an atomic
1749 * manner or not. That is, if we are attempting to decrement the semval
1750 * should we queue up and wait until we can do so legally?
1751 * The original implementation attempted to do this (queue and wait).
1752 * The current implementation does not do so. The POSIX standard
1753 * and SVID should be consulted to determine what behavior is mandated.
1754 */
1755void exit_sem(struct task_struct *tsk)
1756{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001757 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001759 ulp = tsk->sysvsem.undo_list;
1760 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07001762 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001764 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 return;
1766
Manfred Spraul380af1b2008-07-25 01:48:06 -07001767 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001769 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001770 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001771 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Manfred Spraul380af1b2008-07-25 01:48:06 -07001773 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02001774 un = list_entry_rcu(ulp->list_proc.next,
1775 struct sem_undo, list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001776 if (&un->list_proc == &ulp->list_proc)
1777 semid = -1;
1778 else
1779 semid = un->semid;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001780
Rik van Riel6062a8d2013-04-30 19:15:44 -07001781 if (semid == -1) {
1782 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001783 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001784 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001785
Rik van Riel6062a8d2013-04-30 19:15:44 -07001786 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001787 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001788 if (IS_ERR(sma)) {
1789 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001790 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Rik van Riel6062a8d2013-04-30 19:15:44 -07001793 sem_lock(sma, NULL, -1);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001794 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001795 if (un == NULL) {
1796 /* exit_sem raced with IPC_RMID+semget() that created
1797 * exactly the same semid. Nothing to do.
1798 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001799 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001800 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07001801 continue;
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Manfred Spraul380af1b2008-07-25 01:48:06 -07001804 /* remove un from the linked lists */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001805 assert_spin_locked(&sma->sem_perm.lock);
1806 list_del(&un->list_id);
1807
Manfred Spraul380af1b2008-07-25 01:48:06 -07001808 spin_lock(&ulp->lock);
1809 list_del_rcu(&un->list_proc);
1810 spin_unlock(&ulp->lock);
1811
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001812 /* perform adjustments registered in un */
1813 for (i = 0; i < sma->sem_nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001814 struct sem * semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001815 if (un->semadj[i]) {
1816 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 /*
1818 * Range checks of the new semaphore value,
1819 * not defined by sus:
1820 * - Some unices ignore the undo entirely
1821 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1822 * - some cap the value (e.g. FreeBSD caps
1823 * at 0, but doesn't enforce SEMVMX)
1824 *
1825 * Linux caps the semaphore value, both at 0
1826 * and at SEMVMX.
1827 *
1828 * Manfred <manfred@colorfullife.com>
1829 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001830 if (semaphore->semval < 0)
1831 semaphore->semval = 0;
1832 if (semaphore->semval > SEMVMX)
1833 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001834 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001838 INIT_LIST_HEAD(&tasks);
1839 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001840 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001841 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001842 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001843
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001844 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001846 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001850static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001852 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07001853 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Mike Waychison19b49462005-09-06 15:17:10 -07001855 return seq_printf(s,
Manfred Spraulb97e8202009-12-15 16:47:32 -08001856 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
Mike Waychison19b49462005-09-06 15:17:10 -07001857 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001858 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001859 sma->sem_perm.mode,
1860 sma->sem_nsems,
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001861 from_kuid_munged(user_ns, sma->sem_perm.uid),
1862 from_kgid_munged(user_ns, sma->sem_perm.gid),
1863 from_kuid_munged(user_ns, sma->sem_perm.cuid),
1864 from_kgid_munged(user_ns, sma->sem_perm.cgid),
Mike Waychison19b49462005-09-06 15:17:10 -07001865 sma->sem_otime,
1866 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868#endif