blob: 64ff4261f4e27dfca56edb3dab68cf52400a0624 [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>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080083#include <linux/mutex.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070084#include <linux/nsproxy.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <asm/uaccess.h>
87#include "util.h"
88
Kirill Korotaeve3893532006-10-02 02:18:22 -070089#define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Kirill Korotaeve3893532006-10-02 02:18:22 -070091#define sem_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
92#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
93#define sem_rmid(ns, id) ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
94#define sem_checkid(ns, sma, semid) \
95 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
96#define sem_buildid(ns, id, seq) \
97 ipc_buildid(&sem_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Kirill Korotaeve3893532006-10-02 02:18:22 -070099static struct ipc_ids init_sem_ids;
100
101static int newary(struct ipc_namespace *, key_t, int, int);
102static void freeary(struct ipc_namespace *ns, struct sem_array *sma, int id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700104static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
107#define SEMMSL_FAST 256 /* 512 bytes on stack */
108#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
109
110/*
111 * linked list protection:
112 * sem_undo.id_next,
113 * sem_array.sem_pending{,last},
114 * sem_array.sem_undo: sem_lock() for read/write
115 * sem_undo.proc_next: only "current" is allowed to read/write that field.
116 *
117 */
118
Kirill Korotaeve3893532006-10-02 02:18:22 -0700119#define sc_semmsl sem_ctls[0]
120#define sc_semmns sem_ctls[1]
121#define sc_semopm sem_ctls[2]
122#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -0700124static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700125{
126 ns->ids[IPC_SEM_IDS] = ids;
127 ns->sc_semmsl = SEMMSL;
128 ns->sc_semmns = SEMMNS;
129 ns->sc_semopm = SEMOPM;
130 ns->sc_semmni = SEMMNI;
131 ns->used_sems = 0;
132 ipc_init_ids(ids, ns->sc_semmni);
133}
134
Kirill Korotaeve3893532006-10-02 02:18:22 -0700135int sem_init_ns(struct ipc_namespace *ns)
136{
137 struct ipc_ids *ids;
138
139 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
140 if (ids == NULL)
141 return -ENOMEM;
142
143 __sem_init_ns(ns, ids);
144 return 0;
145}
146
147void sem_exit_ns(struct ipc_namespace *ns)
148{
149 int i;
150 struct sem_array *sma;
151
152 mutex_lock(&sem_ids(ns).mutex);
153 for (i = 0; i <= sem_ids(ns).max_id; i++) {
154 sma = sem_lock(ns, i);
155 if (sma == NULL)
156 continue;
157
158 freeary(ns, sma, i);
159 }
160 mutex_unlock(&sem_ids(ns).mutex);
161
Pavel Emelianovc7e12b82006-11-02 22:07:03 -0800162 ipc_fini_ids(ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700163 kfree(ns->ids[IPC_SEM_IDS]);
164 ns->ids[IPC_SEM_IDS] = NULL;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167void __init sem_init (void)
168{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700169 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700170 ipc_init_proc_interface("sysvipc/sem",
171 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700172 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * Lockless wakeup algorithm:
177 * Without the check/retry algorithm a lockless wakeup is possible:
178 * - queue.status is initialized to -EINTR before blocking.
179 * - wakeup is performed by
180 * * unlinking the queue entry from sma->sem_pending
181 * * setting queue.status to IN_WAKEUP
182 * This is the notification for the blocked thread that a
183 * result value is imminent.
184 * * call wake_up_process
185 * * set queue.status to the final value.
186 * - the previously blocked thread checks queue.status:
187 * * if it's IN_WAKEUP, then it must wait until the value changes
188 * * if it's not -EINTR, then the operation was completed by
189 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800190 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * * otherwise it must acquire the spinlock and check what's up.
192 *
193 * The two-stage algorithm is necessary to protect against the following
194 * races:
195 * - if queue.status is set after wake_up_process, then the woken up idle
196 * thread could race forward and try (and fail) to acquire sma->lock
197 * before update_queue had a chance to set queue.status
198 * - if queue.status is written before wake_up_process and if the
199 * blocked process is woken up by a signal between writing
200 * queue.status and the wake_up_process, then the woken up
201 * process could return from semtimedop and die by calling
202 * sys_exit before wake_up_process is called. Then wake_up_process
203 * will oops, because the task structure is already invalid.
204 * (yes, this happened on s390 with sysv msg).
205 *
206 */
207#define IN_WAKEUP 1
208
Kirill Korotaeve3893532006-10-02 02:18:22 -0700209static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 int id;
212 int retval;
213 struct sem_array *sma;
214 int size;
215
216 if (!nsems)
217 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700218 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -ENOSPC;
220
221 size = sizeof (*sma) + nsems * sizeof (struct sem);
222 sma = ipc_rcu_alloc(size);
223 if (!sma) {
224 return -ENOMEM;
225 }
226 memset (sma, 0, size);
227
228 sma->sem_perm.mode = (semflg & S_IRWXUGO);
229 sma->sem_perm.key = key;
230
231 sma->sem_perm.security = NULL;
232 retval = security_sem_alloc(sma);
233 if (retval) {
234 ipc_rcu_putref(sma);
235 return retval;
236 }
237
Kirill Korotaeve3893532006-10-02 02:18:22 -0700238 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if(id == -1) {
240 security_sem_free(sma);
241 ipc_rcu_putref(sma);
242 return -ENOSPC;
243 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700244 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Kirill Korotaeve3893532006-10-02 02:18:22 -0700246 sma->sem_id = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 sma->sem_base = (struct sem *) &sma[1];
248 /* sma->sem_pending = NULL; */
249 sma->sem_pending_last = &sma->sem_pending;
250 /* sma->undo = NULL; */
251 sma->sem_nsems = nsems;
252 sma->sem_ctime = get_seconds();
253 sem_unlock(sma);
254
Mike Waychison19b49462005-09-06 15:17:10 -0700255 return sma->sem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258asmlinkage long sys_semget (key_t key, int nsems, int semflg)
259{
260 int id, err = -EINVAL;
261 struct sem_array *sma;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700262 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kirill Korotaeve3893532006-10-02 02:18:22 -0700264 ns = current->nsproxy->ipc_ns;
265
266 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700268 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (key == IPC_PRIVATE) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700271 err = newary(ns, key, nsems, semflg);
272 } else if ((id = ipc_findkey(&sem_ids(ns), key)) == -1) { /* key not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!(semflg & IPC_CREAT))
274 err = -ENOENT;
275 else
Kirill Korotaeve3893532006-10-02 02:18:22 -0700276 err = newary(ns, key, nsems, semflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
278 err = -EEXIST;
279 } else {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700280 sma = sem_lock(ns, id);
Eric Sesterhenn27315c92006-03-26 18:28:38 +0200281 BUG_ON(sma==NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (nsems > sma->sem_nsems)
283 err = -EINVAL;
284 else if (ipcperms(&sma->sem_perm, semflg))
285 err = -EACCES;
286 else {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700287 int semid = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err = security_sem_associate(sma, semflg);
289 if (!err)
290 err = semid;
291 }
292 sem_unlock(sma);
293 }
294
Kirill Korotaeve3893532006-10-02 02:18:22 -0700295 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return err;
297}
298
299/* Manage the doubly linked list sma->sem_pending as a FIFO:
300 * insert new queue elements at the tail sma->sem_pending_last.
301 */
302static inline void append_to_queue (struct sem_array * sma,
303 struct sem_queue * q)
304{
305 *(q->prev = sma->sem_pending_last) = q;
306 *(sma->sem_pending_last = &q->next) = NULL;
307}
308
309static inline void prepend_to_queue (struct sem_array * sma,
310 struct sem_queue * q)
311{
312 q->next = sma->sem_pending;
313 *(q->prev = &sma->sem_pending) = q;
314 if (q->next)
315 q->next->prev = &q->next;
316 else /* sma->sem_pending_last == &sma->sem_pending */
317 sma->sem_pending_last = &q->next;
318}
319
320static inline void remove_from_queue (struct sem_array * sma,
321 struct sem_queue * q)
322{
323 *(q->prev) = q->next;
324 if (q->next)
325 q->next->prev = q->prev;
326 else /* sma->sem_pending_last == &q->next */
327 sma->sem_pending_last = q->prev;
328 q->prev = NULL; /* mark as removed */
329}
330
331/*
332 * Determine whether a sequence of semaphore operations would succeed
333 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
334 */
335
336static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
337 int nsops, struct sem_undo *un, int pid)
338{
339 int result, sem_op;
340 struct sembuf *sop;
341 struct sem * curr;
342
343 for (sop = sops; sop < sops + nsops; sop++) {
344 curr = sma->sem_base + sop->sem_num;
345 sem_op = sop->sem_op;
346 result = curr->semval;
347
348 if (!sem_op && result)
349 goto would_block;
350
351 result += sem_op;
352 if (result < 0)
353 goto would_block;
354 if (result > SEMVMX)
355 goto out_of_range;
356 if (sop->sem_flg & SEM_UNDO) {
357 int undo = un->semadj[sop->sem_num] - sem_op;
358 /*
359 * Exceeding the undo range is an error.
360 */
361 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
362 goto out_of_range;
363 }
364 curr->semval = result;
365 }
366
367 sop--;
368 while (sop >= sops) {
369 sma->sem_base[sop->sem_num].sempid = pid;
370 if (sop->sem_flg & SEM_UNDO)
371 un->semadj[sop->sem_num] -= sop->sem_op;
372 sop--;
373 }
374
375 sma->sem_otime = get_seconds();
376 return 0;
377
378out_of_range:
379 result = -ERANGE;
380 goto undo;
381
382would_block:
383 if (sop->sem_flg & IPC_NOWAIT)
384 result = -EAGAIN;
385 else
386 result = 1;
387
388undo:
389 sop--;
390 while (sop >= sops) {
391 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
392 sop--;
393 }
394
395 return result;
396}
397
398/* Go through the pending queue for the indicated semaphore
399 * looking for tasks that can be completed.
400 */
401static void update_queue (struct sem_array * sma)
402{
403 int error;
404 struct sem_queue * q;
405
406 q = sma->sem_pending;
407 while(q) {
408 error = try_atomic_semop(sma, q->sops, q->nsops,
409 q->undo, q->pid);
410
411 /* Does q->sleeper still need to sleep? */
412 if (error <= 0) {
413 struct sem_queue *n;
414 remove_from_queue(sma,q);
415 q->status = IN_WAKEUP;
416 /*
417 * Continue scanning. The next operation
418 * that must be checked depends on the type of the
419 * completed operation:
420 * - if the operation modified the array, then
421 * restart from the head of the queue and
422 * check for threads that might be waiting
423 * for semaphore values to become 0.
424 * - if the operation didn't modify the array,
425 * then just continue.
426 */
427 if (q->alter)
428 n = sma->sem_pending;
429 else
430 n = q->next;
431 wake_up_process(q->sleeper);
432 /* hands-off: q will disappear immediately after
433 * writing q->status.
434 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800435 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 q->status = error;
437 q = n;
438 } else {
439 q = q->next;
440 }
441 }
442}
443
444/* The following counts are associated to each semaphore:
445 * semncnt number of tasks waiting on semval being nonzero
446 * semzcnt number of tasks waiting on semval being zero
447 * This model assumes that a task waits on exactly one semaphore.
448 * Since semaphore operations are to be performed atomically, tasks actually
449 * wait on a whole sequence of semaphores simultaneously.
450 * The counts we return here are a rough approximation, but still
451 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
452 */
453static int count_semncnt (struct sem_array * sma, ushort semnum)
454{
455 int semncnt;
456 struct sem_queue * q;
457
458 semncnt = 0;
459 for (q = sma->sem_pending; q; q = q->next) {
460 struct sembuf * sops = q->sops;
461 int nsops = q->nsops;
462 int i;
463 for (i = 0; i < nsops; i++)
464 if (sops[i].sem_num == semnum
465 && (sops[i].sem_op < 0)
466 && !(sops[i].sem_flg & IPC_NOWAIT))
467 semncnt++;
468 }
469 return semncnt;
470}
471static int count_semzcnt (struct sem_array * sma, ushort semnum)
472{
473 int semzcnt;
474 struct sem_queue * q;
475
476 semzcnt = 0;
477 for (q = sma->sem_pending; q; q = q->next) {
478 struct sembuf * sops = q->sops;
479 int nsops = q->nsops;
480 int i;
481 for (i = 0; i < nsops; i++)
482 if (sops[i].sem_num == semnum
483 && (sops[i].sem_op == 0)
484 && !(sops[i].sem_flg & IPC_NOWAIT))
485 semzcnt++;
486 }
487 return semzcnt;
488}
489
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800490/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
491 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 * on exit.
493 */
Kirill Korotaeve3893532006-10-02 02:18:22 -0700494static void freeary (struct ipc_namespace *ns, struct sem_array *sma, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct sem_undo *un;
497 struct sem_queue *q;
498 int size;
499
500 /* Invalidate the existing undo structures for this semaphore set.
501 * (They will be freed without any further action in exit_sem()
502 * or during the next semop.)
503 */
504 for (un = sma->undo; un; un = un->id_next)
505 un->semid = -1;
506
507 /* Wake up all pending processes and let them fail with EIDRM. */
508 q = sma->sem_pending;
509 while(q) {
510 struct sem_queue *n;
511 /* lazy remove_from_queue: we are killing the whole queue */
512 q->prev = NULL;
513 n = q->next;
514 q->status = IN_WAKEUP;
515 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100516 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 q->status = -EIDRM; /* hands-off q */
518 q = n;
519 }
520
521 /* Remove the semaphore set from the ID array*/
Kirill Korotaeve3893532006-10-02 02:18:22 -0700522 sma = sem_rmid(ns, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sem_unlock(sma);
524
Kirill Korotaeve3893532006-10-02 02:18:22 -0700525 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
527 security_sem_free(sma);
528 ipc_rcu_putref(sma);
529}
530
531static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
532{
533 switch(version) {
534 case IPC_64:
535 return copy_to_user(buf, in, sizeof(*in));
536 case IPC_OLD:
537 {
538 struct semid_ds out;
539
540 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
541
542 out.sem_otime = in->sem_otime;
543 out.sem_ctime = in->sem_ctime;
544 out.sem_nsems = in->sem_nsems;
545
546 return copy_to_user(buf, &out, sizeof(out));
547 }
548 default:
549 return -EINVAL;
550 }
551}
552
Kirill Korotaeve3893532006-10-02 02:18:22 -0700553static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
554 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 int err = -EINVAL;
557 struct sem_array *sma;
558
559 switch(cmd) {
560 case IPC_INFO:
561 case SEM_INFO:
562 {
563 struct seminfo seminfo;
564 int max_id;
565
566 err = security_sem_semctl(NULL, cmd);
567 if (err)
568 return err;
569
570 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700571 seminfo.semmni = ns->sc_semmni;
572 seminfo.semmns = ns->sc_semmns;
573 seminfo.semmsl = ns->sc_semmsl;
574 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 seminfo.semvmx = SEMVMX;
576 seminfo.semmnu = SEMMNU;
577 seminfo.semmap = SEMMAP;
578 seminfo.semume = SEMUME;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700579 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700581 seminfo.semusz = sem_ids(ns).in_use;
582 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 } else {
584 seminfo.semusz = SEMUSZ;
585 seminfo.semaem = SEMAEM;
586 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700587 max_id = sem_ids(ns).max_id;
588 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
590 return -EFAULT;
591 return (max_id < 0) ? 0: max_id;
592 }
593 case SEM_STAT:
594 {
595 struct semid64_ds tbuf;
596 int id;
597
Kirill Korotaeve3893532006-10-02 02:18:22 -0700598 if(semid >= sem_ids(ns).entries->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EINVAL;
600
601 memset(&tbuf,0,sizeof(tbuf));
602
Kirill Korotaeve3893532006-10-02 02:18:22 -0700603 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if(sma == NULL)
605 return -EINVAL;
606
607 err = -EACCES;
608 if (ipcperms (&sma->sem_perm, S_IRUGO))
609 goto out_unlock;
610
611 err = security_sem_semctl(sma, cmd);
612 if (err)
613 goto out_unlock;
614
Kirill Korotaeve3893532006-10-02 02:18:22 -0700615 id = sem_buildid(ns, semid, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
618 tbuf.sem_otime = sma->sem_otime;
619 tbuf.sem_ctime = sma->sem_ctime;
620 tbuf.sem_nsems = sma->sem_nsems;
621 sem_unlock(sma);
622 if (copy_semid_to_user (arg.buf, &tbuf, version))
623 return -EFAULT;
624 return id;
625 }
626 default:
627 return -EINVAL;
628 }
629 return err;
630out_unlock:
631 sem_unlock(sma);
632 return err;
633}
634
Kirill Korotaeve3893532006-10-02 02:18:22 -0700635static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
636 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
638 struct sem_array *sma;
639 struct sem* curr;
640 int err;
641 ushort fast_sem_io[SEMMSL_FAST];
642 ushort* sem_io = fast_sem_io;
643 int nsems;
644
Kirill Korotaeve3893532006-10-02 02:18:22 -0700645 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if(sma==NULL)
647 return -EINVAL;
648
649 nsems = sma->sem_nsems;
650
651 err=-EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700652 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto out_unlock;
654
655 err = -EACCES;
656 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
657 goto out_unlock;
658
659 err = security_sem_semctl(sma, cmd);
660 if (err)
661 goto out_unlock;
662
663 err = -EACCES;
664 switch (cmd) {
665 case GETALL:
666 {
667 ushort __user *array = arg.array;
668 int i;
669
670 if(nsems > SEMMSL_FAST) {
671 ipc_rcu_getref(sma);
672 sem_unlock(sma);
673
674 sem_io = ipc_alloc(sizeof(ushort)*nsems);
675 if(sem_io == NULL) {
676 ipc_lock_by_ptr(&sma->sem_perm);
677 ipc_rcu_putref(sma);
678 sem_unlock(sma);
679 return -ENOMEM;
680 }
681
682 ipc_lock_by_ptr(&sma->sem_perm);
683 ipc_rcu_putref(sma);
684 if (sma->sem_perm.deleted) {
685 sem_unlock(sma);
686 err = -EIDRM;
687 goto out_free;
688 }
689 }
690
691 for (i = 0; i < sma->sem_nsems; i++)
692 sem_io[i] = sma->sem_base[i].semval;
693 sem_unlock(sma);
694 err = 0;
695 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
696 err = -EFAULT;
697 goto out_free;
698 }
699 case SETALL:
700 {
701 int i;
702 struct sem_undo *un;
703
704 ipc_rcu_getref(sma);
705 sem_unlock(sma);
706
707 if(nsems > SEMMSL_FAST) {
708 sem_io = ipc_alloc(sizeof(ushort)*nsems);
709 if(sem_io == NULL) {
710 ipc_lock_by_ptr(&sma->sem_perm);
711 ipc_rcu_putref(sma);
712 sem_unlock(sma);
713 return -ENOMEM;
714 }
715 }
716
717 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
718 ipc_lock_by_ptr(&sma->sem_perm);
719 ipc_rcu_putref(sma);
720 sem_unlock(sma);
721 err = -EFAULT;
722 goto out_free;
723 }
724
725 for (i = 0; i < nsems; i++) {
726 if (sem_io[i] > SEMVMX) {
727 ipc_lock_by_ptr(&sma->sem_perm);
728 ipc_rcu_putref(sma);
729 sem_unlock(sma);
730 err = -ERANGE;
731 goto out_free;
732 }
733 }
734 ipc_lock_by_ptr(&sma->sem_perm);
735 ipc_rcu_putref(sma);
736 if (sma->sem_perm.deleted) {
737 sem_unlock(sma);
738 err = -EIDRM;
739 goto out_free;
740 }
741
742 for (i = 0; i < nsems; i++)
743 sma->sem_base[i].semval = sem_io[i];
744 for (un = sma->undo; un; un = un->id_next)
745 for (i = 0; i < nsems; i++)
746 un->semadj[i] = 0;
747 sma->sem_ctime = get_seconds();
748 /* maybe some queued-up processes were waiting for this */
749 update_queue(sma);
750 err = 0;
751 goto out_unlock;
752 }
753 case IPC_STAT:
754 {
755 struct semid64_ds tbuf;
756 memset(&tbuf,0,sizeof(tbuf));
757 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
758 tbuf.sem_otime = sma->sem_otime;
759 tbuf.sem_ctime = sma->sem_ctime;
760 tbuf.sem_nsems = sma->sem_nsems;
761 sem_unlock(sma);
762 if (copy_semid_to_user (arg.buf, &tbuf, version))
763 return -EFAULT;
764 return 0;
765 }
766 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
767 }
768 err = -EINVAL;
769 if(semnum < 0 || semnum >= nsems)
770 goto out_unlock;
771
772 curr = &sma->sem_base[semnum];
773
774 switch (cmd) {
775 case GETVAL:
776 err = curr->semval;
777 goto out_unlock;
778 case GETPID:
779 err = curr->sempid;
780 goto out_unlock;
781 case GETNCNT:
782 err = count_semncnt(sma,semnum);
783 goto out_unlock;
784 case GETZCNT:
785 err = count_semzcnt(sma,semnum);
786 goto out_unlock;
787 case SETVAL:
788 {
789 int val = arg.val;
790 struct sem_undo *un;
791 err = -ERANGE;
792 if (val > SEMVMX || val < 0)
793 goto out_unlock;
794
795 for (un = sma->undo; un; un = un->id_next)
796 un->semadj[semnum] = 0;
797 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700798 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 sma->sem_ctime = get_seconds();
800 /* maybe some queued-up processes were waiting for this */
801 update_queue(sma);
802 err = 0;
803 goto out_unlock;
804 }
805 }
806out_unlock:
807 sem_unlock(sma);
808out_free:
809 if(sem_io != fast_sem_io)
810 ipc_free(sem_io, sizeof(ushort)*nsems);
811 return err;
812}
813
814struct sem_setbuf {
815 uid_t uid;
816 gid_t gid;
817 mode_t mode;
818};
819
820static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
821{
822 switch(version) {
823 case IPC_64:
824 {
825 struct semid64_ds tbuf;
826
827 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
828 return -EFAULT;
829
830 out->uid = tbuf.sem_perm.uid;
831 out->gid = tbuf.sem_perm.gid;
832 out->mode = tbuf.sem_perm.mode;
833
834 return 0;
835 }
836 case IPC_OLD:
837 {
838 struct semid_ds tbuf_old;
839
840 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
841 return -EFAULT;
842
843 out->uid = tbuf_old.sem_perm.uid;
844 out->gid = tbuf_old.sem_perm.gid;
845 out->mode = tbuf_old.sem_perm.mode;
846
847 return 0;
848 }
849 default:
850 return -EINVAL;
851 }
852}
853
Kirill Korotaeve3893532006-10-02 02:18:22 -0700854static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
855 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
857 struct sem_array *sma;
858 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400859 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct kern_ipc_perm *ipcp;
861
862 if(cmd == IPC_SET) {
863 if(copy_semid_from_user (&setbuf, arg.buf, version))
864 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700866 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if(sma==NULL)
868 return -EINVAL;
869
Kirill Korotaeve3893532006-10-02 02:18:22 -0700870 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 err=-EIDRM;
872 goto out_unlock;
873 }
874 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400875
876 err = audit_ipc_obj(ipcp);
877 if (err)
878 goto out_unlock;
879
Linda Knippersac032212006-05-16 22:03:48 -0400880 if (cmd == IPC_SET) {
881 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
882 if (err)
883 goto out_unlock;
884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (current->euid != ipcp->cuid &&
886 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
887 err=-EPERM;
888 goto out_unlock;
889 }
890
891 err = security_sem_semctl(sma, cmd);
892 if (err)
893 goto out_unlock;
894
895 switch(cmd){
896 case IPC_RMID:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700897 freeary(ns, sma, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 err = 0;
899 break;
900 case IPC_SET:
901 ipcp->uid = setbuf.uid;
902 ipcp->gid = setbuf.gid;
903 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
904 | (setbuf.mode & S_IRWXUGO);
905 sma->sem_ctime = get_seconds();
906 sem_unlock(sma);
907 err = 0;
908 break;
909 default:
910 sem_unlock(sma);
911 err = -EINVAL;
912 break;
913 }
914 return err;
915
916out_unlock:
917 sem_unlock(sma);
918 return err;
919}
920
921asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
922{
923 int err = -EINVAL;
924 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700925 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (semid < 0)
928 return -EINVAL;
929
930 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700931 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 switch(cmd) {
934 case IPC_INFO:
935 case SEM_INFO:
936 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700937 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return err;
939 case GETALL:
940 case GETVAL:
941 case GETPID:
942 case GETNCNT:
943 case GETZCNT:
944 case IPC_STAT:
945 case SETVAL:
946 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700947 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return err;
949 case IPC_RMID:
950 case IPC_SET:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700951 mutex_lock(&sem_ids(ns).mutex);
952 err = semctl_down(ns,semid,semnum,cmd,version,arg);
953 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return err;
955 default:
956 return -EINVAL;
957 }
958}
959
960static inline void lock_semundo(void)
961{
962 struct sem_undo_list *undo_list;
963
964 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200965 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 spin_lock(&undo_list->lock);
967}
968
969/* This code has an interaction with copy_semundo().
970 * Consider; two tasks are sharing the undo_list. task1
971 * acquires the undo_list lock in lock_semundo(). If task2 now
972 * exits before task1 releases the lock (by calling
973 * unlock_semundo()), then task1 will never call spin_unlock().
974 * This leave the sem_undo_list in a locked state. If task1 now creats task3
975 * and once again shares the sem_undo_list, the sem_undo_list will still be
976 * locked, and future SEM_UNDO operations will deadlock. This case is
977 * dealt with in copy_semundo() by having it reinitialize the spin lock when
978 * the refcnt goes from 1 to 2.
979 */
980static inline void unlock_semundo(void)
981{
982 struct sem_undo_list *undo_list;
983
984 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200985 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 spin_unlock(&undo_list->lock);
987}
988
989
990/* If the task doesn't already have a undo_list, then allocate one
991 * here. We guarantee there is only one thread using this undo list,
992 * and current is THE ONE
993 *
994 * If this allocation and assignment succeeds, but later
995 * portions of this code fail, there is no need to free the sem_undo_list.
996 * Just let it stay associated with the task, and it'll be freed later
997 * at exit time.
998 *
999 * This can block, so callers must hold no locks.
1000 */
1001static inline int get_undo_list(struct sem_undo_list **undo_listp)
1002{
1003 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 undo_list = current->sysvsem.undo_list;
1006 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001007 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (undo_list == NULL)
1009 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001010 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 atomic_set(&undo_list->refcnt, 1);
1012 current->sysvsem.undo_list = undo_list;
1013 }
1014 *undo_listp = undo_list;
1015 return 0;
1016}
1017
1018static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1019{
1020 struct sem_undo **last, *un;
1021
1022 last = &ulp->proc_list;
1023 un = *last;
1024 while(un != NULL) {
1025 if(un->semid==semid)
1026 break;
1027 if(un->semid==-1) {
1028 *last=un->proc_next;
1029 kfree(un);
1030 } else {
1031 last=&un->proc_next;
1032 }
1033 un=*last;
1034 }
1035 return un;
1036}
1037
Kirill Korotaeve3893532006-10-02 02:18:22 -07001038static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct sem_array *sma;
1041 struct sem_undo_list *ulp;
1042 struct sem_undo *un, *new;
1043 int nsems;
1044 int error;
1045
1046 error = get_undo_list(&ulp);
1047 if (error)
1048 return ERR_PTR(error);
1049
1050 lock_semundo();
1051 un = lookup_undo(ulp, semid);
1052 unlock_semundo();
1053 if (likely(un!=NULL))
1054 goto out;
1055
1056 /* no undo structure around - allocate one. */
Kirill Korotaeve3893532006-10-02 02:18:22 -07001057 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 un = ERR_PTR(-EINVAL);
1059 if(sma==NULL)
1060 goto out;
1061 un = ERR_PTR(-EIDRM);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001062 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 sem_unlock(sma);
1064 goto out;
1065 }
1066 nsems = sma->sem_nsems;
1067 ipc_rcu_getref(sma);
1068 sem_unlock(sma);
1069
Burman Yan4668edc2006-12-06 20:38:51 -08001070 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!new) {
1072 ipc_lock_by_ptr(&sma->sem_perm);
1073 ipc_rcu_putref(sma);
1074 sem_unlock(sma);
1075 return ERR_PTR(-ENOMEM);
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 new->semadj = (short *) &new[1];
1078 new->semid = semid;
1079
1080 lock_semundo();
1081 un = lookup_undo(ulp, semid);
1082 if (un) {
1083 unlock_semundo();
1084 kfree(new);
1085 ipc_lock_by_ptr(&sma->sem_perm);
1086 ipc_rcu_putref(sma);
1087 sem_unlock(sma);
1088 goto out;
1089 }
1090 ipc_lock_by_ptr(&sma->sem_perm);
1091 ipc_rcu_putref(sma);
1092 if (sma->sem_perm.deleted) {
1093 sem_unlock(sma);
1094 unlock_semundo();
1095 kfree(new);
1096 un = ERR_PTR(-EIDRM);
1097 goto out;
1098 }
1099 new->proc_next = ulp->proc_list;
1100 ulp->proc_list = new;
1101 new->id_next = sma->undo;
1102 sma->undo = new;
1103 sem_unlock(sma);
1104 un = new;
1105 unlock_semundo();
1106out:
1107 return un;
1108}
1109
1110asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1111 unsigned nsops, const struct timespec __user *timeout)
1112{
1113 int error = -EINVAL;
1114 struct sem_array *sma;
1115 struct sembuf fast_sops[SEMOPM_FAST];
1116 struct sembuf* sops = fast_sops, *sop;
1117 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001118 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 struct sem_queue queue;
1120 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001121 struct ipc_namespace *ns;
1122
1123 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if (nsops < 1 || semid < 0)
1126 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001127 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -E2BIG;
1129 if(nsops > SEMOPM_FAST) {
1130 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1131 if(sops==NULL)
1132 return -ENOMEM;
1133 }
1134 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1135 error=-EFAULT;
1136 goto out_free;
1137 }
1138 if (timeout) {
1139 struct timespec _timeout;
1140 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1141 error = -EFAULT;
1142 goto out_free;
1143 }
1144 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1145 _timeout.tv_nsec >= 1000000000L) {
1146 error = -EINVAL;
1147 goto out_free;
1148 }
1149 jiffies_left = timespec_to_jiffies(&_timeout);
1150 }
1151 max = 0;
1152 for (sop = sops; sop < sops + nsops; sop++) {
1153 if (sop->sem_num >= max)
1154 max = sop->sem_num;
1155 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001156 undos = 1;
1157 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 alter = 1;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161retry_undos:
1162 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001163 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (IS_ERR(un)) {
1165 error = PTR_ERR(un);
1166 goto out_free;
1167 }
1168 } else
1169 un = NULL;
1170
Kirill Korotaeve3893532006-10-02 02:18:22 -07001171 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 error=-EINVAL;
1173 if(sma==NULL)
1174 goto out_free;
1175 error = -EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001176 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto out_unlock_free;
1178 /*
1179 * semid identifies are not unique - find_undo may have
1180 * allocated an undo structure, it was invalidated by an RMID
1181 * and now a new array with received the same id. Check and retry.
1182 */
1183 if (un && un->semid == -1) {
1184 sem_unlock(sma);
1185 goto retry_undos;
1186 }
1187 error = -EFBIG;
1188 if (max >= sma->sem_nsems)
1189 goto out_unlock_free;
1190
1191 error = -EACCES;
1192 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1193 goto out_unlock_free;
1194
1195 error = security_sem_semop(sma, sops, nsops, alter);
1196 if (error)
1197 goto out_unlock_free;
1198
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001199 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (error <= 0) {
1201 if (alter && error == 0)
1202 update_queue (sma);
1203 goto out_unlock_free;
1204 }
1205
1206 /* We need to sleep on this operation, so we put the current
1207 * task into the pending queue and go to sleep.
1208 */
1209
1210 queue.sma = sma;
1211 queue.sops = sops;
1212 queue.nsops = nsops;
1213 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001214 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 queue.id = semid;
1216 queue.alter = alter;
1217 if (alter)
1218 append_to_queue(sma ,&queue);
1219 else
1220 prepend_to_queue(sma ,&queue);
1221
1222 queue.status = -EINTR;
1223 queue.sleeper = current;
1224 current->state = TASK_INTERRUPTIBLE;
1225 sem_unlock(sma);
1226
1227 if (timeout)
1228 jiffies_left = schedule_timeout(jiffies_left);
1229 else
1230 schedule();
1231
1232 error = queue.status;
1233 while(unlikely(error == IN_WAKEUP)) {
1234 cpu_relax();
1235 error = queue.status;
1236 }
1237
1238 if (error != -EINTR) {
1239 /* fast path: update_queue already obtained all requested
1240 * resources */
1241 goto out_free;
1242 }
1243
Kirill Korotaeve3893532006-10-02 02:18:22 -07001244 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if(sma==NULL) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001246 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 error = -EIDRM;
1248 goto out_free;
1249 }
1250
1251 /*
1252 * If queue.status != -EINTR we are woken up by another process
1253 */
1254 error = queue.status;
1255 if (error != -EINTR) {
1256 goto out_unlock_free;
1257 }
1258
1259 /*
1260 * If an interrupt occurred we have to clean up the queue
1261 */
1262 if (timeout && jiffies_left == 0)
1263 error = -EAGAIN;
1264 remove_from_queue(sma,&queue);
1265 goto out_unlock_free;
1266
1267out_unlock_free:
1268 sem_unlock(sma);
1269out_free:
1270 if(sops != fast_sops)
1271 kfree(sops);
1272 return error;
1273}
1274
1275asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1276{
1277 return sys_semtimedop(semid, tsops, nsops, NULL);
1278}
1279
1280/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1281 * parent and child tasks.
1282 *
1283 * See the notes above unlock_semundo() regarding the spin_lock_init()
1284 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1285 * because of the reasoning in the comment above unlock_semundo.
1286 */
1287
1288int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1289{
1290 struct sem_undo_list *undo_list;
1291 int error;
1292
1293 if (clone_flags & CLONE_SYSVSEM) {
1294 error = get_undo_list(&undo_list);
1295 if (error)
1296 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 atomic_inc(&undo_list->refcnt);
1298 tsk->sysvsem.undo_list = undo_list;
1299 } else
1300 tsk->sysvsem.undo_list = NULL;
1301
1302 return 0;
1303}
1304
1305/*
1306 * add semadj values to semaphores, free undo structures.
1307 * undo structures are not freed when semaphore arrays are destroyed
1308 * so some of them may be out of date.
1309 * IMPLEMENTATION NOTE: There is some confusion over whether the
1310 * set of adjustments that needs to be done should be done in an atomic
1311 * manner or not. That is, if we are attempting to decrement the semval
1312 * should we queue up and wait until we can do so legally?
1313 * The original implementation attempted to do this (queue and wait).
1314 * The current implementation does not do so. The POSIX standard
1315 * and SVID should be consulted to determine what behavior is mandated.
1316 */
1317void exit_sem(struct task_struct *tsk)
1318{
1319 struct sem_undo_list *undo_list;
1320 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001321 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 undo_list = tsk->sysvsem.undo_list;
1324 if (!undo_list)
1325 return;
1326
1327 if (!atomic_dec_and_test(&undo_list->refcnt))
1328 return;
1329
Kirill Korotaeve3893532006-10-02 02:18:22 -07001330 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* There's no need to hold the semundo list lock, as current
1332 * is the last task exiting for this undo list.
1333 */
1334 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1335 struct sem_array *sma;
1336 int nsems, i;
1337 struct sem_undo *un, **unp;
1338 int semid;
1339
1340 semid = u->semid;
1341
1342 if(semid == -1)
1343 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001344 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (sma == NULL)
1346 continue;
1347
1348 if (u->semid == -1)
1349 goto next_entry;
1350
Kirill Korotaeve3893532006-10-02 02:18:22 -07001351 BUG_ON(sem_checkid(ns,sma,u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* remove u from the sma->undo list */
1354 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1355 if (u == un)
1356 goto found;
1357 }
1358 printk ("exit_sem undo list error id=%d\n", u->semid);
1359 goto next_entry;
1360found:
1361 *unp = un->id_next;
1362 /* perform adjustments registered in u */
1363 nsems = sma->sem_nsems;
1364 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001365 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001367 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 /*
1369 * Range checks of the new semaphore value,
1370 * not defined by sus:
1371 * - Some unices ignore the undo entirely
1372 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1373 * - some cap the value (e.g. FreeBSD caps
1374 * at 0, but doesn't enforce SEMVMX)
1375 *
1376 * Linux caps the semaphore value, both at 0
1377 * and at SEMVMX.
1378 *
1379 * Manfred <manfred@colorfullife.com>
1380 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001381 if (semaphore->semval < 0)
1382 semaphore->semval = 0;
1383 if (semaphore->semval > SEMVMX)
1384 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001385 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387 }
1388 sma->sem_otime = get_seconds();
1389 /* maybe some queued-up processes were waiting for this */
1390 update_queue(sma);
1391next_entry:
1392 sem_unlock(sma);
1393 }
1394 kfree(undo_list);
1395}
1396
1397#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001398static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Mike Waychison19b49462005-09-06 15:17:10 -07001400 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Mike Waychison19b49462005-09-06 15:17:10 -07001402 return seq_printf(s,
1403 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1404 sma->sem_perm.key,
1405 sma->sem_id,
1406 sma->sem_perm.mode,
1407 sma->sem_nsems,
1408 sma->sem_perm.uid,
1409 sma->sem_perm.gid,
1410 sma->sem_perm.cuid,
1411 sma->sem_perm.cgid,
1412 sma->sem_otime,
1413 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415#endif