blob: f92a2565d12b0c7fc896f7c73a581e734d6eb225 [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)
Kirill Korotaeve3893532006-10-02 02:18:22 -070093#define sem_checkid(ns, sma, semid) \
94 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
95#define sem_buildid(ns, id, seq) \
96 ipc_buildid(&sem_ids(ns), id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Kirill Korotaeve3893532006-10-02 02:18:22 -070098static struct ipc_ids init_sem_ids;
99
100static int newary(struct ipc_namespace *, key_t, int, int);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700101static void freeary(struct ipc_namespace *, struct sem_array *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700103static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
106#define SEMMSL_FAST 256 /* 512 bytes on stack */
107#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
108
109/*
110 * linked list protection:
111 * sem_undo.id_next,
112 * sem_array.sem_pending{,last},
113 * sem_array.sem_undo: sem_lock() for read/write
114 * sem_undo.proc_next: only "current" is allowed to read/write that field.
115 *
116 */
117
Kirill Korotaeve3893532006-10-02 02:18:22 -0700118#define sc_semmsl sem_ctls[0]
119#define sc_semmns sem_ctls[1]
120#define sc_semopm sem_ctls[2]
121#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -0700123static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700124{
125 ns->ids[IPC_SEM_IDS] = ids;
126 ns->sc_semmsl = SEMMSL;
127 ns->sc_semmns = SEMMNS;
128 ns->sc_semopm = SEMOPM;
129 ns->sc_semmni = SEMMNI;
130 ns->used_sems = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700131 ipc_init_ids(ids);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700132}
133
Kirill Korotaeve3893532006-10-02 02:18:22 -0700134int sem_init_ns(struct ipc_namespace *ns)
135{
136 struct ipc_ids *ids;
137
138 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
139 if (ids == NULL)
140 return -ENOMEM;
141
142 __sem_init_ns(ns, ids);
143 return 0;
144}
145
146void sem_exit_ns(struct ipc_namespace *ns)
147{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700148 struct sem_array *sma;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700149 int next_id;
150 int total, in_use;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700151
152 mutex_lock(&sem_ids(ns).mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700153
154 in_use = sem_ids(ns).in_use;
155
156 for (total = 0, next_id = 0; total < in_use; next_id++) {
157 sma = idr_find(&sem_ids(ns).ipcs_idr, next_id);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700158 if (sma == NULL)
159 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700160 ipc_lock_by_ptr(&sma->sem_perm);
161 freeary(ns, sma);
162 total++;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700163 }
164 mutex_unlock(&sem_ids(ns).mutex);
165
166 kfree(ns->ids[IPC_SEM_IDS]);
167 ns->ids[IPC_SEM_IDS] = NULL;
168}
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
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700178static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
179{
180 ipc_rmid(&sem_ids(ns), &s->sem_perm);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
184 * Lockless wakeup algorithm:
185 * Without the check/retry algorithm a lockless wakeup is possible:
186 * - queue.status is initialized to -EINTR before blocking.
187 * - wakeup is performed by
188 * * unlinking the queue entry from sma->sem_pending
189 * * setting queue.status to IN_WAKEUP
190 * This is the notification for the blocked thread that a
191 * result value is imminent.
192 * * call wake_up_process
193 * * set queue.status to the final value.
194 * - the previously blocked thread checks queue.status:
195 * * if it's IN_WAKEUP, then it must wait until the value changes
196 * * if it's not -EINTR, then the operation was completed by
197 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800198 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * * otherwise it must acquire the spinlock and check what's up.
200 *
201 * The two-stage algorithm is necessary to protect against the following
202 * races:
203 * - if queue.status is set after wake_up_process, then the woken up idle
204 * thread could race forward and try (and fail) to acquire sma->lock
205 * before update_queue had a chance to set queue.status
206 * - if queue.status is written before wake_up_process and if the
207 * blocked process is woken up by a signal between writing
208 * queue.status and the wake_up_process, then the woken up
209 * process could return from semtimedop and die by calling
210 * sys_exit before wake_up_process is called. Then wake_up_process
211 * will oops, because the task structure is already invalid.
212 * (yes, this happened on s390 with sysv msg).
213 *
214 */
215#define IN_WAKEUP 1
216
Kirill Korotaeve3893532006-10-02 02:18:22 -0700217static int newary (struct ipc_namespace *ns, key_t key, int nsems, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int id;
220 int retval;
221 struct sem_array *sma;
222 int size;
223
224 if (!nsems)
225 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700226 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -ENOSPC;
228
229 size = sizeof (*sma) + nsems * sizeof (struct sem);
230 sma = ipc_rcu_alloc(size);
231 if (!sma) {
232 return -ENOMEM;
233 }
234 memset (sma, 0, size);
235
236 sma->sem_perm.mode = (semflg & S_IRWXUGO);
237 sma->sem_perm.key = key;
238
239 sma->sem_perm.security = NULL;
240 retval = security_sem_alloc(sma);
241 if (retval) {
242 ipc_rcu_putref(sma);
243 return retval;
244 }
245
Kirill Korotaeve3893532006-10-02 02:18:22 -0700246 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if(id == -1) {
248 security_sem_free(sma);
249 ipc_rcu_putref(sma);
250 return -ENOSPC;
251 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700252 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700254 sma->sem_perm.id = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 sma->sem_base = (struct sem *) &sma[1];
256 /* sma->sem_pending = NULL; */
257 sma->sem_pending_last = &sma->sem_pending;
258 /* sma->undo = NULL; */
259 sma->sem_nsems = nsems;
260 sma->sem_ctime = get_seconds();
261 sem_unlock(sma);
262
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700263 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266asmlinkage long sys_semget (key_t key, int nsems, int semflg)
267{
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct sem_array *sma;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700270 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Kirill Korotaeve3893532006-10-02 02:18:22 -0700272 ns = current->nsproxy->ipc_ns;
273
274 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700276
277 err = idr_pre_get(&sem_ids(ns).ipcs_idr, GFP_KERNEL);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (key == IPC_PRIVATE) {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700280 if (!err)
281 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 else {
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700283 mutex_lock(&sem_ids(ns).mutex);
284 err = newary(ns, key, nsems, semflg);
285 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700287 } else {
288 mutex_lock(&sem_ids(ns).mutex);
289 sma = (struct sem_array *) ipc_findkey(&sem_ids(ns), key);
290 if (sma == NULL) {
291 /* key not used */
292 if (!(semflg & IPC_CREAT))
293 err = -ENOENT;
294 else if (!err)
295 err = -ENOMEM;
296 else
297 err = newary(ns, key, nsems, semflg);
298 } else {
299 /* sma has been locked by ipc_findkey() */
300
301 if (semflg & IPC_CREAT && semflg & IPC_EXCL)
302 err = -EEXIST;
303 else {
304 if (nsems > sma->sem_nsems)
305 err = -EINVAL;
306 else if (ipcperms(&sma->sem_perm, semflg))
307 err = -EACCES;
308 else {
309 err = security_sem_associate(sma,
310 semflg);
311 if (!err)
312 err = sma->sem_perm.id;
313 }
314 }
315 sem_unlock(sma);
316 }
317 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return err;
321}
322
323/* Manage the doubly linked list sma->sem_pending as a FIFO:
324 * insert new queue elements at the tail sma->sem_pending_last.
325 */
326static inline void append_to_queue (struct sem_array * sma,
327 struct sem_queue * q)
328{
329 *(q->prev = sma->sem_pending_last) = q;
330 *(sma->sem_pending_last = &q->next) = NULL;
331}
332
333static inline void prepend_to_queue (struct sem_array * sma,
334 struct sem_queue * q)
335{
336 q->next = sma->sem_pending;
337 *(q->prev = &sma->sem_pending) = q;
338 if (q->next)
339 q->next->prev = &q->next;
340 else /* sma->sem_pending_last == &sma->sem_pending */
341 sma->sem_pending_last = &q->next;
342}
343
344static inline void remove_from_queue (struct sem_array * sma,
345 struct sem_queue * q)
346{
347 *(q->prev) = q->next;
348 if (q->next)
349 q->next->prev = q->prev;
350 else /* sma->sem_pending_last == &q->next */
351 sma->sem_pending_last = q->prev;
352 q->prev = NULL; /* mark as removed */
353}
354
355/*
356 * Determine whether a sequence of semaphore operations would succeed
357 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
358 */
359
360static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
361 int nsops, struct sem_undo *un, int pid)
362{
363 int result, sem_op;
364 struct sembuf *sop;
365 struct sem * curr;
366
367 for (sop = sops; sop < sops + nsops; sop++) {
368 curr = sma->sem_base + sop->sem_num;
369 sem_op = sop->sem_op;
370 result = curr->semval;
371
372 if (!sem_op && result)
373 goto would_block;
374
375 result += sem_op;
376 if (result < 0)
377 goto would_block;
378 if (result > SEMVMX)
379 goto out_of_range;
380 if (sop->sem_flg & SEM_UNDO) {
381 int undo = un->semadj[sop->sem_num] - sem_op;
382 /*
383 * Exceeding the undo range is an error.
384 */
385 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
386 goto out_of_range;
387 }
388 curr->semval = result;
389 }
390
391 sop--;
392 while (sop >= sops) {
393 sma->sem_base[sop->sem_num].sempid = pid;
394 if (sop->sem_flg & SEM_UNDO)
395 un->semadj[sop->sem_num] -= sop->sem_op;
396 sop--;
397 }
398
399 sma->sem_otime = get_seconds();
400 return 0;
401
402out_of_range:
403 result = -ERANGE;
404 goto undo;
405
406would_block:
407 if (sop->sem_flg & IPC_NOWAIT)
408 result = -EAGAIN;
409 else
410 result = 1;
411
412undo:
413 sop--;
414 while (sop >= sops) {
415 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
416 sop--;
417 }
418
419 return result;
420}
421
422/* Go through the pending queue for the indicated semaphore
423 * looking for tasks that can be completed.
424 */
425static void update_queue (struct sem_array * sma)
426{
427 int error;
428 struct sem_queue * q;
429
430 q = sma->sem_pending;
431 while(q) {
432 error = try_atomic_semop(sma, q->sops, q->nsops,
433 q->undo, q->pid);
434
435 /* Does q->sleeper still need to sleep? */
436 if (error <= 0) {
437 struct sem_queue *n;
438 remove_from_queue(sma,q);
439 q->status = IN_WAKEUP;
440 /*
441 * Continue scanning. The next operation
442 * that must be checked depends on the type of the
443 * completed operation:
444 * - if the operation modified the array, then
445 * restart from the head of the queue and
446 * check for threads that might be waiting
447 * for semaphore values to become 0.
448 * - if the operation didn't modify the array,
449 * then just continue.
450 */
451 if (q->alter)
452 n = sma->sem_pending;
453 else
454 n = q->next;
455 wake_up_process(q->sleeper);
456 /* hands-off: q will disappear immediately after
457 * writing q->status.
458 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800459 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 q->status = error;
461 q = n;
462 } else {
463 q = q->next;
464 }
465 }
466}
467
468/* The following counts are associated to each semaphore:
469 * semncnt number of tasks waiting on semval being nonzero
470 * semzcnt number of tasks waiting on semval being zero
471 * This model assumes that a task waits on exactly one semaphore.
472 * Since semaphore operations are to be performed atomically, tasks actually
473 * wait on a whole sequence of semaphores simultaneously.
474 * The counts we return here are a rough approximation, but still
475 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
476 */
477static int count_semncnt (struct sem_array * sma, ushort semnum)
478{
479 int semncnt;
480 struct sem_queue * q;
481
482 semncnt = 0;
483 for (q = sma->sem_pending; q; q = q->next) {
484 struct sembuf * sops = q->sops;
485 int nsops = q->nsops;
486 int i;
487 for (i = 0; i < nsops; i++)
488 if (sops[i].sem_num == semnum
489 && (sops[i].sem_op < 0)
490 && !(sops[i].sem_flg & IPC_NOWAIT))
491 semncnt++;
492 }
493 return semncnt;
494}
495static int count_semzcnt (struct sem_array * sma, ushort semnum)
496{
497 int semzcnt;
498 struct sem_queue * q;
499
500 semzcnt = 0;
501 for (q = sma->sem_pending; q; q = q->next) {
502 struct sembuf * sops = q->sops;
503 int nsops = q->nsops;
504 int i;
505 for (i = 0; i < nsops; i++)
506 if (sops[i].sem_num == semnum
507 && (sops[i].sem_op == 0)
508 && !(sops[i].sem_flg & IPC_NOWAIT))
509 semzcnt++;
510 }
511 return semzcnt;
512}
513
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800514/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
515 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * on exit.
517 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700518static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct sem_undo *un;
521 struct sem_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 /* Invalidate the existing undo structures for this semaphore set.
524 * (They will be freed without any further action in exit_sem()
525 * or during the next semop.)
526 */
527 for (un = sma->undo; un; un = un->id_next)
528 un->semid = -1;
529
530 /* Wake up all pending processes and let them fail with EIDRM. */
531 q = sma->sem_pending;
532 while(q) {
533 struct sem_queue *n;
534 /* lazy remove_from_queue: we are killing the whole queue */
535 q->prev = NULL;
536 n = q->next;
537 q->status = IN_WAKEUP;
538 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100539 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 q->status = -EIDRM; /* hands-off q */
541 q = n;
542 }
543
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700544 /* Remove the semaphore set from the IDR */
545 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 sem_unlock(sma);
547
Kirill Korotaeve3893532006-10-02 02:18:22 -0700548 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 security_sem_free(sma);
550 ipc_rcu_putref(sma);
551}
552
553static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
554{
555 switch(version) {
556 case IPC_64:
557 return copy_to_user(buf, in, sizeof(*in));
558 case IPC_OLD:
559 {
560 struct semid_ds out;
561
562 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
563
564 out.sem_otime = in->sem_otime;
565 out.sem_ctime = in->sem_ctime;
566 out.sem_nsems = in->sem_nsems;
567
568 return copy_to_user(buf, &out, sizeof(out));
569 }
570 default:
571 return -EINVAL;
572 }
573}
574
Kirill Korotaeve3893532006-10-02 02:18:22 -0700575static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
576 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int err = -EINVAL;
579 struct sem_array *sma;
580
581 switch(cmd) {
582 case IPC_INFO:
583 case SEM_INFO:
584 {
585 struct seminfo seminfo;
586 int max_id;
587
588 err = security_sem_semctl(NULL, cmd);
589 if (err)
590 return err;
591
592 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700593 seminfo.semmni = ns->sc_semmni;
594 seminfo.semmns = ns->sc_semmns;
595 seminfo.semmsl = ns->sc_semmsl;
596 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 seminfo.semvmx = SEMVMX;
598 seminfo.semmnu = SEMMNU;
599 seminfo.semmap = SEMMAP;
600 seminfo.semume = SEMUME;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700601 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700603 seminfo.semusz = sem_ids(ns).in_use;
604 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 } else {
606 seminfo.semusz = SEMUSZ;
607 seminfo.semaem = SEMAEM;
608 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700609 max_id = ipc_get_maxid(&sem_ids(ns));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700610 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
612 return -EFAULT;
613 return (max_id < 0) ? 0: max_id;
614 }
615 case SEM_STAT:
616 {
617 struct semid64_ds tbuf;
618 int id;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 memset(&tbuf,0,sizeof(tbuf));
621
Kirill Korotaeve3893532006-10-02 02:18:22 -0700622 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if(sma == NULL)
624 return -EINVAL;
625
626 err = -EACCES;
627 if (ipcperms (&sma->sem_perm, S_IRUGO))
628 goto out_unlock;
629
630 err = security_sem_semctl(sma, cmd);
631 if (err)
632 goto out_unlock;
633
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700634 id = sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
637 tbuf.sem_otime = sma->sem_otime;
638 tbuf.sem_ctime = sma->sem_ctime;
639 tbuf.sem_nsems = sma->sem_nsems;
640 sem_unlock(sma);
641 if (copy_semid_to_user (arg.buf, &tbuf, version))
642 return -EFAULT;
643 return id;
644 }
645 default:
646 return -EINVAL;
647 }
648 return err;
649out_unlock:
650 sem_unlock(sma);
651 return err;
652}
653
Kirill Korotaeve3893532006-10-02 02:18:22 -0700654static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
655 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 struct sem_array *sma;
658 struct sem* curr;
659 int err;
660 ushort fast_sem_io[SEMMSL_FAST];
661 ushort* sem_io = fast_sem_io;
662 int nsems;
663
Kirill Korotaeve3893532006-10-02 02:18:22 -0700664 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if(sma==NULL)
666 return -EINVAL;
667
668 nsems = sma->sem_nsems;
669
670 err=-EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700671 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto out_unlock;
673
674 err = -EACCES;
675 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
676 goto out_unlock;
677
678 err = security_sem_semctl(sma, cmd);
679 if (err)
680 goto out_unlock;
681
682 err = -EACCES;
683 switch (cmd) {
684 case GETALL:
685 {
686 ushort __user *array = arg.array;
687 int i;
688
689 if(nsems > SEMMSL_FAST) {
690 ipc_rcu_getref(sma);
691 sem_unlock(sma);
692
693 sem_io = ipc_alloc(sizeof(ushort)*nsems);
694 if(sem_io == NULL) {
695 ipc_lock_by_ptr(&sma->sem_perm);
696 ipc_rcu_putref(sma);
697 sem_unlock(sma);
698 return -ENOMEM;
699 }
700
701 ipc_lock_by_ptr(&sma->sem_perm);
702 ipc_rcu_putref(sma);
703 if (sma->sem_perm.deleted) {
704 sem_unlock(sma);
705 err = -EIDRM;
706 goto out_free;
707 }
708 }
709
710 for (i = 0; i < sma->sem_nsems; i++)
711 sem_io[i] = sma->sem_base[i].semval;
712 sem_unlock(sma);
713 err = 0;
714 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
715 err = -EFAULT;
716 goto out_free;
717 }
718 case SETALL:
719 {
720 int i;
721 struct sem_undo *un;
722
723 ipc_rcu_getref(sma);
724 sem_unlock(sma);
725
726 if(nsems > SEMMSL_FAST) {
727 sem_io = ipc_alloc(sizeof(ushort)*nsems);
728 if(sem_io == NULL) {
729 ipc_lock_by_ptr(&sma->sem_perm);
730 ipc_rcu_putref(sma);
731 sem_unlock(sma);
732 return -ENOMEM;
733 }
734 }
735
736 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
737 ipc_lock_by_ptr(&sma->sem_perm);
738 ipc_rcu_putref(sma);
739 sem_unlock(sma);
740 err = -EFAULT;
741 goto out_free;
742 }
743
744 for (i = 0; i < nsems; i++) {
745 if (sem_io[i] > SEMVMX) {
746 ipc_lock_by_ptr(&sma->sem_perm);
747 ipc_rcu_putref(sma);
748 sem_unlock(sma);
749 err = -ERANGE;
750 goto out_free;
751 }
752 }
753 ipc_lock_by_ptr(&sma->sem_perm);
754 ipc_rcu_putref(sma);
755 if (sma->sem_perm.deleted) {
756 sem_unlock(sma);
757 err = -EIDRM;
758 goto out_free;
759 }
760
761 for (i = 0; i < nsems; i++)
762 sma->sem_base[i].semval = sem_io[i];
763 for (un = sma->undo; un; un = un->id_next)
764 for (i = 0; i < nsems; i++)
765 un->semadj[i] = 0;
766 sma->sem_ctime = get_seconds();
767 /* maybe some queued-up processes were waiting for this */
768 update_queue(sma);
769 err = 0;
770 goto out_unlock;
771 }
772 case IPC_STAT:
773 {
774 struct semid64_ds tbuf;
775 memset(&tbuf,0,sizeof(tbuf));
776 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
777 tbuf.sem_otime = sma->sem_otime;
778 tbuf.sem_ctime = sma->sem_ctime;
779 tbuf.sem_nsems = sma->sem_nsems;
780 sem_unlock(sma);
781 if (copy_semid_to_user (arg.buf, &tbuf, version))
782 return -EFAULT;
783 return 0;
784 }
785 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
786 }
787 err = -EINVAL;
788 if(semnum < 0 || semnum >= nsems)
789 goto out_unlock;
790
791 curr = &sma->sem_base[semnum];
792
793 switch (cmd) {
794 case GETVAL:
795 err = curr->semval;
796 goto out_unlock;
797 case GETPID:
798 err = curr->sempid;
799 goto out_unlock;
800 case GETNCNT:
801 err = count_semncnt(sma,semnum);
802 goto out_unlock;
803 case GETZCNT:
804 err = count_semzcnt(sma,semnum);
805 goto out_unlock;
806 case SETVAL:
807 {
808 int val = arg.val;
809 struct sem_undo *un;
810 err = -ERANGE;
811 if (val > SEMVMX || val < 0)
812 goto out_unlock;
813
814 for (un = sma->undo; un; un = un->id_next)
815 un->semadj[semnum] = 0;
816 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700817 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 sma->sem_ctime = get_seconds();
819 /* maybe some queued-up processes were waiting for this */
820 update_queue(sma);
821 err = 0;
822 goto out_unlock;
823 }
824 }
825out_unlock:
826 sem_unlock(sma);
827out_free:
828 if(sem_io != fast_sem_io)
829 ipc_free(sem_io, sizeof(ushort)*nsems);
830 return err;
831}
832
833struct sem_setbuf {
834 uid_t uid;
835 gid_t gid;
836 mode_t mode;
837};
838
839static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
840{
841 switch(version) {
842 case IPC_64:
843 {
844 struct semid64_ds tbuf;
845
846 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
847 return -EFAULT;
848
849 out->uid = tbuf.sem_perm.uid;
850 out->gid = tbuf.sem_perm.gid;
851 out->mode = tbuf.sem_perm.mode;
852
853 return 0;
854 }
855 case IPC_OLD:
856 {
857 struct semid_ds tbuf_old;
858
859 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
860 return -EFAULT;
861
862 out->uid = tbuf_old.sem_perm.uid;
863 out->gid = tbuf_old.sem_perm.gid;
864 out->mode = tbuf_old.sem_perm.mode;
865
866 return 0;
867 }
868 default:
869 return -EINVAL;
870 }
871}
872
Kirill Korotaeve3893532006-10-02 02:18:22 -0700873static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
874 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 struct sem_array *sma;
877 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400878 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct kern_ipc_perm *ipcp;
880
881 if(cmd == IPC_SET) {
882 if(copy_semid_from_user (&setbuf, arg.buf, version))
883 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700885 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if(sma==NULL)
887 return -EINVAL;
888
Kirill Korotaeve3893532006-10-02 02:18:22 -0700889 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 err=-EIDRM;
891 goto out_unlock;
892 }
893 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400894
895 err = audit_ipc_obj(ipcp);
896 if (err)
897 goto out_unlock;
898
Linda Knippersac032212006-05-16 22:03:48 -0400899 if (cmd == IPC_SET) {
900 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
901 if (err)
902 goto out_unlock;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (current->euid != ipcp->cuid &&
905 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
906 err=-EPERM;
907 goto out_unlock;
908 }
909
910 err = security_sem_semctl(sma, cmd);
911 if (err)
912 goto out_unlock;
913
914 switch(cmd){
915 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700916 freeary(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 err = 0;
918 break;
919 case IPC_SET:
920 ipcp->uid = setbuf.uid;
921 ipcp->gid = setbuf.gid;
922 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
923 | (setbuf.mode & S_IRWXUGO);
924 sma->sem_ctime = get_seconds();
925 sem_unlock(sma);
926 err = 0;
927 break;
928 default:
929 sem_unlock(sma);
930 err = -EINVAL;
931 break;
932 }
933 return err;
934
935out_unlock:
936 sem_unlock(sma);
937 return err;
938}
939
940asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
941{
942 int err = -EINVAL;
943 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700944 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (semid < 0)
947 return -EINVAL;
948
949 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700950 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 switch(cmd) {
953 case IPC_INFO:
954 case SEM_INFO:
955 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700956 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return err;
958 case GETALL:
959 case GETVAL:
960 case GETPID:
961 case GETNCNT:
962 case GETZCNT:
963 case IPC_STAT:
964 case SETVAL:
965 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700966 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return err;
968 case IPC_RMID:
969 case IPC_SET:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700970 mutex_lock(&sem_ids(ns).mutex);
971 err = semctl_down(ns,semid,semnum,cmd,version,arg);
972 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return err;
974 default:
975 return -EINVAL;
976 }
977}
978
979static inline void lock_semundo(void)
980{
981 struct sem_undo_list *undo_list;
982
983 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200984 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_lock(&undo_list->lock);
986}
987
988/* This code has an interaction with copy_semundo().
989 * Consider; two tasks are sharing the undo_list. task1
990 * acquires the undo_list lock in lock_semundo(). If task2 now
991 * exits before task1 releases the lock (by calling
992 * unlock_semundo()), then task1 will never call spin_unlock().
993 * This leave the sem_undo_list in a locked state. If task1 now creats task3
994 * and once again shares the sem_undo_list, the sem_undo_list will still be
995 * locked, and future SEM_UNDO operations will deadlock. This case is
996 * dealt with in copy_semundo() by having it reinitialize the spin lock when
997 * the refcnt goes from 1 to 2.
998 */
999static inline void unlock_semundo(void)
1000{
1001 struct sem_undo_list *undo_list;
1002
1003 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001004 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 spin_unlock(&undo_list->lock);
1006}
1007
1008
1009/* If the task doesn't already have a undo_list, then allocate one
1010 * here. We guarantee there is only one thread using this undo list,
1011 * and current is THE ONE
1012 *
1013 * If this allocation and assignment succeeds, but later
1014 * portions of this code fail, there is no need to free the sem_undo_list.
1015 * Just let it stay associated with the task, and it'll be freed later
1016 * at exit time.
1017 *
1018 * This can block, so callers must hold no locks.
1019 */
1020static inline int get_undo_list(struct sem_undo_list **undo_listp)
1021{
1022 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 undo_list = current->sysvsem.undo_list;
1025 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001026 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (undo_list == NULL)
1028 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001029 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 atomic_set(&undo_list->refcnt, 1);
1031 current->sysvsem.undo_list = undo_list;
1032 }
1033 *undo_listp = undo_list;
1034 return 0;
1035}
1036
1037static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1038{
1039 struct sem_undo **last, *un;
1040
1041 last = &ulp->proc_list;
1042 un = *last;
1043 while(un != NULL) {
1044 if(un->semid==semid)
1045 break;
1046 if(un->semid==-1) {
1047 *last=un->proc_next;
1048 kfree(un);
1049 } else {
1050 last=&un->proc_next;
1051 }
1052 un=*last;
1053 }
1054 return un;
1055}
1056
Kirill Korotaeve3893532006-10-02 02:18:22 -07001057static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
1059 struct sem_array *sma;
1060 struct sem_undo_list *ulp;
1061 struct sem_undo *un, *new;
1062 int nsems;
1063 int error;
1064
1065 error = get_undo_list(&ulp);
1066 if (error)
1067 return ERR_PTR(error);
1068
1069 lock_semundo();
1070 un = lookup_undo(ulp, semid);
1071 unlock_semundo();
1072 if (likely(un!=NULL))
1073 goto out;
1074
1075 /* no undo structure around - allocate one. */
Kirill Korotaeve3893532006-10-02 02:18:22 -07001076 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 un = ERR_PTR(-EINVAL);
1078 if(sma==NULL)
1079 goto out;
1080 un = ERR_PTR(-EIDRM);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001081 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 sem_unlock(sma);
1083 goto out;
1084 }
1085 nsems = sma->sem_nsems;
1086 ipc_rcu_getref(sma);
1087 sem_unlock(sma);
1088
Burman Yan4668edc2006-12-06 20:38:51 -08001089 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!new) {
1091 ipc_lock_by_ptr(&sma->sem_perm);
1092 ipc_rcu_putref(sma);
1093 sem_unlock(sma);
1094 return ERR_PTR(-ENOMEM);
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 new->semadj = (short *) &new[1];
1097 new->semid = semid;
1098
1099 lock_semundo();
1100 un = lookup_undo(ulp, semid);
1101 if (un) {
1102 unlock_semundo();
1103 kfree(new);
1104 ipc_lock_by_ptr(&sma->sem_perm);
1105 ipc_rcu_putref(sma);
1106 sem_unlock(sma);
1107 goto out;
1108 }
1109 ipc_lock_by_ptr(&sma->sem_perm);
1110 ipc_rcu_putref(sma);
1111 if (sma->sem_perm.deleted) {
1112 sem_unlock(sma);
1113 unlock_semundo();
1114 kfree(new);
1115 un = ERR_PTR(-EIDRM);
1116 goto out;
1117 }
1118 new->proc_next = ulp->proc_list;
1119 ulp->proc_list = new;
1120 new->id_next = sma->undo;
1121 sma->undo = new;
1122 sem_unlock(sma);
1123 un = new;
1124 unlock_semundo();
1125out:
1126 return un;
1127}
1128
1129asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1130 unsigned nsops, const struct timespec __user *timeout)
1131{
1132 int error = -EINVAL;
1133 struct sem_array *sma;
1134 struct sembuf fast_sops[SEMOPM_FAST];
1135 struct sembuf* sops = fast_sops, *sop;
1136 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001137 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct sem_queue queue;
1139 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001140 struct ipc_namespace *ns;
1141
1142 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 if (nsops < 1 || semid < 0)
1145 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001146 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -E2BIG;
1148 if(nsops > SEMOPM_FAST) {
1149 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1150 if(sops==NULL)
1151 return -ENOMEM;
1152 }
1153 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1154 error=-EFAULT;
1155 goto out_free;
1156 }
1157 if (timeout) {
1158 struct timespec _timeout;
1159 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1160 error = -EFAULT;
1161 goto out_free;
1162 }
1163 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1164 _timeout.tv_nsec >= 1000000000L) {
1165 error = -EINVAL;
1166 goto out_free;
1167 }
1168 jiffies_left = timespec_to_jiffies(&_timeout);
1169 }
1170 max = 0;
1171 for (sop = sops; sop < sops + nsops; sop++) {
1172 if (sop->sem_num >= max)
1173 max = sop->sem_num;
1174 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001175 undos = 1;
1176 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 alter = 1;
1178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180retry_undos:
1181 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001182 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (IS_ERR(un)) {
1184 error = PTR_ERR(un);
1185 goto out_free;
1186 }
1187 } else
1188 un = NULL;
1189
Kirill Korotaeve3893532006-10-02 02:18:22 -07001190 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 error=-EINVAL;
1192 if(sma==NULL)
1193 goto out_free;
1194 error = -EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001195 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 goto out_unlock_free;
1197 /*
1198 * semid identifies are not unique - find_undo may have
1199 * allocated an undo structure, it was invalidated by an RMID
1200 * and now a new array with received the same id. Check and retry.
1201 */
1202 if (un && un->semid == -1) {
1203 sem_unlock(sma);
1204 goto retry_undos;
1205 }
1206 error = -EFBIG;
1207 if (max >= sma->sem_nsems)
1208 goto out_unlock_free;
1209
1210 error = -EACCES;
1211 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1212 goto out_unlock_free;
1213
1214 error = security_sem_semop(sma, sops, nsops, alter);
1215 if (error)
1216 goto out_unlock_free;
1217
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001218 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (error <= 0) {
1220 if (alter && error == 0)
1221 update_queue (sma);
1222 goto out_unlock_free;
1223 }
1224
1225 /* We need to sleep on this operation, so we put the current
1226 * task into the pending queue and go to sleep.
1227 */
1228
1229 queue.sma = sma;
1230 queue.sops = sops;
1231 queue.nsops = nsops;
1232 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001233 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 queue.id = semid;
1235 queue.alter = alter;
1236 if (alter)
1237 append_to_queue(sma ,&queue);
1238 else
1239 prepend_to_queue(sma ,&queue);
1240
1241 queue.status = -EINTR;
1242 queue.sleeper = current;
1243 current->state = TASK_INTERRUPTIBLE;
1244 sem_unlock(sma);
1245
1246 if (timeout)
1247 jiffies_left = schedule_timeout(jiffies_left);
1248 else
1249 schedule();
1250
1251 error = queue.status;
1252 while(unlikely(error == IN_WAKEUP)) {
1253 cpu_relax();
1254 error = queue.status;
1255 }
1256
1257 if (error != -EINTR) {
1258 /* fast path: update_queue already obtained all requested
1259 * resources */
1260 goto out_free;
1261 }
1262
Kirill Korotaeve3893532006-10-02 02:18:22 -07001263 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if(sma==NULL) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001265 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 error = -EIDRM;
1267 goto out_free;
1268 }
1269
1270 /*
1271 * If queue.status != -EINTR we are woken up by another process
1272 */
1273 error = queue.status;
1274 if (error != -EINTR) {
1275 goto out_unlock_free;
1276 }
1277
1278 /*
1279 * If an interrupt occurred we have to clean up the queue
1280 */
1281 if (timeout && jiffies_left == 0)
1282 error = -EAGAIN;
1283 remove_from_queue(sma,&queue);
1284 goto out_unlock_free;
1285
1286out_unlock_free:
1287 sem_unlock(sma);
1288out_free:
1289 if(sops != fast_sops)
1290 kfree(sops);
1291 return error;
1292}
1293
1294asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1295{
1296 return sys_semtimedop(semid, tsops, nsops, NULL);
1297}
1298
1299/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1300 * parent and child tasks.
1301 *
1302 * See the notes above unlock_semundo() regarding the spin_lock_init()
1303 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1304 * because of the reasoning in the comment above unlock_semundo.
1305 */
1306
1307int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1308{
1309 struct sem_undo_list *undo_list;
1310 int error;
1311
1312 if (clone_flags & CLONE_SYSVSEM) {
1313 error = get_undo_list(&undo_list);
1314 if (error)
1315 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 atomic_inc(&undo_list->refcnt);
1317 tsk->sysvsem.undo_list = undo_list;
1318 } else
1319 tsk->sysvsem.undo_list = NULL;
1320
1321 return 0;
1322}
1323
1324/*
1325 * add semadj values to semaphores, free undo structures.
1326 * undo structures are not freed when semaphore arrays are destroyed
1327 * so some of them may be out of date.
1328 * IMPLEMENTATION NOTE: There is some confusion over whether the
1329 * set of adjustments that needs to be done should be done in an atomic
1330 * manner or not. That is, if we are attempting to decrement the semval
1331 * should we queue up and wait until we can do so legally?
1332 * The original implementation attempted to do this (queue and wait).
1333 * The current implementation does not do so. The POSIX standard
1334 * and SVID should be consulted to determine what behavior is mandated.
1335 */
1336void exit_sem(struct task_struct *tsk)
1337{
1338 struct sem_undo_list *undo_list;
1339 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001340 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 undo_list = tsk->sysvsem.undo_list;
1343 if (!undo_list)
1344 return;
1345
1346 if (!atomic_dec_and_test(&undo_list->refcnt))
1347 return;
1348
Kirill Korotaeve3893532006-10-02 02:18:22 -07001349 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 /* There's no need to hold the semundo list lock, as current
1351 * is the last task exiting for this undo list.
1352 */
1353 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1354 struct sem_array *sma;
1355 int nsems, i;
1356 struct sem_undo *un, **unp;
1357 int semid;
1358
1359 semid = u->semid;
1360
1361 if(semid == -1)
1362 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001363 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (sma == NULL)
1365 continue;
1366
1367 if (u->semid == -1)
1368 goto next_entry;
1369
Kirill Korotaeve3893532006-10-02 02:18:22 -07001370 BUG_ON(sem_checkid(ns,sma,u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* remove u from the sma->undo list */
1373 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1374 if (u == un)
1375 goto found;
1376 }
1377 printk ("exit_sem undo list error id=%d\n", u->semid);
1378 goto next_entry;
1379found:
1380 *unp = un->id_next;
1381 /* perform adjustments registered in u */
1382 nsems = sma->sem_nsems;
1383 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001384 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001386 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /*
1388 * Range checks of the new semaphore value,
1389 * not defined by sus:
1390 * - Some unices ignore the undo entirely
1391 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1392 * - some cap the value (e.g. FreeBSD caps
1393 * at 0, but doesn't enforce SEMVMX)
1394 *
1395 * Linux caps the semaphore value, both at 0
1396 * and at SEMVMX.
1397 *
1398 * Manfred <manfred@colorfullife.com>
1399 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001400 if (semaphore->semval < 0)
1401 semaphore->semval = 0;
1402 if (semaphore->semval > SEMVMX)
1403 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001404 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 }
1407 sma->sem_otime = get_seconds();
1408 /* maybe some queued-up processes were waiting for this */
1409 update_queue(sma);
1410next_entry:
1411 sem_unlock(sma);
1412 }
1413 kfree(undo_list);
1414}
1415
1416#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001417static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Mike Waychison19b49462005-09-06 15:17:10 -07001419 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Mike Waychison19b49462005-09-06 15:17:10 -07001421 return seq_printf(s,
1422 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1423 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001424 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001425 sma->sem_perm.mode,
1426 sma->sem_nsems,
1427 sma->sem_perm.uid,
1428 sma->sem_perm.gid,
1429 sma->sem_perm.cuid,
1430 sma->sem_perm.cgid,
1431 sma->sem_otime,
1432 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434#endif