blob: 6af226af0b90ea538aa4586471111d70cece3a4e [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
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700100static int newary(struct ipc_namespace *, struct ipc_params *);
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
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700217static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 int id;
220 int retval;
221 struct sem_array *sma;
222 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700223 key_t key = params->key;
224 int nsems = params->u.nsems;
225 int semflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 if (!nsems)
228 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700229 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return -ENOSPC;
231
232 size = sizeof (*sma) + nsems * sizeof (struct sem);
233 sma = ipc_rcu_alloc(size);
234 if (!sma) {
235 return -ENOMEM;
236 }
237 memset (sma, 0, size);
238
239 sma->sem_perm.mode = (semflg & S_IRWXUGO);
240 sma->sem_perm.key = key;
241
242 sma->sem_perm.security = NULL;
243 retval = security_sem_alloc(sma);
244 if (retval) {
245 ipc_rcu_putref(sma);
246 return retval;
247 }
248
Kirill Korotaeve3893532006-10-02 02:18:22 -0700249 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if(id == -1) {
251 security_sem_free(sma);
252 ipc_rcu_putref(sma);
253 return -ENOSPC;
254 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700255 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700257 sma->sem_perm.id = sem_buildid(ns, id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 sma->sem_base = (struct sem *) &sma[1];
259 /* sma->sem_pending = NULL; */
260 sma->sem_pending_last = &sma->sem_pending;
261 /* sma->undo = NULL; */
262 sma->sem_nsems = nsems;
263 sma->sem_ctime = get_seconds();
264 sem_unlock(sma);
265
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700266 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700269
270static inline int sem_security(void *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700272 return security_sem_associate((struct sem_array *) sma, semflg);
273}
274
275static inline int sem_more_checks(void *sma, struct ipc_params *params)
276{
277 if (params->u.nsems > ((struct sem_array *)sma)->sem_nsems)
278 return -EINVAL;
279
280 return 0;
281}
282
283asmlinkage long sys_semget(key_t key, int nsems, int semflg)
284{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700285 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700286 struct ipc_ops sem_ops;
287 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Kirill Korotaeve3893532006-10-02 02:18:22 -0700289 ns = current->nsproxy->ipc_ns;
290
291 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700293
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700294 sem_ops.getnew = newary;
295 sem_ops.associate = sem_security;
296 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700297
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700298 sem_params.key = key;
299 sem_params.flg = semflg;
300 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700301
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700302 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305/* Manage the doubly linked list sma->sem_pending as a FIFO:
306 * insert new queue elements at the tail sma->sem_pending_last.
307 */
308static inline void append_to_queue (struct sem_array * sma,
309 struct sem_queue * q)
310{
311 *(q->prev = sma->sem_pending_last) = q;
312 *(sma->sem_pending_last = &q->next) = NULL;
313}
314
315static inline void prepend_to_queue (struct sem_array * sma,
316 struct sem_queue * q)
317{
318 q->next = sma->sem_pending;
319 *(q->prev = &sma->sem_pending) = q;
320 if (q->next)
321 q->next->prev = &q->next;
322 else /* sma->sem_pending_last == &sma->sem_pending */
323 sma->sem_pending_last = &q->next;
324}
325
326static inline void remove_from_queue (struct sem_array * sma,
327 struct sem_queue * q)
328{
329 *(q->prev) = q->next;
330 if (q->next)
331 q->next->prev = q->prev;
332 else /* sma->sem_pending_last == &q->next */
333 sma->sem_pending_last = q->prev;
334 q->prev = NULL; /* mark as removed */
335}
336
337/*
338 * Determine whether a sequence of semaphore operations would succeed
339 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
340 */
341
342static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
343 int nsops, struct sem_undo *un, int pid)
344{
345 int result, sem_op;
346 struct sembuf *sop;
347 struct sem * curr;
348
349 for (sop = sops; sop < sops + nsops; sop++) {
350 curr = sma->sem_base + sop->sem_num;
351 sem_op = sop->sem_op;
352 result = curr->semval;
353
354 if (!sem_op && result)
355 goto would_block;
356
357 result += sem_op;
358 if (result < 0)
359 goto would_block;
360 if (result > SEMVMX)
361 goto out_of_range;
362 if (sop->sem_flg & SEM_UNDO) {
363 int undo = un->semadj[sop->sem_num] - sem_op;
364 /*
365 * Exceeding the undo range is an error.
366 */
367 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
368 goto out_of_range;
369 }
370 curr->semval = result;
371 }
372
373 sop--;
374 while (sop >= sops) {
375 sma->sem_base[sop->sem_num].sempid = pid;
376 if (sop->sem_flg & SEM_UNDO)
377 un->semadj[sop->sem_num] -= sop->sem_op;
378 sop--;
379 }
380
381 sma->sem_otime = get_seconds();
382 return 0;
383
384out_of_range:
385 result = -ERANGE;
386 goto undo;
387
388would_block:
389 if (sop->sem_flg & IPC_NOWAIT)
390 result = -EAGAIN;
391 else
392 result = 1;
393
394undo:
395 sop--;
396 while (sop >= sops) {
397 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
398 sop--;
399 }
400
401 return result;
402}
403
404/* Go through the pending queue for the indicated semaphore
405 * looking for tasks that can be completed.
406 */
407static void update_queue (struct sem_array * sma)
408{
409 int error;
410 struct sem_queue * q;
411
412 q = sma->sem_pending;
413 while(q) {
414 error = try_atomic_semop(sma, q->sops, q->nsops,
415 q->undo, q->pid);
416
417 /* Does q->sleeper still need to sleep? */
418 if (error <= 0) {
419 struct sem_queue *n;
420 remove_from_queue(sma,q);
421 q->status = IN_WAKEUP;
422 /*
423 * Continue scanning. The next operation
424 * that must be checked depends on the type of the
425 * completed operation:
426 * - if the operation modified the array, then
427 * restart from the head of the queue and
428 * check for threads that might be waiting
429 * for semaphore values to become 0.
430 * - if the operation didn't modify the array,
431 * then just continue.
432 */
433 if (q->alter)
434 n = sma->sem_pending;
435 else
436 n = q->next;
437 wake_up_process(q->sleeper);
438 /* hands-off: q will disappear immediately after
439 * writing q->status.
440 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800441 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 q->status = error;
443 q = n;
444 } else {
445 q = q->next;
446 }
447 }
448}
449
450/* The following counts are associated to each semaphore:
451 * semncnt number of tasks waiting on semval being nonzero
452 * semzcnt number of tasks waiting on semval being zero
453 * This model assumes that a task waits on exactly one semaphore.
454 * Since semaphore operations are to be performed atomically, tasks actually
455 * wait on a whole sequence of semaphores simultaneously.
456 * The counts we return here are a rough approximation, but still
457 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
458 */
459static int count_semncnt (struct sem_array * sma, ushort semnum)
460{
461 int semncnt;
462 struct sem_queue * q;
463
464 semncnt = 0;
465 for (q = sma->sem_pending; q; q = q->next) {
466 struct sembuf * sops = q->sops;
467 int nsops = q->nsops;
468 int i;
469 for (i = 0; i < nsops; i++)
470 if (sops[i].sem_num == semnum
471 && (sops[i].sem_op < 0)
472 && !(sops[i].sem_flg & IPC_NOWAIT))
473 semncnt++;
474 }
475 return semncnt;
476}
477static int count_semzcnt (struct sem_array * sma, ushort semnum)
478{
479 int semzcnt;
480 struct sem_queue * q;
481
482 semzcnt = 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 semzcnt++;
492 }
493 return semzcnt;
494}
495
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800496/* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
497 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * on exit.
499 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700500static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 struct sem_undo *un;
503 struct sem_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /* Invalidate the existing undo structures for this semaphore set.
506 * (They will be freed without any further action in exit_sem()
507 * or during the next semop.)
508 */
509 for (un = sma->undo; un; un = un->id_next)
510 un->semid = -1;
511
512 /* Wake up all pending processes and let them fail with EIDRM. */
513 q = sma->sem_pending;
514 while(q) {
515 struct sem_queue *n;
516 /* lazy remove_from_queue: we are killing the whole queue */
517 q->prev = NULL;
518 n = q->next;
519 q->status = IN_WAKEUP;
520 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100521 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 q->status = -EIDRM; /* hands-off q */
523 q = n;
524 }
525
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700526 /* Remove the semaphore set from the IDR */
527 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 sem_unlock(sma);
529
Kirill Korotaeve3893532006-10-02 02:18:22 -0700530 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 security_sem_free(sma);
532 ipc_rcu_putref(sma);
533}
534
535static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
536{
537 switch(version) {
538 case IPC_64:
539 return copy_to_user(buf, in, sizeof(*in));
540 case IPC_OLD:
541 {
542 struct semid_ds out;
543
544 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
545
546 out.sem_otime = in->sem_otime;
547 out.sem_ctime = in->sem_ctime;
548 out.sem_nsems = in->sem_nsems;
549
550 return copy_to_user(buf, &out, sizeof(out));
551 }
552 default:
553 return -EINVAL;
554 }
555}
556
Kirill Korotaeve3893532006-10-02 02:18:22 -0700557static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
558 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int err = -EINVAL;
561 struct sem_array *sma;
562
563 switch(cmd) {
564 case IPC_INFO:
565 case SEM_INFO:
566 {
567 struct seminfo seminfo;
568 int max_id;
569
570 err = security_sem_semctl(NULL, cmd);
571 if (err)
572 return err;
573
574 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700575 seminfo.semmni = ns->sc_semmni;
576 seminfo.semmns = ns->sc_semmns;
577 seminfo.semmsl = ns->sc_semmsl;
578 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 seminfo.semvmx = SEMVMX;
580 seminfo.semmnu = SEMMNU;
581 seminfo.semmap = SEMMAP;
582 seminfo.semume = SEMUME;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700583 mutex_lock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700585 seminfo.semusz = sem_ids(ns).in_use;
586 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else {
588 seminfo.semusz = SEMUSZ;
589 seminfo.semaem = SEMAEM;
590 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700591 max_id = ipc_get_maxid(&sem_ids(ns));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700592 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
594 return -EFAULT;
595 return (max_id < 0) ? 0: max_id;
596 }
597 case SEM_STAT:
598 {
599 struct semid64_ds tbuf;
600 int id;
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 memset(&tbuf,0,sizeof(tbuf));
603
Kirill Korotaeve3893532006-10-02 02:18:22 -0700604 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if(sma == NULL)
606 return -EINVAL;
607
608 err = -EACCES;
609 if (ipcperms (&sma->sem_perm, S_IRUGO))
610 goto out_unlock;
611
612 err = security_sem_semctl(sma, cmd);
613 if (err)
614 goto out_unlock;
615
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700616 id = sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
619 tbuf.sem_otime = sma->sem_otime;
620 tbuf.sem_ctime = sma->sem_ctime;
621 tbuf.sem_nsems = sma->sem_nsems;
622 sem_unlock(sma);
623 if (copy_semid_to_user (arg.buf, &tbuf, version))
624 return -EFAULT;
625 return id;
626 }
627 default:
628 return -EINVAL;
629 }
630 return err;
631out_unlock:
632 sem_unlock(sma);
633 return err;
634}
635
Kirill Korotaeve3893532006-10-02 02:18:22 -0700636static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
637 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct sem_array *sma;
640 struct sem* curr;
641 int err;
642 ushort fast_sem_io[SEMMSL_FAST];
643 ushort* sem_io = fast_sem_io;
644 int nsems;
645
Kirill Korotaeve3893532006-10-02 02:18:22 -0700646 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if(sma==NULL)
648 return -EINVAL;
649
650 nsems = sma->sem_nsems;
651
652 err=-EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700653 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto out_unlock;
655
656 err = -EACCES;
657 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
658 goto out_unlock;
659
660 err = security_sem_semctl(sma, cmd);
661 if (err)
662 goto out_unlock;
663
664 err = -EACCES;
665 switch (cmd) {
666 case GETALL:
667 {
668 ushort __user *array = arg.array;
669 int i;
670
671 if(nsems > SEMMSL_FAST) {
672 ipc_rcu_getref(sma);
673 sem_unlock(sma);
674
675 sem_io = ipc_alloc(sizeof(ushort)*nsems);
676 if(sem_io == NULL) {
677 ipc_lock_by_ptr(&sma->sem_perm);
678 ipc_rcu_putref(sma);
679 sem_unlock(sma);
680 return -ENOMEM;
681 }
682
683 ipc_lock_by_ptr(&sma->sem_perm);
684 ipc_rcu_putref(sma);
685 if (sma->sem_perm.deleted) {
686 sem_unlock(sma);
687 err = -EIDRM;
688 goto out_free;
689 }
690 }
691
692 for (i = 0; i < sma->sem_nsems; i++)
693 sem_io[i] = sma->sem_base[i].semval;
694 sem_unlock(sma);
695 err = 0;
696 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
697 err = -EFAULT;
698 goto out_free;
699 }
700 case SETALL:
701 {
702 int i;
703 struct sem_undo *un;
704
705 ipc_rcu_getref(sma);
706 sem_unlock(sma);
707
708 if(nsems > SEMMSL_FAST) {
709 sem_io = ipc_alloc(sizeof(ushort)*nsems);
710 if(sem_io == NULL) {
711 ipc_lock_by_ptr(&sma->sem_perm);
712 ipc_rcu_putref(sma);
713 sem_unlock(sma);
714 return -ENOMEM;
715 }
716 }
717
718 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
719 ipc_lock_by_ptr(&sma->sem_perm);
720 ipc_rcu_putref(sma);
721 sem_unlock(sma);
722 err = -EFAULT;
723 goto out_free;
724 }
725
726 for (i = 0; i < nsems; i++) {
727 if (sem_io[i] > SEMVMX) {
728 ipc_lock_by_ptr(&sma->sem_perm);
729 ipc_rcu_putref(sma);
730 sem_unlock(sma);
731 err = -ERANGE;
732 goto out_free;
733 }
734 }
735 ipc_lock_by_ptr(&sma->sem_perm);
736 ipc_rcu_putref(sma);
737 if (sma->sem_perm.deleted) {
738 sem_unlock(sma);
739 err = -EIDRM;
740 goto out_free;
741 }
742
743 for (i = 0; i < nsems; i++)
744 sma->sem_base[i].semval = sem_io[i];
745 for (un = sma->undo; un; un = un->id_next)
746 for (i = 0; i < nsems; i++)
747 un->semadj[i] = 0;
748 sma->sem_ctime = get_seconds();
749 /* maybe some queued-up processes were waiting for this */
750 update_queue(sma);
751 err = 0;
752 goto out_unlock;
753 }
754 case IPC_STAT:
755 {
756 struct semid64_ds tbuf;
757 memset(&tbuf,0,sizeof(tbuf));
758 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
759 tbuf.sem_otime = sma->sem_otime;
760 tbuf.sem_ctime = sma->sem_ctime;
761 tbuf.sem_nsems = sma->sem_nsems;
762 sem_unlock(sma);
763 if (copy_semid_to_user (arg.buf, &tbuf, version))
764 return -EFAULT;
765 return 0;
766 }
767 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
768 }
769 err = -EINVAL;
770 if(semnum < 0 || semnum >= nsems)
771 goto out_unlock;
772
773 curr = &sma->sem_base[semnum];
774
775 switch (cmd) {
776 case GETVAL:
777 err = curr->semval;
778 goto out_unlock;
779 case GETPID:
780 err = curr->sempid;
781 goto out_unlock;
782 case GETNCNT:
783 err = count_semncnt(sma,semnum);
784 goto out_unlock;
785 case GETZCNT:
786 err = count_semzcnt(sma,semnum);
787 goto out_unlock;
788 case SETVAL:
789 {
790 int val = arg.val;
791 struct sem_undo *un;
792 err = -ERANGE;
793 if (val > SEMVMX || val < 0)
794 goto out_unlock;
795
796 for (un = sma->undo; un; un = un->id_next)
797 un->semadj[semnum] = 0;
798 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700799 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 sma->sem_ctime = get_seconds();
801 /* maybe some queued-up processes were waiting for this */
802 update_queue(sma);
803 err = 0;
804 goto out_unlock;
805 }
806 }
807out_unlock:
808 sem_unlock(sma);
809out_free:
810 if(sem_io != fast_sem_io)
811 ipc_free(sem_io, sizeof(ushort)*nsems);
812 return err;
813}
814
815struct sem_setbuf {
816 uid_t uid;
817 gid_t gid;
818 mode_t mode;
819};
820
821static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
822{
823 switch(version) {
824 case IPC_64:
825 {
826 struct semid64_ds tbuf;
827
828 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
829 return -EFAULT;
830
831 out->uid = tbuf.sem_perm.uid;
832 out->gid = tbuf.sem_perm.gid;
833 out->mode = tbuf.sem_perm.mode;
834
835 return 0;
836 }
837 case IPC_OLD:
838 {
839 struct semid_ds tbuf_old;
840
841 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
842 return -EFAULT;
843
844 out->uid = tbuf_old.sem_perm.uid;
845 out->gid = tbuf_old.sem_perm.gid;
846 out->mode = tbuf_old.sem_perm.mode;
847
848 return 0;
849 }
850 default:
851 return -EINVAL;
852 }
853}
854
Kirill Korotaeve3893532006-10-02 02:18:22 -0700855static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
856 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 struct sem_array *sma;
859 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400860 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct kern_ipc_perm *ipcp;
862
863 if(cmd == IPC_SET) {
864 if(copy_semid_from_user (&setbuf, arg.buf, version))
865 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700867 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if(sma==NULL)
869 return -EINVAL;
870
Kirill Korotaeve3893532006-10-02 02:18:22 -0700871 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 err=-EIDRM;
873 goto out_unlock;
874 }
875 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400876
877 err = audit_ipc_obj(ipcp);
878 if (err)
879 goto out_unlock;
880
Linda Knippersac032212006-05-16 22:03:48 -0400881 if (cmd == IPC_SET) {
882 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
883 if (err)
884 goto out_unlock;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (current->euid != ipcp->cuid &&
887 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
888 err=-EPERM;
889 goto out_unlock;
890 }
891
892 err = security_sem_semctl(sma, cmd);
893 if (err)
894 goto out_unlock;
895
896 switch(cmd){
897 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700898 freeary(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 err = 0;
900 break;
901 case IPC_SET:
902 ipcp->uid = setbuf.uid;
903 ipcp->gid = setbuf.gid;
904 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
905 | (setbuf.mode & S_IRWXUGO);
906 sma->sem_ctime = get_seconds();
907 sem_unlock(sma);
908 err = 0;
909 break;
910 default:
911 sem_unlock(sma);
912 err = -EINVAL;
913 break;
914 }
915 return err;
916
917out_unlock:
918 sem_unlock(sma);
919 return err;
920}
921
922asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
923{
924 int err = -EINVAL;
925 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700926 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (semid < 0)
929 return -EINVAL;
930
931 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700932 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 switch(cmd) {
935 case IPC_INFO:
936 case SEM_INFO:
937 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700938 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return err;
940 case GETALL:
941 case GETVAL:
942 case GETPID:
943 case GETNCNT:
944 case GETZCNT:
945 case IPC_STAT:
946 case SETVAL:
947 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700948 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 return err;
950 case IPC_RMID:
951 case IPC_SET:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700952 mutex_lock(&sem_ids(ns).mutex);
953 err = semctl_down(ns,semid,semnum,cmd,version,arg);
954 mutex_unlock(&sem_ids(ns).mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return err;
956 default:
957 return -EINVAL;
958 }
959}
960
961static inline void lock_semundo(void)
962{
963 struct sem_undo_list *undo_list;
964
965 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200966 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 spin_lock(&undo_list->lock);
968}
969
970/* This code has an interaction with copy_semundo().
971 * Consider; two tasks are sharing the undo_list. task1
972 * acquires the undo_list lock in lock_semundo(). If task2 now
973 * exits before task1 releases the lock (by calling
974 * unlock_semundo()), then task1 will never call spin_unlock().
975 * This leave the sem_undo_list in a locked state. If task1 now creats task3
976 * and once again shares the sem_undo_list, the sem_undo_list will still be
977 * locked, and future SEM_UNDO operations will deadlock. This case is
978 * dealt with in copy_semundo() by having it reinitialize the spin lock when
979 * the refcnt goes from 1 to 2.
980 */
981static inline void unlock_semundo(void)
982{
983 struct sem_undo_list *undo_list;
984
985 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200986 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 spin_unlock(&undo_list->lock);
988}
989
990
991/* If the task doesn't already have a undo_list, then allocate one
992 * here. We guarantee there is only one thread using this undo list,
993 * and current is THE ONE
994 *
995 * If this allocation and assignment succeeds, but later
996 * portions of this code fail, there is no need to free the sem_undo_list.
997 * Just let it stay associated with the task, and it'll be freed later
998 * at exit time.
999 *
1000 * This can block, so callers must hold no locks.
1001 */
1002static inline int get_undo_list(struct sem_undo_list **undo_listp)
1003{
1004 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 undo_list = current->sysvsem.undo_list;
1007 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001008 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (undo_list == NULL)
1010 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001011 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 atomic_set(&undo_list->refcnt, 1);
1013 current->sysvsem.undo_list = undo_list;
1014 }
1015 *undo_listp = undo_list;
1016 return 0;
1017}
1018
1019static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1020{
1021 struct sem_undo **last, *un;
1022
1023 last = &ulp->proc_list;
1024 un = *last;
1025 while(un != NULL) {
1026 if(un->semid==semid)
1027 break;
1028 if(un->semid==-1) {
1029 *last=un->proc_next;
1030 kfree(un);
1031 } else {
1032 last=&un->proc_next;
1033 }
1034 un=*last;
1035 }
1036 return un;
1037}
1038
Kirill Korotaeve3893532006-10-02 02:18:22 -07001039static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct sem_array *sma;
1042 struct sem_undo_list *ulp;
1043 struct sem_undo *un, *new;
1044 int nsems;
1045 int error;
1046
1047 error = get_undo_list(&ulp);
1048 if (error)
1049 return ERR_PTR(error);
1050
1051 lock_semundo();
1052 un = lookup_undo(ulp, semid);
1053 unlock_semundo();
1054 if (likely(un!=NULL))
1055 goto out;
1056
1057 /* no undo structure around - allocate one. */
Kirill Korotaeve3893532006-10-02 02:18:22 -07001058 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 un = ERR_PTR(-EINVAL);
1060 if(sma==NULL)
1061 goto out;
1062 un = ERR_PTR(-EIDRM);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001063 if (sem_checkid(ns,sma,semid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 sem_unlock(sma);
1065 goto out;
1066 }
1067 nsems = sma->sem_nsems;
1068 ipc_rcu_getref(sma);
1069 sem_unlock(sma);
1070
Burman Yan4668edc2006-12-06 20:38:51 -08001071 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!new) {
1073 ipc_lock_by_ptr(&sma->sem_perm);
1074 ipc_rcu_putref(sma);
1075 sem_unlock(sma);
1076 return ERR_PTR(-ENOMEM);
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 new->semadj = (short *) &new[1];
1079 new->semid = semid;
1080
1081 lock_semundo();
1082 un = lookup_undo(ulp, semid);
1083 if (un) {
1084 unlock_semundo();
1085 kfree(new);
1086 ipc_lock_by_ptr(&sma->sem_perm);
1087 ipc_rcu_putref(sma);
1088 sem_unlock(sma);
1089 goto out;
1090 }
1091 ipc_lock_by_ptr(&sma->sem_perm);
1092 ipc_rcu_putref(sma);
1093 if (sma->sem_perm.deleted) {
1094 sem_unlock(sma);
1095 unlock_semundo();
1096 kfree(new);
1097 un = ERR_PTR(-EIDRM);
1098 goto out;
1099 }
1100 new->proc_next = ulp->proc_list;
1101 ulp->proc_list = new;
1102 new->id_next = sma->undo;
1103 sma->undo = new;
1104 sem_unlock(sma);
1105 un = new;
1106 unlock_semundo();
1107out:
1108 return un;
1109}
1110
1111asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1112 unsigned nsops, const struct timespec __user *timeout)
1113{
1114 int error = -EINVAL;
1115 struct sem_array *sma;
1116 struct sembuf fast_sops[SEMOPM_FAST];
1117 struct sembuf* sops = fast_sops, *sop;
1118 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001119 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct sem_queue queue;
1121 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001122 struct ipc_namespace *ns;
1123
1124 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 if (nsops < 1 || semid < 0)
1127 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001128 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -E2BIG;
1130 if(nsops > SEMOPM_FAST) {
1131 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1132 if(sops==NULL)
1133 return -ENOMEM;
1134 }
1135 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1136 error=-EFAULT;
1137 goto out_free;
1138 }
1139 if (timeout) {
1140 struct timespec _timeout;
1141 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1142 error = -EFAULT;
1143 goto out_free;
1144 }
1145 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1146 _timeout.tv_nsec >= 1000000000L) {
1147 error = -EINVAL;
1148 goto out_free;
1149 }
1150 jiffies_left = timespec_to_jiffies(&_timeout);
1151 }
1152 max = 0;
1153 for (sop = sops; sop < sops + nsops; sop++) {
1154 if (sop->sem_num >= max)
1155 max = sop->sem_num;
1156 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001157 undos = 1;
1158 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 alter = 1;
1160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162retry_undos:
1163 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001164 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (IS_ERR(un)) {
1166 error = PTR_ERR(un);
1167 goto out_free;
1168 }
1169 } else
1170 un = NULL;
1171
Kirill Korotaeve3893532006-10-02 02:18:22 -07001172 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 error=-EINVAL;
1174 if(sma==NULL)
1175 goto out_free;
1176 error = -EIDRM;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001177 if (sem_checkid(ns,sma,semid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 goto out_unlock_free;
1179 /*
1180 * semid identifies are not unique - find_undo may have
1181 * allocated an undo structure, it was invalidated by an RMID
1182 * and now a new array with received the same id. Check and retry.
1183 */
1184 if (un && un->semid == -1) {
1185 sem_unlock(sma);
1186 goto retry_undos;
1187 }
1188 error = -EFBIG;
1189 if (max >= sma->sem_nsems)
1190 goto out_unlock_free;
1191
1192 error = -EACCES;
1193 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1194 goto out_unlock_free;
1195
1196 error = security_sem_semop(sma, sops, nsops, alter);
1197 if (error)
1198 goto out_unlock_free;
1199
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001200 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (error <= 0) {
1202 if (alter && error == 0)
1203 update_queue (sma);
1204 goto out_unlock_free;
1205 }
1206
1207 /* We need to sleep on this operation, so we put the current
1208 * task into the pending queue and go to sleep.
1209 */
1210
1211 queue.sma = sma;
1212 queue.sops = sops;
1213 queue.nsops = nsops;
1214 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001215 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 queue.id = semid;
1217 queue.alter = alter;
1218 if (alter)
1219 append_to_queue(sma ,&queue);
1220 else
1221 prepend_to_queue(sma ,&queue);
1222
1223 queue.status = -EINTR;
1224 queue.sleeper = current;
1225 current->state = TASK_INTERRUPTIBLE;
1226 sem_unlock(sma);
1227
1228 if (timeout)
1229 jiffies_left = schedule_timeout(jiffies_left);
1230 else
1231 schedule();
1232
1233 error = queue.status;
1234 while(unlikely(error == IN_WAKEUP)) {
1235 cpu_relax();
1236 error = queue.status;
1237 }
1238
1239 if (error != -EINTR) {
1240 /* fast path: update_queue already obtained all requested
1241 * resources */
1242 goto out_free;
1243 }
1244
Kirill Korotaeve3893532006-10-02 02:18:22 -07001245 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if(sma==NULL) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001247 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 error = -EIDRM;
1249 goto out_free;
1250 }
1251
1252 /*
1253 * If queue.status != -EINTR we are woken up by another process
1254 */
1255 error = queue.status;
1256 if (error != -EINTR) {
1257 goto out_unlock_free;
1258 }
1259
1260 /*
1261 * If an interrupt occurred we have to clean up the queue
1262 */
1263 if (timeout && jiffies_left == 0)
1264 error = -EAGAIN;
1265 remove_from_queue(sma,&queue);
1266 goto out_unlock_free;
1267
1268out_unlock_free:
1269 sem_unlock(sma);
1270out_free:
1271 if(sops != fast_sops)
1272 kfree(sops);
1273 return error;
1274}
1275
1276asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1277{
1278 return sys_semtimedop(semid, tsops, nsops, NULL);
1279}
1280
1281/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1282 * parent and child tasks.
1283 *
1284 * See the notes above unlock_semundo() regarding the spin_lock_init()
1285 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1286 * because of the reasoning in the comment above unlock_semundo.
1287 */
1288
1289int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1290{
1291 struct sem_undo_list *undo_list;
1292 int error;
1293
1294 if (clone_flags & CLONE_SYSVSEM) {
1295 error = get_undo_list(&undo_list);
1296 if (error)
1297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 atomic_inc(&undo_list->refcnt);
1299 tsk->sysvsem.undo_list = undo_list;
1300 } else
1301 tsk->sysvsem.undo_list = NULL;
1302
1303 return 0;
1304}
1305
1306/*
1307 * add semadj values to semaphores, free undo structures.
1308 * undo structures are not freed when semaphore arrays are destroyed
1309 * so some of them may be out of date.
1310 * IMPLEMENTATION NOTE: There is some confusion over whether the
1311 * set of adjustments that needs to be done should be done in an atomic
1312 * manner or not. That is, if we are attempting to decrement the semval
1313 * should we queue up and wait until we can do so legally?
1314 * The original implementation attempted to do this (queue and wait).
1315 * The current implementation does not do so. The POSIX standard
1316 * and SVID should be consulted to determine what behavior is mandated.
1317 */
1318void exit_sem(struct task_struct *tsk)
1319{
1320 struct sem_undo_list *undo_list;
1321 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001322 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 undo_list = tsk->sysvsem.undo_list;
1325 if (!undo_list)
1326 return;
1327
1328 if (!atomic_dec_and_test(&undo_list->refcnt))
1329 return;
1330
Kirill Korotaeve3893532006-10-02 02:18:22 -07001331 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 /* There's no need to hold the semundo list lock, as current
1333 * is the last task exiting for this undo list.
1334 */
1335 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1336 struct sem_array *sma;
1337 int nsems, i;
1338 struct sem_undo *un, **unp;
1339 int semid;
1340
1341 semid = u->semid;
1342
1343 if(semid == -1)
1344 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001345 sma = sem_lock(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (sma == NULL)
1347 continue;
1348
1349 if (u->semid == -1)
1350 goto next_entry;
1351
Kirill Korotaeve3893532006-10-02 02:18:22 -07001352 BUG_ON(sem_checkid(ns,sma,u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* remove u from the sma->undo list */
1355 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1356 if (u == un)
1357 goto found;
1358 }
1359 printk ("exit_sem undo list error id=%d\n", u->semid);
1360 goto next_entry;
1361found:
1362 *unp = un->id_next;
1363 /* perform adjustments registered in u */
1364 nsems = sma->sem_nsems;
1365 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001366 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001368 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 /*
1370 * Range checks of the new semaphore value,
1371 * not defined by sus:
1372 * - Some unices ignore the undo entirely
1373 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1374 * - some cap the value (e.g. FreeBSD caps
1375 * at 0, but doesn't enforce SEMVMX)
1376 *
1377 * Linux caps the semaphore value, both at 0
1378 * and at SEMVMX.
1379 *
1380 * Manfred <manfred@colorfullife.com>
1381 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001382 if (semaphore->semval < 0)
1383 semaphore->semval = 0;
1384 if (semaphore->semval > SEMVMX)
1385 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001386 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 }
1389 sma->sem_otime = get_seconds();
1390 /* maybe some queued-up processes were waiting for this */
1391 update_queue(sma);
1392next_entry:
1393 sem_unlock(sma);
1394 }
1395 kfree(undo_list);
1396}
1397
1398#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001399static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Mike Waychison19b49462005-09-06 15:17:10 -07001401 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Mike Waychison19b49462005-09-06 15:17:10 -07001403 return seq_printf(s,
1404 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1405 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001406 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001407 sma->sem_perm.mode,
1408 sma->sem_nsems,
1409 sma->sem_perm.uid,
1410 sma->sem_perm.gid,
1411 sma->sem_perm.cuid,
1412 sma->sem_perm.cgid,
1413 sma->sem_otime,
1414 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416#endif