blob: 08da8648925d87531187277d51abaf484c3f4885 [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 *
6 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7 * This code underwent a massive rewrite in order to solve some problems
8 * with the original code. In particular the original code failed to
9 * wake up processes that were waiting for semval to go to 0 if the
10 * value went to 0 and was then incremented rapidly enough. In solving
11 * this problem I have also modified the implementation so that it
12 * processes pending operations in a FIFO manner, thus give a guarantee
13 * that processes waiting for a lock on the semaphore won't starve
14 * unless another locking process fails to unlock.
15 * In addition the following two changes in behavior have been introduced:
16 * - The original implementation of semop returned the value
17 * last semaphore element examined on success. This does not
18 * match the manual page specifications, and effectively
19 * allows the user to read the semaphore even if they do not
20 * have read permissions. The implementation now returns 0
21 * on success as stated in the manual page.
22 * - There is some confusion over whether the set of undo adjustments
23 * to be performed at exit should be done in an atomic manner.
24 * That is, if we are attempting to decrement the semval should we queue
25 * up and wait until we can do so legally?
26 * The original implementation attempted to do this.
27 * The current implementation does not do so. This is because I don't
28 * think it is the right thing (TM) to do, and because I couldn't
29 * see a clean way to get the old behavior with the new design.
30 * The POSIX standard and SVID should be consulted to determine
31 * what behavior is mandated.
32 *
33 * Further notes on refinement (Christoph Rohland, December 1998):
34 * - The POSIX standard says, that the undo adjustments simply should
35 * redo. So the current implementation is o.K.
36 * - The previous code had two flaws:
37 * 1) It actively gave the semaphore to the next waiting process
38 * sleeping on the semaphore. Since this process did not have the
39 * cpu this led to many unnecessary context switches and bad
40 * performance. Now we only check which process should be able to
41 * get the semaphore and if this process wants to reduce some
42 * semaphore value we simply wake it up without doing the
43 * operation. So it has to try to get it later. Thus e.g. the
44 * running process may reacquire the semaphore during the current
45 * time slice. If it only waits for zero or increases the semaphore,
46 * we do the operation in advance and wake it up.
47 * 2) It did not wake up all zero waiting processes. We try to do
48 * better but only get the semops right which only wait for zero or
49 * increase. If there are decrement operations in the operations
50 * array we do the same as before.
51 *
52 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
55 *
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
57 *
58 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +010059 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
62 * Lockless wakeup
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040064 *
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070067 *
68 * namespaces support
69 * OpenVZ, SWsoft Inc.
70 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/init.h>
76#include <linux/proc_fs.h>
77#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/security.h>
79#include <linux/syscalls.h>
80#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080081#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070082#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070083#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070084#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080085#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <asm/uaccess.h>
88#include "util.h"
89
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080090#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Kirill Korotaeve3893532006-10-02 02:18:22 -070092#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070093#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Nadia Derbey7748dbf2007-10-18 23:40:49 -070095static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -080096static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070098static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#endif
100
101#define SEMMSL_FAST 256 /* 512 bytes on stack */
102#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
103
104/*
105 * linked list protection:
106 * sem_undo.id_next,
107 * sem_array.sem_pending{,last},
108 * sem_array.sem_undo: sem_lock() for read/write
109 * sem_undo.proc_next: only "current" is allowed to read/write that field.
110 *
111 */
112
Kirill Korotaeve3893532006-10-02 02:18:22 -0700113#define sc_semmsl sem_ctls[0]
114#define sc_semmns sem_ctls[1]
115#define sc_semopm sem_ctls[2]
116#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800118void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700119{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700120 ns->sc_semmsl = SEMMSL;
121 ns->sc_semmns = SEMMNS;
122 ns->sc_semopm = SEMOPM;
123 ns->sc_semmni = SEMMNI;
124 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800125 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700126}
127
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800128#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700129void sem_exit_ns(struct ipc_namespace *ns)
130{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800131 free_ipcs(ns, &sem_ids(ns), freeary);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700132}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135void __init sem_init (void)
136{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800137 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700138 ipc_init_proc_interface("sysvipc/sem",
139 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700140 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Nadia Derbey3e148c72007-10-18 23:40:54 -0700143/*
144 * This routine is called in the paths where the rw_mutex is held to protect
145 * access to the idr tree.
146 */
147static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
148 int id)
149{
150 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
151
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800152 if (IS_ERR(ipcp))
153 return (struct sem_array *)ipcp;
154
Nadia Derbey3e148c72007-10-18 23:40:54 -0700155 return container_of(ipcp, struct sem_array, sem_perm);
156}
157
158/*
159 * sem_lock_(check_) routines are called in the paths where the rw_mutex
160 * is not held.
161 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700162static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
163{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700164 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
165
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800166 if (IS_ERR(ipcp))
167 return (struct sem_array *)ipcp;
168
Nadia Derbey03f02c72007-10-18 23:40:51 -0700169 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700170}
171
172static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
173 int id)
174{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700175 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
176
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800177 if (IS_ERR(ipcp))
178 return (struct sem_array *)ipcp;
179
Nadia Derbey03f02c72007-10-18 23:40:51 -0700180 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700181}
182
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700183static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
184{
185 ipc_rmid(&sem_ids(ns), &s->sem_perm);
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * Lockless wakeup algorithm:
190 * Without the check/retry algorithm a lockless wakeup is possible:
191 * - queue.status is initialized to -EINTR before blocking.
192 * - wakeup is performed by
193 * * unlinking the queue entry from sma->sem_pending
194 * * setting queue.status to IN_WAKEUP
195 * This is the notification for the blocked thread that a
196 * result value is imminent.
197 * * call wake_up_process
198 * * set queue.status to the final value.
199 * - the previously blocked thread checks queue.status:
200 * * if it's IN_WAKEUP, then it must wait until the value changes
201 * * if it's not -EINTR, then the operation was completed by
202 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800203 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * * otherwise it must acquire the spinlock and check what's up.
205 *
206 * The two-stage algorithm is necessary to protect against the following
207 * races:
208 * - if queue.status is set after wake_up_process, then the woken up idle
209 * thread could race forward and try (and fail) to acquire sma->lock
210 * before update_queue had a chance to set queue.status
211 * - if queue.status is written before wake_up_process and if the
212 * blocked process is woken up by a signal between writing
213 * queue.status and the wake_up_process, then the woken up
214 * process could return from semtimedop and die by calling
215 * sys_exit before wake_up_process is called. Then wake_up_process
216 * will oops, because the task structure is already invalid.
217 * (yes, this happened on s390 with sysv msg).
218 *
219 */
220#define IN_WAKEUP 1
221
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700222/**
223 * newary - Create a new semaphore set
224 * @ns: namespace
225 * @params: ptr to the structure that contains key, semflg and nsems
226 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700227 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700228 */
229
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700230static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int id;
233 int retval;
234 struct sem_array *sma;
235 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700236 key_t key = params->key;
237 int nsems = params->u.nsems;
238 int semflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (!nsems)
241 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700242 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -ENOSPC;
244
245 size = sizeof (*sma) + nsems * sizeof (struct sem);
246 sma = ipc_rcu_alloc(size);
247 if (!sma) {
248 return -ENOMEM;
249 }
250 memset (sma, 0, size);
251
252 sma->sem_perm.mode = (semflg & S_IRWXUGO);
253 sma->sem_perm.key = key;
254
255 sma->sem_perm.security = NULL;
256 retval = security_sem_alloc(sma);
257 if (retval) {
258 ipc_rcu_putref(sma);
259 return retval;
260 }
261
Kirill Korotaeve3893532006-10-02 02:18:22 -0700262 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700263 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 security_sem_free(sma);
265 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700266 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700268 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 sma->sem_base = (struct sem *) &sma[1];
271 /* sma->sem_pending = NULL; */
272 sma->sem_pending_last = &sma->sem_pending;
273 /* sma->undo = NULL; */
274 sma->sem_nsems = nsems;
275 sma->sem_ctime = get_seconds();
276 sem_unlock(sma);
277
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700278 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700281
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700282/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700283 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700284 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700285static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700287 struct sem_array *sma;
288
289 sma = container_of(ipcp, struct sem_array, sem_perm);
290 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700291}
292
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700293/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700294 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700295 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700296static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
297 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700298{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700299 struct sem_array *sma;
300
301 sma = container_of(ipcp, struct sem_array, sem_perm);
302 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700303 return -EINVAL;
304
305 return 0;
306}
307
308asmlinkage long sys_semget(key_t key, int nsems, int semflg)
309{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700310 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700311 struct ipc_ops sem_ops;
312 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Kirill Korotaeve3893532006-10-02 02:18:22 -0700314 ns = current->nsproxy->ipc_ns;
315
316 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700318
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700319 sem_ops.getnew = newary;
320 sem_ops.associate = sem_security;
321 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700322
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700323 sem_params.key = key;
324 sem_params.flg = semflg;
325 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700326
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700327 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330/* Manage the doubly linked list sma->sem_pending as a FIFO:
331 * insert new queue elements at the tail sma->sem_pending_last.
332 */
333static inline void append_to_queue (struct sem_array * sma,
334 struct sem_queue * q)
335{
336 *(q->prev = sma->sem_pending_last) = q;
337 *(sma->sem_pending_last = &q->next) = NULL;
338}
339
340static inline void prepend_to_queue (struct sem_array * sma,
341 struct sem_queue * q)
342{
343 q->next = sma->sem_pending;
344 *(q->prev = &sma->sem_pending) = q;
345 if (q->next)
346 q->next->prev = &q->next;
347 else /* sma->sem_pending_last == &sma->sem_pending */
348 sma->sem_pending_last = &q->next;
349}
350
351static inline void remove_from_queue (struct sem_array * sma,
352 struct sem_queue * q)
353{
354 *(q->prev) = q->next;
355 if (q->next)
356 q->next->prev = q->prev;
357 else /* sma->sem_pending_last == &q->next */
358 sma->sem_pending_last = q->prev;
359 q->prev = NULL; /* mark as removed */
360}
361
362/*
363 * Determine whether a sequence of semaphore operations would succeed
364 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
365 */
366
367static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
368 int nsops, struct sem_undo *un, int pid)
369{
370 int result, sem_op;
371 struct sembuf *sop;
372 struct sem * curr;
373
374 for (sop = sops; sop < sops + nsops; sop++) {
375 curr = sma->sem_base + sop->sem_num;
376 sem_op = sop->sem_op;
377 result = curr->semval;
378
379 if (!sem_op && result)
380 goto would_block;
381
382 result += sem_op;
383 if (result < 0)
384 goto would_block;
385 if (result > SEMVMX)
386 goto out_of_range;
387 if (sop->sem_flg & SEM_UNDO) {
388 int undo = un->semadj[sop->sem_num] - sem_op;
389 /*
390 * Exceeding the undo range is an error.
391 */
392 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
393 goto out_of_range;
394 }
395 curr->semval = result;
396 }
397
398 sop--;
399 while (sop >= sops) {
400 sma->sem_base[sop->sem_num].sempid = pid;
401 if (sop->sem_flg & SEM_UNDO)
402 un->semadj[sop->sem_num] -= sop->sem_op;
403 sop--;
404 }
405
406 sma->sem_otime = get_seconds();
407 return 0;
408
409out_of_range:
410 result = -ERANGE;
411 goto undo;
412
413would_block:
414 if (sop->sem_flg & IPC_NOWAIT)
415 result = -EAGAIN;
416 else
417 result = 1;
418
419undo:
420 sop--;
421 while (sop >= sops) {
422 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
423 sop--;
424 }
425
426 return result;
427}
428
429/* Go through the pending queue for the indicated semaphore
430 * looking for tasks that can be completed.
431 */
432static void update_queue (struct sem_array * sma)
433{
434 int error;
435 struct sem_queue * q;
436
437 q = sma->sem_pending;
438 while(q) {
439 error = try_atomic_semop(sma, q->sops, q->nsops,
440 q->undo, q->pid);
441
442 /* Does q->sleeper still need to sleep? */
443 if (error <= 0) {
444 struct sem_queue *n;
445 remove_from_queue(sma,q);
446 q->status = IN_WAKEUP;
447 /*
448 * Continue scanning. The next operation
449 * that must be checked depends on the type of the
450 * completed operation:
451 * - if the operation modified the array, then
452 * restart from the head of the queue and
453 * check for threads that might be waiting
454 * for semaphore values to become 0.
455 * - if the operation didn't modify the array,
456 * then just continue.
457 */
458 if (q->alter)
459 n = sma->sem_pending;
460 else
461 n = q->next;
462 wake_up_process(q->sleeper);
463 /* hands-off: q will disappear immediately after
464 * writing q->status.
465 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800466 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 q->status = error;
468 q = n;
469 } else {
470 q = q->next;
471 }
472 }
473}
474
475/* The following counts are associated to each semaphore:
476 * semncnt number of tasks waiting on semval being nonzero
477 * semzcnt number of tasks waiting on semval being zero
478 * This model assumes that a task waits on exactly one semaphore.
479 * Since semaphore operations are to be performed atomically, tasks actually
480 * wait on a whole sequence of semaphores simultaneously.
481 * The counts we return here are a rough approximation, but still
482 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
483 */
484static int count_semncnt (struct sem_array * sma, ushort semnum)
485{
486 int semncnt;
487 struct sem_queue * q;
488
489 semncnt = 0;
490 for (q = sma->sem_pending; q; q = q->next) {
491 struct sembuf * sops = q->sops;
492 int nsops = q->nsops;
493 int i;
494 for (i = 0; i < nsops; i++)
495 if (sops[i].sem_num == semnum
496 && (sops[i].sem_op < 0)
497 && !(sops[i].sem_flg & IPC_NOWAIT))
498 semncnt++;
499 }
500 return semncnt;
501}
502static int count_semzcnt (struct sem_array * sma, ushort semnum)
503{
504 int semzcnt;
505 struct sem_queue * q;
506
507 semzcnt = 0;
508 for (q = sma->sem_pending; q; q = q->next) {
509 struct sembuf * sops = q->sops;
510 int nsops = q->nsops;
511 int i;
512 for (i = 0; i < nsops; i++)
513 if (sops[i].sem_num == semnum
514 && (sops[i].sem_op == 0)
515 && !(sops[i].sem_flg & IPC_NOWAIT))
516 semzcnt++;
517 }
518 return semzcnt;
519}
520
Nadia Derbey3e148c72007-10-18 23:40:54 -0700521/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
522 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
523 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800525static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct sem_undo *un;
528 struct sem_queue *q;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800529 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* Invalidate the existing undo structures for this semaphore set.
532 * (They will be freed without any further action in exit_sem()
533 * or during the next semop.)
534 */
535 for (un = sma->undo; un; un = un->id_next)
536 un->semid = -1;
537
538 /* Wake up all pending processes and let them fail with EIDRM. */
539 q = sma->sem_pending;
540 while(q) {
541 struct sem_queue *n;
542 /* lazy remove_from_queue: we are killing the whole queue */
543 q->prev = NULL;
544 n = q->next;
545 q->status = IN_WAKEUP;
546 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100547 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 q->status = -EIDRM; /* hands-off q */
549 q = n;
550 }
551
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700552 /* Remove the semaphore set from the IDR */
553 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 sem_unlock(sma);
555
Kirill Korotaeve3893532006-10-02 02:18:22 -0700556 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 security_sem_free(sma);
558 ipc_rcu_putref(sma);
559}
560
561static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
562{
563 switch(version) {
564 case IPC_64:
565 return copy_to_user(buf, in, sizeof(*in));
566 case IPC_OLD:
567 {
568 struct semid_ds out;
569
570 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
571
572 out.sem_otime = in->sem_otime;
573 out.sem_ctime = in->sem_ctime;
574 out.sem_nsems = in->sem_nsems;
575
576 return copy_to_user(buf, &out, sizeof(out));
577 }
578 default:
579 return -EINVAL;
580 }
581}
582
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800583static int semctl_nolock(struct ipc_namespace *ns, int semid,
584 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 int err = -EINVAL;
587 struct sem_array *sma;
588
589 switch(cmd) {
590 case IPC_INFO:
591 case SEM_INFO:
592 {
593 struct seminfo seminfo;
594 int max_id;
595
596 err = security_sem_semctl(NULL, cmd);
597 if (err)
598 return err;
599
600 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700601 seminfo.semmni = ns->sc_semmni;
602 seminfo.semmns = ns->sc_semmns;
603 seminfo.semmsl = ns->sc_semmsl;
604 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 seminfo.semvmx = SEMVMX;
606 seminfo.semmnu = SEMMNU;
607 seminfo.semmap = SEMMAP;
608 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700609 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700611 seminfo.semusz = sem_ids(ns).in_use;
612 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } else {
614 seminfo.semusz = SEMUSZ;
615 seminfo.semaem = SEMAEM;
616 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700617 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700618 up_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
620 return -EFAULT;
621 return (max_id < 0) ? 0: max_id;
622 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800623 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 case SEM_STAT:
625 {
626 struct semid64_ds tbuf;
627 int id;
628
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800629 if (cmd == SEM_STAT) {
630 sma = sem_lock(ns, semid);
631 if (IS_ERR(sma))
632 return PTR_ERR(sma);
633 id = sma->sem_perm.id;
634 } else {
635 sma = sem_lock_check(ns, semid);
636 if (IS_ERR(sma))
637 return PTR_ERR(sma);
638 id = 0;
639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 err = -EACCES;
642 if (ipcperms (&sma->sem_perm, S_IRUGO))
643 goto out_unlock;
644
645 err = security_sem_semctl(sma, cmd);
646 if (err)
647 goto out_unlock;
648
Nadia Derbey023a5352007-10-18 23:40:51 -0700649 memset(&tbuf, 0, sizeof(tbuf));
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
652 tbuf.sem_otime = sma->sem_otime;
653 tbuf.sem_ctime = sma->sem_ctime;
654 tbuf.sem_nsems = sma->sem_nsems;
655 sem_unlock(sma);
656 if (copy_semid_to_user (arg.buf, &tbuf, version))
657 return -EFAULT;
658 return id;
659 }
660 default:
661 return -EINVAL;
662 }
663 return err;
664out_unlock:
665 sem_unlock(sma);
666 return err;
667}
668
Kirill Korotaeve3893532006-10-02 02:18:22 -0700669static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
670 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 struct sem_array *sma;
673 struct sem* curr;
674 int err;
675 ushort fast_sem_io[SEMMSL_FAST];
676 ushort* sem_io = fast_sem_io;
677 int nsems;
678
Nadia Derbey023a5352007-10-18 23:40:51 -0700679 sma = sem_lock_check(ns, semid);
680 if (IS_ERR(sma))
681 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 nsems = sma->sem_nsems;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 err = -EACCES;
686 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
687 goto out_unlock;
688
689 err = security_sem_semctl(sma, cmd);
690 if (err)
691 goto out_unlock;
692
693 err = -EACCES;
694 switch (cmd) {
695 case GETALL:
696 {
697 ushort __user *array = arg.array;
698 int i;
699
700 if(nsems > SEMMSL_FAST) {
701 ipc_rcu_getref(sma);
702 sem_unlock(sma);
703
704 sem_io = ipc_alloc(sizeof(ushort)*nsems);
705 if(sem_io == NULL) {
706 ipc_lock_by_ptr(&sma->sem_perm);
707 ipc_rcu_putref(sma);
708 sem_unlock(sma);
709 return -ENOMEM;
710 }
711
712 ipc_lock_by_ptr(&sma->sem_perm);
713 ipc_rcu_putref(sma);
714 if (sma->sem_perm.deleted) {
715 sem_unlock(sma);
716 err = -EIDRM;
717 goto out_free;
718 }
719 }
720
721 for (i = 0; i < sma->sem_nsems; i++)
722 sem_io[i] = sma->sem_base[i].semval;
723 sem_unlock(sma);
724 err = 0;
725 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
726 err = -EFAULT;
727 goto out_free;
728 }
729 case SETALL:
730 {
731 int i;
732 struct sem_undo *un;
733
734 ipc_rcu_getref(sma);
735 sem_unlock(sma);
736
737 if(nsems > SEMMSL_FAST) {
738 sem_io = ipc_alloc(sizeof(ushort)*nsems);
739 if(sem_io == NULL) {
740 ipc_lock_by_ptr(&sma->sem_perm);
741 ipc_rcu_putref(sma);
742 sem_unlock(sma);
743 return -ENOMEM;
744 }
745 }
746
747 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
748 ipc_lock_by_ptr(&sma->sem_perm);
749 ipc_rcu_putref(sma);
750 sem_unlock(sma);
751 err = -EFAULT;
752 goto out_free;
753 }
754
755 for (i = 0; i < nsems; i++) {
756 if (sem_io[i] > SEMVMX) {
757 ipc_lock_by_ptr(&sma->sem_perm);
758 ipc_rcu_putref(sma);
759 sem_unlock(sma);
760 err = -ERANGE;
761 goto out_free;
762 }
763 }
764 ipc_lock_by_ptr(&sma->sem_perm);
765 ipc_rcu_putref(sma);
766 if (sma->sem_perm.deleted) {
767 sem_unlock(sma);
768 err = -EIDRM;
769 goto out_free;
770 }
771
772 for (i = 0; i < nsems; i++)
773 sma->sem_base[i].semval = sem_io[i];
774 for (un = sma->undo; un; un = un->id_next)
775 for (i = 0; i < nsems; i++)
776 un->semadj[i] = 0;
777 sma->sem_ctime = get_seconds();
778 /* maybe some queued-up processes were waiting for this */
779 update_queue(sma);
780 err = 0;
781 goto out_unlock;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
784 }
785 err = -EINVAL;
786 if(semnum < 0 || semnum >= nsems)
787 goto out_unlock;
788
789 curr = &sma->sem_base[semnum];
790
791 switch (cmd) {
792 case GETVAL:
793 err = curr->semval;
794 goto out_unlock;
795 case GETPID:
796 err = curr->sempid;
797 goto out_unlock;
798 case GETNCNT:
799 err = count_semncnt(sma,semnum);
800 goto out_unlock;
801 case GETZCNT:
802 err = count_semzcnt(sma,semnum);
803 goto out_unlock;
804 case SETVAL:
805 {
806 int val = arg.val;
807 struct sem_undo *un;
808 err = -ERANGE;
809 if (val > SEMVMX || val < 0)
810 goto out_unlock;
811
812 for (un = sma->undo; un; un = un->id_next)
813 un->semadj[semnum] = 0;
814 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700815 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 sma->sem_ctime = get_seconds();
817 /* maybe some queued-up processes were waiting for this */
818 update_queue(sma);
819 err = 0;
820 goto out_unlock;
821 }
822 }
823out_unlock:
824 sem_unlock(sma);
825out_free:
826 if(sem_io != fast_sem_io)
827 ipc_free(sem_io, sizeof(ushort)*nsems);
828 return err;
829}
830
831struct sem_setbuf {
832 uid_t uid;
833 gid_t gid;
834 mode_t mode;
835};
836
837static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
838{
839 switch(version) {
840 case IPC_64:
841 {
842 struct semid64_ds tbuf;
843
844 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
845 return -EFAULT;
846
847 out->uid = tbuf.sem_perm.uid;
848 out->gid = tbuf.sem_perm.gid;
849 out->mode = tbuf.sem_perm.mode;
850
851 return 0;
852 }
853 case IPC_OLD:
854 {
855 struct semid_ds tbuf_old;
856
857 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
858 return -EFAULT;
859
860 out->uid = tbuf_old.sem_perm.uid;
861 out->gid = tbuf_old.sem_perm.gid;
862 out->mode = tbuf_old.sem_perm.mode;
863
864 return 0;
865 }
866 default:
867 return -EINVAL;
868 }
869}
870
Kirill Korotaeve3893532006-10-02 02:18:22 -0700871static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
872 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct sem_array *sma;
875 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400876 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct kern_ipc_perm *ipcp;
878
879 if(cmd == IPC_SET) {
880 if(copy_semid_from_user (&setbuf, arg.buf, version))
881 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700883 sma = sem_lock_check_down(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700884 if (IS_ERR(sma))
885 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400888
889 err = audit_ipc_obj(ipcp);
890 if (err)
891 goto out_unlock;
892
Linda Knippersac032212006-05-16 22:03:48 -0400893 if (cmd == IPC_SET) {
894 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
895 if (err)
896 goto out_unlock;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (current->euid != ipcp->cuid &&
899 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
900 err=-EPERM;
901 goto out_unlock;
902 }
903
904 err = security_sem_semctl(sma, cmd);
905 if (err)
906 goto out_unlock;
907
908 switch(cmd){
909 case IPC_RMID:
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800910 freeary(ns, ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 err = 0;
912 break;
913 case IPC_SET:
914 ipcp->uid = setbuf.uid;
915 ipcp->gid = setbuf.gid;
916 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
917 | (setbuf.mode & S_IRWXUGO);
918 sma->sem_ctime = get_seconds();
919 sem_unlock(sma);
920 err = 0;
921 break;
922 default:
923 sem_unlock(sma);
924 err = -EINVAL;
925 break;
926 }
927 return err;
928
929out_unlock:
930 sem_unlock(sma);
931 return err;
932}
933
934asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
935{
936 int err = -EINVAL;
937 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700938 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (semid < 0)
941 return -EINVAL;
942
943 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700944 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 switch(cmd) {
947 case IPC_INFO:
948 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800949 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 case SEM_STAT:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800951 err = semctl_nolock(ns, semid, cmd, version, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return err;
953 case GETALL:
954 case GETVAL:
955 case GETPID:
956 case GETNCNT:
957 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 case SETVAL:
959 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700960 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return err;
962 case IPC_RMID:
963 case IPC_SET:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700964 down_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700965 err = semctl_down(ns,semid,semnum,cmd,version,arg);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700966 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return err;
968 default:
969 return -EINVAL;
970 }
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/* If the task doesn't already have a undo_list, then allocate one
974 * here. We guarantee there is only one thread using this undo list,
975 * and current is THE ONE
976 *
977 * If this allocation and assignment succeeds, but later
978 * portions of this code fail, there is no need to free the sem_undo_list.
979 * Just let it stay associated with the task, and it'll be freed later
980 * at exit time.
981 *
982 * This can block, so callers must hold no locks.
983 */
984static inline int get_undo_list(struct sem_undo_list **undo_listp)
985{
986 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 undo_list = current->sysvsem.undo_list;
989 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -0700990 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (undo_list == NULL)
992 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200993 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 atomic_set(&undo_list->refcnt, 1);
995 current->sysvsem.undo_list = undo_list;
996 }
997 *undo_listp = undo_list;
998 return 0;
999}
1000
1001static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1002{
1003 struct sem_undo **last, *un;
1004
1005 last = &ulp->proc_list;
1006 un = *last;
1007 while(un != NULL) {
1008 if(un->semid==semid)
1009 break;
1010 if(un->semid==-1) {
1011 *last=un->proc_next;
1012 kfree(un);
1013 } else {
1014 last=&un->proc_next;
1015 }
1016 un=*last;
1017 }
1018 return un;
1019}
1020
Kirill Korotaeve3893532006-10-02 02:18:22 -07001021static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 struct sem_array *sma;
1024 struct sem_undo_list *ulp;
1025 struct sem_undo *un, *new;
1026 int nsems;
1027 int error;
1028
1029 error = get_undo_list(&ulp);
1030 if (error)
1031 return ERR_PTR(error);
1032
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001033 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001035 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (likely(un!=NULL))
1037 goto out;
1038
1039 /* no undo structure around - allocate one. */
Nadia Derbey023a5352007-10-18 23:40:51 -07001040 sma = sem_lock_check(ns, semid);
1041 if (IS_ERR(sma))
1042 return ERR_PTR(PTR_ERR(sma));
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 nsems = sma->sem_nsems;
1045 ipc_rcu_getref(sma);
1046 sem_unlock(sma);
1047
Burman Yan4668edc2006-12-06 20:38:51 -08001048 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (!new) {
1050 ipc_lock_by_ptr(&sma->sem_perm);
1051 ipc_rcu_putref(sma);
1052 sem_unlock(sma);
1053 return ERR_PTR(-ENOMEM);
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 new->semadj = (short *) &new[1];
1056 new->semid = semid;
1057
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001058 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 un = lookup_undo(ulp, semid);
1060 if (un) {
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001061 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 kfree(new);
1063 ipc_lock_by_ptr(&sma->sem_perm);
1064 ipc_rcu_putref(sma);
1065 sem_unlock(sma);
1066 goto out;
1067 }
1068 ipc_lock_by_ptr(&sma->sem_perm);
1069 ipc_rcu_putref(sma);
1070 if (sma->sem_perm.deleted) {
1071 sem_unlock(sma);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001072 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 kfree(new);
1074 un = ERR_PTR(-EIDRM);
1075 goto out;
1076 }
1077 new->proc_next = ulp->proc_list;
1078 ulp->proc_list = new;
1079 new->id_next = sma->undo;
1080 sma->undo = new;
1081 sem_unlock(sma);
1082 un = new;
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001083 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084out:
1085 return un;
1086}
1087
1088asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1089 unsigned nsops, const struct timespec __user *timeout)
1090{
1091 int error = -EINVAL;
1092 struct sem_array *sma;
1093 struct sembuf fast_sops[SEMOPM_FAST];
1094 struct sembuf* sops = fast_sops, *sop;
1095 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001096 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct sem_queue queue;
1098 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001099 struct ipc_namespace *ns;
1100
1101 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (nsops < 1 || semid < 0)
1104 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001105 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -E2BIG;
1107 if(nsops > SEMOPM_FAST) {
1108 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1109 if(sops==NULL)
1110 return -ENOMEM;
1111 }
1112 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1113 error=-EFAULT;
1114 goto out_free;
1115 }
1116 if (timeout) {
1117 struct timespec _timeout;
1118 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1119 error = -EFAULT;
1120 goto out_free;
1121 }
1122 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1123 _timeout.tv_nsec >= 1000000000L) {
1124 error = -EINVAL;
1125 goto out_free;
1126 }
1127 jiffies_left = timespec_to_jiffies(&_timeout);
1128 }
1129 max = 0;
1130 for (sop = sops; sop < sops + nsops; sop++) {
1131 if (sop->sem_num >= max)
1132 max = sop->sem_num;
1133 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001134 undos = 1;
1135 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 alter = 1;
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139retry_undos:
1140 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001141 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (IS_ERR(un)) {
1143 error = PTR_ERR(un);
1144 goto out_free;
1145 }
1146 } else
1147 un = NULL;
1148
Nadia Derbey023a5352007-10-18 23:40:51 -07001149 sma = sem_lock_check(ns, semid);
1150 if (IS_ERR(sma)) {
1151 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001153 }
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
Nadia Derbey023a5352007-10-18 23:40:51 -07001156 * semid identifiers are not unique - find_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * allocated an undo structure, it was invalidated by an RMID
1158 * and now a new array with received the same id. Check and retry.
1159 */
1160 if (un && un->semid == -1) {
1161 sem_unlock(sma);
1162 goto retry_undos;
1163 }
1164 error = -EFBIG;
1165 if (max >= sma->sem_nsems)
1166 goto out_unlock_free;
1167
1168 error = -EACCES;
1169 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1170 goto out_unlock_free;
1171
1172 error = security_sem_semop(sma, sops, nsops, alter);
1173 if (error)
1174 goto out_unlock_free;
1175
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001176 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (error <= 0) {
1178 if (alter && error == 0)
1179 update_queue (sma);
1180 goto out_unlock_free;
1181 }
1182
1183 /* We need to sleep on this operation, so we put the current
1184 * task into the pending queue and go to sleep.
1185 */
1186
1187 queue.sma = sma;
1188 queue.sops = sops;
1189 queue.nsops = nsops;
1190 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001191 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 queue.id = semid;
1193 queue.alter = alter;
1194 if (alter)
1195 append_to_queue(sma ,&queue);
1196 else
1197 prepend_to_queue(sma ,&queue);
1198
1199 queue.status = -EINTR;
1200 queue.sleeper = current;
1201 current->state = TASK_INTERRUPTIBLE;
1202 sem_unlock(sma);
1203
1204 if (timeout)
1205 jiffies_left = schedule_timeout(jiffies_left);
1206 else
1207 schedule();
1208
1209 error = queue.status;
1210 while(unlikely(error == IN_WAKEUP)) {
1211 cpu_relax();
1212 error = queue.status;
1213 }
1214
1215 if (error != -EINTR) {
1216 /* fast path: update_queue already obtained all requested
1217 * resources */
1218 goto out_free;
1219 }
1220
Kirill Korotaeve3893532006-10-02 02:18:22 -07001221 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001222 if (IS_ERR(sma)) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001223 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 error = -EIDRM;
1225 goto out_free;
1226 }
1227
1228 /*
1229 * If queue.status != -EINTR we are woken up by another process
1230 */
1231 error = queue.status;
1232 if (error != -EINTR) {
1233 goto out_unlock_free;
1234 }
1235
1236 /*
1237 * If an interrupt occurred we have to clean up the queue
1238 */
1239 if (timeout && jiffies_left == 0)
1240 error = -EAGAIN;
1241 remove_from_queue(sma,&queue);
1242 goto out_unlock_free;
1243
1244out_unlock_free:
1245 sem_unlock(sma);
1246out_free:
1247 if(sops != fast_sops)
1248 kfree(sops);
1249 return error;
1250}
1251
1252asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1253{
1254 return sys_semtimedop(semid, tsops, nsops, NULL);
1255}
1256
1257/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1258 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
1260
1261int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1262{
1263 struct sem_undo_list *undo_list;
1264 int error;
1265
1266 if (clone_flags & CLONE_SYSVSEM) {
1267 error = get_undo_list(&undo_list);
1268 if (error)
1269 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 atomic_inc(&undo_list->refcnt);
1271 tsk->sysvsem.undo_list = undo_list;
1272 } else
1273 tsk->sysvsem.undo_list = NULL;
1274
1275 return 0;
1276}
1277
1278/*
1279 * add semadj values to semaphores, free undo structures.
1280 * undo structures are not freed when semaphore arrays are destroyed
1281 * so some of them may be out of date.
1282 * IMPLEMENTATION NOTE: There is some confusion over whether the
1283 * set of adjustments that needs to be done should be done in an atomic
1284 * manner or not. That is, if we are attempting to decrement the semval
1285 * should we queue up and wait until we can do so legally?
1286 * The original implementation attempted to do this (queue and wait).
1287 * The current implementation does not do so. The POSIX standard
1288 * and SVID should be consulted to determine what behavior is mandated.
1289 */
1290void exit_sem(struct task_struct *tsk)
1291{
1292 struct sem_undo_list *undo_list;
1293 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001294 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 undo_list = tsk->sysvsem.undo_list;
1297 if (!undo_list)
1298 return;
1299
1300 if (!atomic_dec_and_test(&undo_list->refcnt))
1301 return;
1302
Kirill Korotaeve3893532006-10-02 02:18:22 -07001303 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /* There's no need to hold the semundo list lock, as current
1305 * is the last task exiting for this undo list.
1306 */
1307 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1308 struct sem_array *sma;
1309 int nsems, i;
1310 struct sem_undo *un, **unp;
1311 int semid;
1312
1313 semid = u->semid;
1314
1315 if(semid == -1)
1316 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001317 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001318 if (IS_ERR(sma))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 continue;
1320
1321 if (u->semid == -1)
1322 goto next_entry;
1323
Nadia Derbey1b531f22007-10-18 23:40:55 -07001324 BUG_ON(sem_checkid(sma, u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /* remove u from the sma->undo list */
1327 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1328 if (u == un)
1329 goto found;
1330 }
1331 printk ("exit_sem undo list error id=%d\n", u->semid);
1332 goto next_entry;
1333found:
1334 *unp = un->id_next;
1335 /* perform adjustments registered in u */
1336 nsems = sma->sem_nsems;
1337 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001338 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001340 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /*
1342 * Range checks of the new semaphore value,
1343 * not defined by sus:
1344 * - Some unices ignore the undo entirely
1345 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1346 * - some cap the value (e.g. FreeBSD caps
1347 * at 0, but doesn't enforce SEMVMX)
1348 *
1349 * Linux caps the semaphore value, both at 0
1350 * and at SEMVMX.
1351 *
1352 * Manfred <manfred@colorfullife.com>
1353 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001354 if (semaphore->semval < 0)
1355 semaphore->semval = 0;
1356 if (semaphore->semval > SEMVMX)
1357 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001358 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 }
1361 sma->sem_otime = get_seconds();
1362 /* maybe some queued-up processes were waiting for this */
1363 update_queue(sma);
1364next_entry:
1365 sem_unlock(sma);
1366 }
1367 kfree(undo_list);
1368}
1369
1370#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001371static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Mike Waychison19b49462005-09-06 15:17:10 -07001373 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Mike Waychison19b49462005-09-06 15:17:10 -07001375 return seq_printf(s,
1376 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1377 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001378 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001379 sma->sem_perm.mode,
1380 sma->sem_nsems,
1381 sma->sem_perm.uid,
1382 sma->sem_perm.gid,
1383 sma->sem_perm.cuid,
1384 sma->sem_perm.cgid,
1385 sma->sem_otime,
1386 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388#endif