blob: d3e12efd55cb39e6cdce7cd48ac73833100f60cf [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>
78#include <linux/smp_lock.h>
79#include <linux/security.h>
80#include <linux/syscalls.h>
81#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080082#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070083#include <linux/seq_file.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080084#include <linux/mutex.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070085#include <linux/nsproxy.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
Kirill Korotaeve3893532006-10-02 02:18:22 -070090#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_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
93#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
94#define sem_rmid(ns, id) ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
95#define sem_checkid(ns, sma, semid) \
96 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
97#define sem_buildid(ns, id, seq) \
98 ipc_buildid(&sem_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Kirill Korotaeve3893532006-10-02 02:18:22 -0700100static struct ipc_ids init_sem_ids;
101
102static int newary(struct ipc_namespace *, key_t, int, int);
103static void freeary(struct ipc_namespace *ns, struct sem_array *sma, int id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700105static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#endif
107
108#define SEMMSL_FAST 256 /* 512 bytes on stack */
109#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
110
111/*
112 * linked list protection:
113 * sem_undo.id_next,
114 * sem_array.sem_pending{,last},
115 * sem_array.sem_undo: sem_lock() for read/write
116 * sem_undo.proc_next: only "current" is allowed to read/write that field.
117 *
118 */
119
Kirill Korotaeve3893532006-10-02 02:18:22 -0700120#define sc_semmsl sem_ctls[0]
121#define sc_semmns sem_ctls[1]
122#define sc_semopm sem_ctls[2]
123#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Kirill Korotaeve3893532006-10-02 02:18:22 -0700125static void __ipc_init __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
126{
127 ns->ids[IPC_SEM_IDS] = ids;
128 ns->sc_semmsl = SEMMSL;
129 ns->sc_semmns = SEMMNS;
130 ns->sc_semopm = SEMOPM;
131 ns->sc_semmni = SEMMNI;
132 ns->used_sems = 0;
133 ipc_init_ids(ids, ns->sc_semmni);
134}
135
136#ifdef CONFIG_IPC_NS
137int sem_init_ns(struct ipc_namespace *ns)
138{
139 struct ipc_ids *ids;
140
141 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
142 if (ids == NULL)
143 return -ENOMEM;
144
145 __sem_init_ns(ns, ids);
146 return 0;
147}
148
149void sem_exit_ns(struct ipc_namespace *ns)
150{
151 int i;
152 struct sem_array *sma;
153
154 mutex_lock(&sem_ids(ns).mutex);
155 for (i = 0; i <= sem_ids(ns).max_id; i++) {
156 sma = sem_lock(ns, i);
157 if (sma == NULL)
158 continue;
159
160 freeary(ns, sma, i);
161 }
162 mutex_unlock(&sem_ids(ns).mutex);
163
Pavel Emelianovc7e12b82006-11-02 22:07:03 -0800164 ipc_fini_ids(ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700165 kfree(ns->ids[IPC_SEM_IDS]);
166 ns->ids[IPC_SEM_IDS] = NULL;
167}
168#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170void __init sem_init (void)
171{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700172 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700173 ipc_init_proc_interface("sysvipc/sem",
174 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700175 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/*
179 * Lockless wakeup algorithm:
180 * Without the check/retry algorithm a lockless wakeup is possible:
181 * - queue.status is initialized to -EINTR before blocking.
182 * - wakeup is performed by
183 * * unlinking the queue entry from sma->sem_pending
184 * * setting queue.status to IN_WAKEUP
185 * This is the notification for the blocked thread that a
186 * result value is imminent.
187 * * call wake_up_process
188 * * set queue.status to the final value.
189 * - the previously blocked thread checks queue.status:
190 * * if it's IN_WAKEUP, then it must wait until the value changes
191 * * if it's not -EINTR, then the operation was completed by
192 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800193 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * * otherwise it must acquire the spinlock and check what's up.
195 *
196 * The two-stage algorithm is necessary to protect against the following
197 * races:
198 * - if queue.status is set after wake_up_process, then the woken up idle
199 * thread could race forward and try (and fail) to acquire sma->lock
200 * before update_queue had a chance to set queue.status
201 * - if queue.status is written before wake_up_process and if the
202 * blocked process is woken up by a signal between writing
203 * queue.status and the wake_up_process, then the woken up
204 * process could return from semtimedop and die by calling
205 * sys_exit before wake_up_process is called. Then wake_up_process
206 * will oops, because the task structure is already invalid.
207 * (yes, this happened on s390 with sysv msg).
208 *
209 */
210#define IN_WAKEUP 1
211
Kirill Korotaeve3893532006-10-02 02:18:22 -0700212static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 int id;
215 int retval;
216 struct sem_array *sma;
217 int size;
218
219 if (!nsems)
220 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700221 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -ENOSPC;
223
224 size = sizeof (*sma) + nsems * sizeof (struct sem);
225 sma = ipc_rcu_alloc(size);
226 if (!sma) {
227 return -ENOMEM;
228 }
229 memset (sma, 0, size);
230
231 sma->sem_perm.mode = (semflg & S_IRWXUGO);
232 sma->sem_perm.key = key;
233
234 sma->sem_perm.security = NULL;
235 retval = security_sem_alloc(sma);
236 if (retval) {
237 ipc_rcu_putref(sma);
238 return retval;
239 }
240
Kirill Korotaeve3893532006-10-02 02:18:22 -0700241 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if(id == -1) {
243 security_sem_free(sma);
244 ipc_rcu_putref(sma);
245 return -ENOSPC;
246 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700247 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Kirill Korotaeve3893532006-10-02 02:18:22 -0700249 sma->sem_id = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 sma->sem_base = (struct sem *) &sma[1];
251 /* sma->sem_pending = NULL; */
252 sma->sem_pending_last = &sma->sem_pending;
253 /* sma->undo = NULL; */
254 sma->sem_nsems = nsems;
255 sma->sem_ctime = get_seconds();
256 sem_unlock(sma);
257
Mike Waychison19b49462005-09-06 15:17:10 -0700258 return sma->sem_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
261asmlinkage long sys_semget (key_t key, int nsems, int semflg)
262{
263 int id, err = -EINVAL;
264 struct sem_array *sma;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700265 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Kirill Korotaeve3893532006-10-02 02:18:22 -0700267 ns = current->nsproxy->ipc_ns;
268
269 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700271 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (key == IPC_PRIVATE) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700274 err = newary(ns, key, nsems, semflg);
275 } else if ((id = ipc_findkey(&sem_ids(ns), key)) == -1) { /* key not used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (!(semflg & IPC_CREAT))
277 err = -ENOENT;
278 else
Kirill Korotaeve3893532006-10-02 02:18:22 -0700279 err = newary(ns, key, nsems, semflg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
281 err = -EEXIST;
282 } else {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700283 sma = sem_lock(ns, id);
Eric Sesterhenn27315c92006-03-26 18:28:38 +0200284 BUG_ON(sma==NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (nsems > sma->sem_nsems)
286 err = -EINVAL;
287 else if (ipcperms(&sma->sem_perm, semflg))
288 err = -EACCES;
289 else {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700290 int semid = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 err = security_sem_associate(sma, semflg);
292 if (!err)
293 err = semid;
294 }
295 sem_unlock(sma);
296 }
297
Kirill Korotaeve3893532006-10-02 02:18:22 -0700298 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return err;
300}
301
302/* Manage the doubly linked list sma->sem_pending as a FIFO:
303 * insert new queue elements at the tail sma->sem_pending_last.
304 */
305static inline void append_to_queue (struct sem_array * sma,
306 struct sem_queue * q)
307{
308 *(q->prev = sma->sem_pending_last) = q;
309 *(sma->sem_pending_last = &q->next) = NULL;
310}
311
312static inline void prepend_to_queue (struct sem_array * sma,
313 struct sem_queue * q)
314{
315 q->next = sma->sem_pending;
316 *(q->prev = &sma->sem_pending) = q;
317 if (q->next)
318 q->next->prev = &q->next;
319 else /* sma->sem_pending_last == &sma->sem_pending */
320 sma->sem_pending_last = &q->next;
321}
322
323static inline void remove_from_queue (struct sem_array * sma,
324 struct sem_queue * q)
325{
326 *(q->prev) = q->next;
327 if (q->next)
328 q->next->prev = q->prev;
329 else /* sma->sem_pending_last == &q->next */
330 sma->sem_pending_last = q->prev;
331 q->prev = NULL; /* mark as removed */
332}
333
334/*
335 * Determine whether a sequence of semaphore operations would succeed
336 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
337 */
338
339static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
340 int nsops, struct sem_undo *un, int pid)
341{
342 int result, sem_op;
343 struct sembuf *sop;
344 struct sem * curr;
345
346 for (sop = sops; sop < sops + nsops; sop++) {
347 curr = sma->sem_base + sop->sem_num;
348 sem_op = sop->sem_op;
349 result = curr->semval;
350
351 if (!sem_op && result)
352 goto would_block;
353
354 result += sem_op;
355 if (result < 0)
356 goto would_block;
357 if (result > SEMVMX)
358 goto out_of_range;
359 if (sop->sem_flg & SEM_UNDO) {
360 int undo = un->semadj[sop->sem_num] - sem_op;
361 /*
362 * Exceeding the undo range is an error.
363 */
364 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
365 goto out_of_range;
366 }
367 curr->semval = result;
368 }
369
370 sop--;
371 while (sop >= sops) {
372 sma->sem_base[sop->sem_num].sempid = pid;
373 if (sop->sem_flg & SEM_UNDO)
374 un->semadj[sop->sem_num] -= sop->sem_op;
375 sop--;
376 }
377
378 sma->sem_otime = get_seconds();
379 return 0;
380
381out_of_range:
382 result = -ERANGE;
383 goto undo;
384
385would_block:
386 if (sop->sem_flg & IPC_NOWAIT)
387 result = -EAGAIN;
388 else
389 result = 1;
390
391undo:
392 sop--;
393 while (sop >= sops) {
394 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
395 sop--;
396 }
397
398 return result;
399}
400
401/* Go through the pending queue for the indicated semaphore
402 * looking for tasks that can be completed.
403 */
404static void update_queue (struct sem_array * sma)
405{
406 int error;
407 struct sem_queue * q;
408
409 q = sma->sem_pending;
410 while(q) {
411 error = try_atomic_semop(sma, q->sops, q->nsops,
412 q->undo, q->pid);
413
414 /* Does q->sleeper still need to sleep? */
415 if (error <= 0) {
416 struct sem_queue *n;
417 remove_from_queue(sma,q);
418 q->status = IN_WAKEUP;
419 /*
420 * Continue scanning. The next operation
421 * that must be checked depends on the type of the
422 * completed operation:
423 * - if the operation modified the array, then
424 * restart from the head of the queue and
425 * check for threads that might be waiting
426 * for semaphore values to become 0.
427 * - if the operation didn't modify the array,
428 * then just continue.
429 */
430 if (q->alter)
431 n = sma->sem_pending;
432 else
433 n = q->next;
434 wake_up_process(q->sleeper);
435 /* hands-off: q will disappear immediately after
436 * writing q->status.
437 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800438 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 q->status = error;
440 q = n;
441 } else {
442 q = q->next;
443 }
444 }
445}
446
447/* The following counts are associated to each semaphore:
448 * semncnt number of tasks waiting on semval being nonzero
449 * semzcnt number of tasks waiting on semval being zero
450 * This model assumes that a task waits on exactly one semaphore.
451 * Since semaphore operations are to be performed atomically, tasks actually
452 * wait on a whole sequence of semaphores simultaneously.
453 * The counts we return here are a rough approximation, but still
454 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
455 */
456static int count_semncnt (struct sem_array * sma, ushort semnum)
457{
458 int semncnt;
459 struct sem_queue * q;
460
461 semncnt = 0;
462 for (q = sma->sem_pending; q; q = q->next) {
463 struct sembuf * sops = q->sops;
464 int nsops = q->nsops;
465 int i;
466 for (i = 0; i < nsops; i++)
467 if (sops[i].sem_num == semnum
468 && (sops[i].sem_op < 0)
469 && !(sops[i].sem_flg & IPC_NOWAIT))
470 semncnt++;
471 }
472 return semncnt;
473}
474static int count_semzcnt (struct sem_array * sma, ushort semnum)
475{
476 int semzcnt;
477 struct sem_queue * q;
478
479 semzcnt = 0;
480 for (q = sma->sem_pending; q; q = q->next) {
481 struct sembuf * sops = q->sops;
482 int nsops = q->nsops;
483 int i;
484 for (i = 0; i < nsops; i++)
485 if (sops[i].sem_num == semnum
486 && (sops[i].sem_op == 0)
487 && !(sops[i].sem_flg & IPC_NOWAIT))
488 semzcnt++;
489 }
490 return semzcnt;
491}
492
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800493/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
494 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * on exit.
496 */
Kirill Korotaeve3893532006-10-02 02:18:22 -0700497static void freeary (struct ipc_namespace *ns, struct sem_array *sma, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct sem_undo *un;
500 struct sem_queue *q;
501 int size;
502
503 /* Invalidate the existing undo structures for this semaphore set.
504 * (They will be freed without any further action in exit_sem()
505 * or during the next semop.)
506 */
507 for (un = sma->undo; un; un = un->id_next)
508 un->semid = -1;
509
510 /* Wake up all pending processes and let them fail with EIDRM. */
511 q = sma->sem_pending;
512 while(q) {
513 struct sem_queue *n;
514 /* lazy remove_from_queue: we are killing the whole queue */
515 q->prev = NULL;
516 n = q->next;
517 q->status = IN_WAKEUP;
518 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100519 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 q->status = -EIDRM; /* hands-off q */
521 q = n;
522 }
523
524 /* Remove the semaphore set from the ID array*/
Kirill Korotaeve3893532006-10-02 02:18:22 -0700525 sma = sem_rmid(ns, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 sem_unlock(sma);
527
Kirill Korotaeve3893532006-10-02 02:18:22 -0700528 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
530 security_sem_free(sma);
531 ipc_rcu_putref(sma);
532}
533
534static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
535{
536 switch(version) {
537 case IPC_64:
538 return copy_to_user(buf, in, sizeof(*in));
539 case IPC_OLD:
540 {
541 struct semid_ds out;
542
543 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
544
545 out.sem_otime = in->sem_otime;
546 out.sem_ctime = in->sem_ctime;
547 out.sem_nsems = in->sem_nsems;
548
549 return copy_to_user(buf, &out, sizeof(out));
550 }
551 default:
552 return -EINVAL;
553 }
554}
555
Kirill Korotaeve3893532006-10-02 02:18:22 -0700556static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
557 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 int err = -EINVAL;
560 struct sem_array *sma;
561
562 switch(cmd) {
563 case IPC_INFO:
564 case SEM_INFO:
565 {
566 struct seminfo seminfo;
567 int max_id;
568
569 err = security_sem_semctl(NULL, cmd);
570 if (err)
571 return err;
572
573 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700574 seminfo.semmni = ns->sc_semmni;
575 seminfo.semmns = ns->sc_semmns;
576 seminfo.semmsl = ns->sc_semmsl;
577 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 seminfo.semvmx = SEMVMX;
579 seminfo.semmnu = SEMMNU;
580 seminfo.semmap = SEMMAP;
581 seminfo.semume = SEMUME;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700582 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700584 seminfo.semusz = sem_ids(ns).in_use;
585 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 } else {
587 seminfo.semusz = SEMUSZ;
588 seminfo.semaem = SEMAEM;
589 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700590 max_id = sem_ids(ns).max_id;
591 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
593 return -EFAULT;
594 return (max_id < 0) ? 0: max_id;
595 }
596 case SEM_STAT:
597 {
598 struct semid64_ds tbuf;
599 int id;
600
Kirill Korotaeve3893532006-10-02 02:18:22 -0700601 if(semid >= sem_ids(ns).entries->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
603
604 memset(&tbuf,0,sizeof(tbuf));
605
Kirill Korotaeve3893532006-10-02 02:18:22 -0700606 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if(sma == NULL)
608 return -EINVAL;
609
610 err = -EACCES;
611 if (ipcperms (&sma->sem_perm, S_IRUGO))
612 goto out_unlock;
613
614 err = security_sem_semctl(sma, cmd);
615 if (err)
616 goto out_unlock;
617
Kirill Korotaeve3893532006-10-02 02:18:22 -0700618 id = sem_buildid(ns, semid, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
621 tbuf.sem_otime = sma->sem_otime;
622 tbuf.sem_ctime = sma->sem_ctime;
623 tbuf.sem_nsems = sma->sem_nsems;
624 sem_unlock(sma);
625 if (copy_semid_to_user (arg.buf, &tbuf, version))
626 return -EFAULT;
627 return id;
628 }
629 default:
630 return -EINVAL;
631 }
632 return err;
633out_unlock:
634 sem_unlock(sma);
635 return err;
636}
637
Kirill Korotaeve3893532006-10-02 02:18:22 -0700638static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
639 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 struct sem_array *sma;
642 struct sem* curr;
643 int err;
644 ushort fast_sem_io[SEMMSL_FAST];
645 ushort* sem_io = fast_sem_io;
646 int nsems;
647
Kirill Korotaeve3893532006-10-02 02:18:22 -0700648 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if(sma==NULL)
650 return -EINVAL;
651
652 nsems = sma->sem_nsems;
653
654 err=-EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700655 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto out_unlock;
657
658 err = -EACCES;
659 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
660 goto out_unlock;
661
662 err = security_sem_semctl(sma, cmd);
663 if (err)
664 goto out_unlock;
665
666 err = -EACCES;
667 switch (cmd) {
668 case GETALL:
669 {
670 ushort __user *array = arg.array;
671 int i;
672
673 if(nsems > SEMMSL_FAST) {
674 ipc_rcu_getref(sma);
675 sem_unlock(sma);
676
677 sem_io = ipc_alloc(sizeof(ushort)*nsems);
678 if(sem_io == NULL) {
679 ipc_lock_by_ptr(&sma->sem_perm);
680 ipc_rcu_putref(sma);
681 sem_unlock(sma);
682 return -ENOMEM;
683 }
684
685 ipc_lock_by_ptr(&sma->sem_perm);
686 ipc_rcu_putref(sma);
687 if (sma->sem_perm.deleted) {
688 sem_unlock(sma);
689 err = -EIDRM;
690 goto out_free;
691 }
692 }
693
694 for (i = 0; i < sma->sem_nsems; i++)
695 sem_io[i] = sma->sem_base[i].semval;
696 sem_unlock(sma);
697 err = 0;
698 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
699 err = -EFAULT;
700 goto out_free;
701 }
702 case SETALL:
703 {
704 int i;
705 struct sem_undo *un;
706
707 ipc_rcu_getref(sma);
708 sem_unlock(sma);
709
710 if(nsems > SEMMSL_FAST) {
711 sem_io = ipc_alloc(sizeof(ushort)*nsems);
712 if(sem_io == NULL) {
713 ipc_lock_by_ptr(&sma->sem_perm);
714 ipc_rcu_putref(sma);
715 sem_unlock(sma);
716 return -ENOMEM;
717 }
718 }
719
720 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
721 ipc_lock_by_ptr(&sma->sem_perm);
722 ipc_rcu_putref(sma);
723 sem_unlock(sma);
724 err = -EFAULT;
725 goto out_free;
726 }
727
728 for (i = 0; i < nsems; i++) {
729 if (sem_io[i] > SEMVMX) {
730 ipc_lock_by_ptr(&sma->sem_perm);
731 ipc_rcu_putref(sma);
732 sem_unlock(sma);
733 err = -ERANGE;
734 goto out_free;
735 }
736 }
737 ipc_lock_by_ptr(&sma->sem_perm);
738 ipc_rcu_putref(sma);
739 if (sma->sem_perm.deleted) {
740 sem_unlock(sma);
741 err = -EIDRM;
742 goto out_free;
743 }
744
745 for (i = 0; i < nsems; i++)
746 sma->sem_base[i].semval = sem_io[i];
747 for (un = sma->undo; un; un = un->id_next)
748 for (i = 0; i < nsems; i++)
749 un->semadj[i] = 0;
750 sma->sem_ctime = get_seconds();
751 /* maybe some queued-up processes were waiting for this */
752 update_queue(sma);
753 err = 0;
754 goto out_unlock;
755 }
756 case IPC_STAT:
757 {
758 struct semid64_ds tbuf;
759 memset(&tbuf,0,sizeof(tbuf));
760 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
761 tbuf.sem_otime = sma->sem_otime;
762 tbuf.sem_ctime = sma->sem_ctime;
763 tbuf.sem_nsems = sma->sem_nsems;
764 sem_unlock(sma);
765 if (copy_semid_to_user (arg.buf, &tbuf, version))
766 return -EFAULT;
767 return 0;
768 }
769 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
770 }
771 err = -EINVAL;
772 if(semnum < 0 || semnum >= nsems)
773 goto out_unlock;
774
775 curr = &sma->sem_base[semnum];
776
777 switch (cmd) {
778 case GETVAL:
779 err = curr->semval;
780 goto out_unlock;
781 case GETPID:
782 err = curr->sempid;
783 goto out_unlock;
784 case GETNCNT:
785 err = count_semncnt(sma,semnum);
786 goto out_unlock;
787 case GETZCNT:
788 err = count_semzcnt(sma,semnum);
789 goto out_unlock;
790 case SETVAL:
791 {
792 int val = arg.val;
793 struct sem_undo *un;
794 err = -ERANGE;
795 if (val > SEMVMX || val < 0)
796 goto out_unlock;
797
798 for (un = sma->undo; un; un = un->id_next)
799 un->semadj[semnum] = 0;
800 curr->semval = val;
801 curr->sempid = current->tgid;
802 sma->sem_ctime = get_seconds();
803 /* maybe some queued-up processes were waiting for this */
804 update_queue(sma);
805 err = 0;
806 goto out_unlock;
807 }
808 }
809out_unlock:
810 sem_unlock(sma);
811out_free:
812 if(sem_io != fast_sem_io)
813 ipc_free(sem_io, sizeof(ushort)*nsems);
814 return err;
815}
816
817struct sem_setbuf {
818 uid_t uid;
819 gid_t gid;
820 mode_t mode;
821};
822
823static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
824{
825 switch(version) {
826 case IPC_64:
827 {
828 struct semid64_ds tbuf;
829
830 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
831 return -EFAULT;
832
833 out->uid = tbuf.sem_perm.uid;
834 out->gid = tbuf.sem_perm.gid;
835 out->mode = tbuf.sem_perm.mode;
836
837 return 0;
838 }
839 case IPC_OLD:
840 {
841 struct semid_ds tbuf_old;
842
843 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
844 return -EFAULT;
845
846 out->uid = tbuf_old.sem_perm.uid;
847 out->gid = tbuf_old.sem_perm.gid;
848 out->mode = tbuf_old.sem_perm.mode;
849
850 return 0;
851 }
852 default:
853 return -EINVAL;
854 }
855}
856
Kirill Korotaeve3893532006-10-02 02:18:22 -0700857static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
858 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct sem_array *sma;
861 int err;
862 struct sem_setbuf setbuf;
863 struct kern_ipc_perm *ipcp;
864
865 if(cmd == IPC_SET) {
866 if(copy_semid_from_user (&setbuf, arg.buf, version))
867 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700869 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if(sma==NULL)
871 return -EINVAL;
872
Kirill Korotaeve3893532006-10-02 02:18:22 -0700873 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 err=-EIDRM;
875 goto out_unlock;
876 }
877 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400878
879 err = audit_ipc_obj(ipcp);
880 if (err)
881 goto out_unlock;
882
Linda Knippersac032212006-05-16 22:03:48 -0400883 if (cmd == IPC_SET) {
884 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
885 if (err)
886 goto out_unlock;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (current->euid != ipcp->cuid &&
889 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
890 err=-EPERM;
891 goto out_unlock;
892 }
893
894 err = security_sem_semctl(sma, cmd);
895 if (err)
896 goto out_unlock;
897
898 switch(cmd){
899 case IPC_RMID:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700900 freeary(ns, sma, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 err = 0;
902 break;
903 case IPC_SET:
904 ipcp->uid = setbuf.uid;
905 ipcp->gid = setbuf.gid;
906 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
907 | (setbuf.mode & S_IRWXUGO);
908 sma->sem_ctime = get_seconds();
909 sem_unlock(sma);
910 err = 0;
911 break;
912 default:
913 sem_unlock(sma);
914 err = -EINVAL;
915 break;
916 }
917 return err;
918
919out_unlock:
920 sem_unlock(sma);
921 return err;
922}
923
924asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
925{
926 int err = -EINVAL;
927 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700928 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (semid < 0)
931 return -EINVAL;
932
933 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700934 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 switch(cmd) {
937 case IPC_INFO:
938 case SEM_INFO:
939 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700940 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return err;
942 case GETALL:
943 case GETVAL:
944 case GETPID:
945 case GETNCNT:
946 case GETZCNT:
947 case IPC_STAT:
948 case SETVAL:
949 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700950 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return err;
952 case IPC_RMID:
953 case IPC_SET:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700954 mutex_lock(&sem_ids(ns).mutex);
955 err = semctl_down(ns,semid,semnum,cmd,version,arg);
956 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return err;
958 default:
959 return -EINVAL;
960 }
961}
962
963static inline void lock_semundo(void)
964{
965 struct sem_undo_list *undo_list;
966
967 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200968 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 spin_lock(&undo_list->lock);
970}
971
972/* This code has an interaction with copy_semundo().
973 * Consider; two tasks are sharing the undo_list. task1
974 * acquires the undo_list lock in lock_semundo(). If task2 now
975 * exits before task1 releases the lock (by calling
976 * unlock_semundo()), then task1 will never call spin_unlock().
977 * This leave the sem_undo_list in a locked state. If task1 now creats task3
978 * and once again shares the sem_undo_list, the sem_undo_list will still be
979 * locked, and future SEM_UNDO operations will deadlock. This case is
980 * dealt with in copy_semundo() by having it reinitialize the spin lock when
981 * the refcnt goes from 1 to 2.
982 */
983static inline void unlock_semundo(void)
984{
985 struct sem_undo_list *undo_list;
986
987 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200988 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 spin_unlock(&undo_list->lock);
990}
991
992
993/* If the task doesn't already have a undo_list, then allocate one
994 * here. We guarantee there is only one thread using this undo list,
995 * and current is THE ONE
996 *
997 * If this allocation and assignment succeeds, but later
998 * portions of this code fail, there is no need to free the sem_undo_list.
999 * Just let it stay associated with the task, and it'll be freed later
1000 * at exit time.
1001 *
1002 * This can block, so callers must hold no locks.
1003 */
1004static inline int get_undo_list(struct sem_undo_list **undo_listp)
1005{
1006 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 undo_list = current->sysvsem.undo_list;
1009 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001010 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (undo_list == NULL)
1012 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001013 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 atomic_set(&undo_list->refcnt, 1);
1015 current->sysvsem.undo_list = undo_list;
1016 }
1017 *undo_listp = undo_list;
1018 return 0;
1019}
1020
1021static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1022{
1023 struct sem_undo **last, *un;
1024
1025 last = &ulp->proc_list;
1026 un = *last;
1027 while(un != NULL) {
1028 if(un->semid==semid)
1029 break;
1030 if(un->semid==-1) {
1031 *last=un->proc_next;
1032 kfree(un);
1033 } else {
1034 last=&un->proc_next;
1035 }
1036 un=*last;
1037 }
1038 return un;
1039}
1040
Kirill Korotaeve3893532006-10-02 02:18:22 -07001041static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct sem_array *sma;
1044 struct sem_undo_list *ulp;
1045 struct sem_undo *un, *new;
1046 int nsems;
1047 int error;
1048
1049 error = get_undo_list(&ulp);
1050 if (error)
1051 return ERR_PTR(error);
1052
1053 lock_semundo();
1054 un = lookup_undo(ulp, semid);
1055 unlock_semundo();
1056 if (likely(un!=NULL))
1057 goto out;
1058
1059 /* no undo structure around - allocate one. */
Kirill Korotaeve3893532006-10-02 02:18:22 -07001060 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 un = ERR_PTR(-EINVAL);
1062 if(sma==NULL)
1063 goto out;
1064 un = ERR_PTR(-EIDRM);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001065 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 sem_unlock(sma);
1067 goto out;
1068 }
1069 nsems = sma->sem_nsems;
1070 ipc_rcu_getref(sma);
1071 sem_unlock(sma);
1072
Burman Yan4668edc2006-12-06 20:38:51 -08001073 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (!new) {
1075 ipc_lock_by_ptr(&sma->sem_perm);
1076 ipc_rcu_putref(sma);
1077 sem_unlock(sma);
1078 return ERR_PTR(-ENOMEM);
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 new->semadj = (short *) &new[1];
1081 new->semid = semid;
1082
1083 lock_semundo();
1084 un = lookup_undo(ulp, semid);
1085 if (un) {
1086 unlock_semundo();
1087 kfree(new);
1088 ipc_lock_by_ptr(&sma->sem_perm);
1089 ipc_rcu_putref(sma);
1090 sem_unlock(sma);
1091 goto out;
1092 }
1093 ipc_lock_by_ptr(&sma->sem_perm);
1094 ipc_rcu_putref(sma);
1095 if (sma->sem_perm.deleted) {
1096 sem_unlock(sma);
1097 unlock_semundo();
1098 kfree(new);
1099 un = ERR_PTR(-EIDRM);
1100 goto out;
1101 }
1102 new->proc_next = ulp->proc_list;
1103 ulp->proc_list = new;
1104 new->id_next = sma->undo;
1105 sma->undo = new;
1106 sem_unlock(sma);
1107 un = new;
1108 unlock_semundo();
1109out:
1110 return un;
1111}
1112
1113asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1114 unsigned nsops, const struct timespec __user *timeout)
1115{
1116 int error = -EINVAL;
1117 struct sem_array *sma;
1118 struct sembuf fast_sops[SEMOPM_FAST];
1119 struct sembuf* sops = fast_sops, *sop;
1120 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001121 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct sem_queue queue;
1123 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001124 struct ipc_namespace *ns;
1125
1126 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (nsops < 1 || semid < 0)
1129 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001130 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return -E2BIG;
1132 if(nsops > SEMOPM_FAST) {
1133 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1134 if(sops==NULL)
1135 return -ENOMEM;
1136 }
1137 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1138 error=-EFAULT;
1139 goto out_free;
1140 }
1141 if (timeout) {
1142 struct timespec _timeout;
1143 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1144 error = -EFAULT;
1145 goto out_free;
1146 }
1147 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1148 _timeout.tv_nsec >= 1000000000L) {
1149 error = -EINVAL;
1150 goto out_free;
1151 }
1152 jiffies_left = timespec_to_jiffies(&_timeout);
1153 }
1154 max = 0;
1155 for (sop = sops; sop < sops + nsops; sop++) {
1156 if (sop->sem_num >= max)
1157 max = sop->sem_num;
1158 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001159 undos = 1;
1160 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 alter = 1;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164retry_undos:
1165 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001166 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (IS_ERR(un)) {
1168 error = PTR_ERR(un);
1169 goto out_free;
1170 }
1171 } else
1172 un = NULL;
1173
Kirill Korotaeve3893532006-10-02 02:18:22 -07001174 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 error=-EINVAL;
1176 if(sma==NULL)
1177 goto out_free;
1178 error = -EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001179 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto out_unlock_free;
1181 /*
1182 * semid identifies are not unique - find_undo may have
1183 * allocated an undo structure, it was invalidated by an RMID
1184 * and now a new array with received the same id. Check and retry.
1185 */
1186 if (un && un->semid == -1) {
1187 sem_unlock(sma);
1188 goto retry_undos;
1189 }
1190 error = -EFBIG;
1191 if (max >= sma->sem_nsems)
1192 goto out_unlock_free;
1193
1194 error = -EACCES;
1195 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1196 goto out_unlock_free;
1197
1198 error = security_sem_semop(sma, sops, nsops, alter);
1199 if (error)
1200 goto out_unlock_free;
1201
1202 error = try_atomic_semop (sma, sops, nsops, un, current->tgid);
1203 if (error <= 0) {
1204 if (alter && error == 0)
1205 update_queue (sma);
1206 goto out_unlock_free;
1207 }
1208
1209 /* We need to sleep on this operation, so we put the current
1210 * task into the pending queue and go to sleep.
1211 */
1212
1213 queue.sma = sma;
1214 queue.sops = sops;
1215 queue.nsops = nsops;
1216 queue.undo = un;
1217 queue.pid = current->tgid;
1218 queue.id = semid;
1219 queue.alter = alter;
1220 if (alter)
1221 append_to_queue(sma ,&queue);
1222 else
1223 prepend_to_queue(sma ,&queue);
1224
1225 queue.status = -EINTR;
1226 queue.sleeper = current;
1227 current->state = TASK_INTERRUPTIBLE;
1228 sem_unlock(sma);
1229
1230 if (timeout)
1231 jiffies_left = schedule_timeout(jiffies_left);
1232 else
1233 schedule();
1234
1235 error = queue.status;
1236 while(unlikely(error == IN_WAKEUP)) {
1237 cpu_relax();
1238 error = queue.status;
1239 }
1240
1241 if (error != -EINTR) {
1242 /* fast path: update_queue already obtained all requested
1243 * resources */
1244 goto out_free;
1245 }
1246
Kirill Korotaeve3893532006-10-02 02:18:22 -07001247 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if(sma==NULL) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001249 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 error = -EIDRM;
1251 goto out_free;
1252 }
1253
1254 /*
1255 * If queue.status != -EINTR we are woken up by another process
1256 */
1257 error = queue.status;
1258 if (error != -EINTR) {
1259 goto out_unlock_free;
1260 }
1261
1262 /*
1263 * If an interrupt occurred we have to clean up the queue
1264 */
1265 if (timeout && jiffies_left == 0)
1266 error = -EAGAIN;
1267 remove_from_queue(sma,&queue);
1268 goto out_unlock_free;
1269
1270out_unlock_free:
1271 sem_unlock(sma);
1272out_free:
1273 if(sops != fast_sops)
1274 kfree(sops);
1275 return error;
1276}
1277
1278asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1279{
1280 return sys_semtimedop(semid, tsops, nsops, NULL);
1281}
1282
1283/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1284 * parent and child tasks.
1285 *
1286 * See the notes above unlock_semundo() regarding the spin_lock_init()
1287 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1288 * because of the reasoning in the comment above unlock_semundo.
1289 */
1290
1291int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1292{
1293 struct sem_undo_list *undo_list;
1294 int error;
1295
1296 if (clone_flags & CLONE_SYSVSEM) {
1297 error = get_undo_list(&undo_list);
1298 if (error)
1299 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 atomic_inc(&undo_list->refcnt);
1301 tsk->sysvsem.undo_list = undo_list;
1302 } else
1303 tsk->sysvsem.undo_list = NULL;
1304
1305 return 0;
1306}
1307
1308/*
1309 * add semadj values to semaphores, free undo structures.
1310 * undo structures are not freed when semaphore arrays are destroyed
1311 * so some of them may be out of date.
1312 * IMPLEMENTATION NOTE: There is some confusion over whether the
1313 * set of adjustments that needs to be done should be done in an atomic
1314 * manner or not. That is, if we are attempting to decrement the semval
1315 * should we queue up and wait until we can do so legally?
1316 * The original implementation attempted to do this (queue and wait).
1317 * The current implementation does not do so. The POSIX standard
1318 * and SVID should be consulted to determine what behavior is mandated.
1319 */
1320void exit_sem(struct task_struct *tsk)
1321{
1322 struct sem_undo_list *undo_list;
1323 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001324 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 undo_list = tsk->sysvsem.undo_list;
1327 if (!undo_list)
1328 return;
1329
1330 if (!atomic_dec_and_test(&undo_list->refcnt))
1331 return;
1332
Kirill Korotaeve3893532006-10-02 02:18:22 -07001333 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /* There's no need to hold the semundo list lock, as current
1335 * is the last task exiting for this undo list.
1336 */
1337 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1338 struct sem_array *sma;
1339 int nsems, i;
1340 struct sem_undo *un, **unp;
1341 int semid;
1342
1343 semid = u->semid;
1344
1345 if(semid == -1)
1346 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001347 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (sma == NULL)
1349 continue;
1350
1351 if (u->semid == -1)
1352 goto next_entry;
1353
Kirill Korotaeve3893532006-10-02 02:18:22 -07001354 BUG_ON(sem_checkid(ns,sma,u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /* remove u from the sma->undo list */
1357 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1358 if (u == un)
1359 goto found;
1360 }
1361 printk ("exit_sem undo list error id=%d\n", u->semid);
1362 goto next_entry;
1363found:
1364 *unp = un->id_next;
1365 /* perform adjustments registered in u */
1366 nsems = sma->sem_nsems;
1367 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001368 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001370 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Range checks of the new semaphore value,
1373 * not defined by sus:
1374 * - Some unices ignore the undo entirely
1375 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1376 * - some cap the value (e.g. FreeBSD caps
1377 * at 0, but doesn't enforce SEMVMX)
1378 *
1379 * Linux caps the semaphore value, both at 0
1380 * and at SEMVMX.
1381 *
1382 * Manfred <manfred@colorfullife.com>
1383 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001384 if (semaphore->semval < 0)
1385 semaphore->semval = 0;
1386 if (semaphore->semval > SEMVMX)
1387 semaphore->semval = SEMVMX;
1388 semaphore->sempid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 }
1391 sma->sem_otime = get_seconds();
1392 /* maybe some queued-up processes were waiting for this */
1393 update_queue(sma);
1394next_entry:
1395 sem_unlock(sma);
1396 }
1397 kfree(undo_list);
1398}
1399
1400#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001401static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Mike Waychison19b49462005-09-06 15:17:10 -07001403 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Mike Waychison19b49462005-09-06 15:17:10 -07001405 return seq_printf(s,
1406 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1407 sma->sem_perm.key,
1408 sma->sem_id,
1409 sma->sem_perm.mode,
1410 sma->sem_nsems,
1411 sma->sem_perm.uid,
1412 sma->sem_perm.gid,
1413 sma->sem_perm.cuid,
1414 sma->sem_perm.cgid,
1415 sma->sem_otime,
1416 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418#endif