blob: 0b45a4d383c6ae662845f5e26c6bc19f25c10a22 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
6 * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995):
7 * This code underwent a massive rewrite in order to solve some problems
8 * with the original code. In particular the original code failed to
9 * wake up processes that were waiting for semval to go to 0 if the
10 * value went to 0 and was then incremented rapidly enough. In solving
11 * this problem I have also modified the implementation so that it
12 * processes pending operations in a FIFO manner, thus give a guarantee
13 * that processes waiting for a lock on the semaphore won't starve
14 * unless another locking process fails to unlock.
15 * In addition the following two changes in behavior have been introduced:
16 * - The original implementation of semop returned the value
17 * last semaphore element examined on success. This does not
18 * match the manual page specifications, and effectively
19 * allows the user to read the semaphore even if they do not
20 * have read permissions. The implementation now returns 0
21 * on success as stated in the manual page.
22 * - There is some confusion over whether the set of undo adjustments
23 * to be performed at exit should be done in an atomic manner.
24 * That is, if we are attempting to decrement the semval should we queue
25 * up and wait until we can do so legally?
26 * The original implementation attempted to do this.
27 * The current implementation does not do so. This is because I don't
28 * think it is the right thing (TM) to do, and because I couldn't
29 * see a clean way to get the old behavior with the new design.
30 * The POSIX standard and SVID should be consulted to determine
31 * what behavior is mandated.
32 *
33 * Further notes on refinement (Christoph Rohland, December 1998):
34 * - The POSIX standard says, that the undo adjustments simply should
35 * redo. So the current implementation is o.K.
36 * - The previous code had two flaws:
37 * 1) It actively gave the semaphore to the next waiting process
38 * sleeping on the semaphore. Since this process did not have the
39 * cpu this led to many unnecessary context switches and bad
40 * performance. Now we only check which process should be able to
41 * get the semaphore and if this process wants to reduce some
42 * semaphore value we simply wake it up without doing the
43 * operation. So it has to try to get it later. Thus e.g. the
44 * running process may reacquire the semaphore during the current
45 * time slice. If it only waits for zero or increases the semaphore,
46 * we do the operation in advance and wake it up.
47 * 2) It did not wake up all zero waiting processes. We try to do
48 * better but only get the semops right which only wait for zero or
49 * increase. If there are decrement operations in the operations
50 * array we do the same as before.
51 *
52 * With the incarnation of O(1) scheduler, it becomes unnecessary to perform
53 * check/retry algorithm for waking up blocked processes as the new scheduler
54 * is better at handling thread switch than the old one.
55 *
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
57 *
58 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +010059 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
62 * Lockless wakeup
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040064 *
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070067 *
68 * namespaces support
69 * OpenVZ, SWsoft Inc.
70 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/init.h>
76#include <linux/proc_fs.h>
77#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/security.h>
79#include <linux/syscalls.h>
80#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080081#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070082#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070083#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070084#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080085#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <asm/uaccess.h>
88#include "util.h"
89
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080090#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Kirill Korotaeve3893532006-10-02 02:18:22 -070092#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070093#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
94#define sem_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Nadia Derbey7748dbf2007-10-18 23:40:49 -070096static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -080097static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -070099static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#endif
101
102#define SEMMSL_FAST 256 /* 512 bytes on stack */
103#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
104
105/*
106 * linked list protection:
107 * sem_undo.id_next,
108 * sem_array.sem_pending{,last},
109 * sem_array.sem_undo: sem_lock() for read/write
110 * sem_undo.proc_next: only "current" is allowed to read/write that field.
111 *
112 */
113
Kirill Korotaeve3893532006-10-02 02:18:22 -0700114#define sc_semmsl sem_ctls[0]
115#define sc_semmns sem_ctls[1]
116#define sc_semopm sem_ctls[2]
117#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800119void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700120{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700121 ns->sc_semmsl = SEMMSL;
122 ns->sc_semmns = SEMMNS;
123 ns->sc_semopm = SEMOPM;
124 ns->sc_semmni = SEMMNI;
125 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800126 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700127}
128
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800129#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700130void sem_exit_ns(struct ipc_namespace *ns)
131{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800132 free_ipcs(ns, &sem_ids(ns), freeary);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700133}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800134#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136void __init sem_init (void)
137{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800138 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700139 ipc_init_proc_interface("sysvipc/sem",
140 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700141 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Nadia Derbey3e148c72007-10-18 23:40:54 -0700144/*
145 * This routine is called in the paths where the rw_mutex is held to protect
146 * access to the idr tree.
147 */
148static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
149 int id)
150{
151 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
152
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800153 if (IS_ERR(ipcp))
154 return (struct sem_array *)ipcp;
155
Nadia Derbey3e148c72007-10-18 23:40:54 -0700156 return container_of(ipcp, struct sem_array, sem_perm);
157}
158
159/*
160 * sem_lock_(check_) routines are called in the paths where the rw_mutex
161 * is not held.
162 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700163static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
164{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700165 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
166
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800167 if (IS_ERR(ipcp))
168 return (struct sem_array *)ipcp;
169
Nadia Derbey03f02c72007-10-18 23:40:51 -0700170 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700171}
172
173static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
174 int id)
175{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700176 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
177
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800178 if (IS_ERR(ipcp))
179 return (struct sem_array *)ipcp;
180
Nadia Derbey03f02c72007-10-18 23:40:51 -0700181 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700182}
183
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700184static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
185{
186 ipc_rmid(&sem_ids(ns), &s->sem_perm);
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * Lockless wakeup algorithm:
191 * Without the check/retry algorithm a lockless wakeup is possible:
192 * - queue.status is initialized to -EINTR before blocking.
193 * - wakeup is performed by
194 * * unlinking the queue entry from sma->sem_pending
195 * * setting queue.status to IN_WAKEUP
196 * This is the notification for the blocked thread that a
197 * result value is imminent.
198 * * call wake_up_process
199 * * set queue.status to the final value.
200 * - the previously blocked thread checks queue.status:
201 * * if it's IN_WAKEUP, then it must wait until the value changes
202 * * if it's not -EINTR, then the operation was completed by
203 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800204 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * * otherwise it must acquire the spinlock and check what's up.
206 *
207 * The two-stage algorithm is necessary to protect against the following
208 * races:
209 * - if queue.status is set after wake_up_process, then the woken up idle
210 * thread could race forward and try (and fail) to acquire sma->lock
211 * before update_queue had a chance to set queue.status
212 * - if queue.status is written before wake_up_process and if the
213 * blocked process is woken up by a signal between writing
214 * queue.status and the wake_up_process, then the woken up
215 * process could return from semtimedop and die by calling
216 * sys_exit before wake_up_process is called. Then wake_up_process
217 * will oops, because the task structure is already invalid.
218 * (yes, this happened on s390 with sysv msg).
219 *
220 */
221#define IN_WAKEUP 1
222
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700223/**
224 * newary - Create a new semaphore set
225 * @ns: namespace
226 * @params: ptr to the structure that contains key, semflg and nsems
227 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700228 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700229 */
230
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700231static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 int id;
234 int retval;
235 struct sem_array *sma;
236 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700237 key_t key = params->key;
238 int nsems = params->u.nsems;
239 int semflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!nsems)
242 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700243 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return -ENOSPC;
245
246 size = sizeof (*sma) + nsems * sizeof (struct sem);
247 sma = ipc_rcu_alloc(size);
248 if (!sma) {
249 return -ENOMEM;
250 }
251 memset (sma, 0, size);
252
253 sma->sem_perm.mode = (semflg & S_IRWXUGO);
254 sma->sem_perm.key = key;
255
256 sma->sem_perm.security = NULL;
257 retval = security_sem_alloc(sma);
258 if (retval) {
259 ipc_rcu_putref(sma);
260 return retval;
261 }
262
Kirill Korotaeve3893532006-10-02 02:18:22 -0700263 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700264 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 security_sem_free(sma);
266 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700267 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700269 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Nadia Derbey1b531f22007-10-18 23:40:55 -0700271 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 sma->sem_base = (struct sem *) &sma[1];
273 /* sma->sem_pending = NULL; */
274 sma->sem_pending_last = &sma->sem_pending;
275 /* sma->undo = NULL; */
276 sma->sem_nsems = nsems;
277 sma->sem_ctime = get_seconds();
278 sem_unlock(sma);
279
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700280 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700283
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700284/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700285 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700286 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700287static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700289 struct sem_array *sma;
290
291 sma = container_of(ipcp, struct sem_array, sem_perm);
292 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700293}
294
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700295/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700296 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700297 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700298static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
299 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700300{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700301 struct sem_array *sma;
302
303 sma = container_of(ipcp, struct sem_array, sem_perm);
304 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700305 return -EINVAL;
306
307 return 0;
308}
309
310asmlinkage long sys_semget(key_t key, int nsems, int semflg)
311{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700312 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700313 struct ipc_ops sem_ops;
314 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Kirill Korotaeve3893532006-10-02 02:18:22 -0700316 ns = current->nsproxy->ipc_ns;
317
318 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700320
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700321 sem_ops.getnew = newary;
322 sem_ops.associate = sem_security;
323 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700324
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700325 sem_params.key = key;
326 sem_params.flg = semflg;
327 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700328
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700329 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/* Manage the doubly linked list sma->sem_pending as a FIFO:
333 * insert new queue elements at the tail sma->sem_pending_last.
334 */
335static inline void append_to_queue (struct sem_array * sma,
336 struct sem_queue * q)
337{
338 *(q->prev = sma->sem_pending_last) = q;
339 *(sma->sem_pending_last = &q->next) = NULL;
340}
341
342static inline void prepend_to_queue (struct sem_array * sma,
343 struct sem_queue * q)
344{
345 q->next = sma->sem_pending;
346 *(q->prev = &sma->sem_pending) = q;
347 if (q->next)
348 q->next->prev = &q->next;
349 else /* sma->sem_pending_last == &sma->sem_pending */
350 sma->sem_pending_last = &q->next;
351}
352
353static inline void remove_from_queue (struct sem_array * sma,
354 struct sem_queue * q)
355{
356 *(q->prev) = q->next;
357 if (q->next)
358 q->next->prev = q->prev;
359 else /* sma->sem_pending_last == &q->next */
360 sma->sem_pending_last = q->prev;
361 q->prev = NULL; /* mark as removed */
362}
363
364/*
365 * Determine whether a sequence of semaphore operations would succeed
366 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
367 */
368
369static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
370 int nsops, struct sem_undo *un, int pid)
371{
372 int result, sem_op;
373 struct sembuf *sop;
374 struct sem * curr;
375
376 for (sop = sops; sop < sops + nsops; sop++) {
377 curr = sma->sem_base + sop->sem_num;
378 sem_op = sop->sem_op;
379 result = curr->semval;
380
381 if (!sem_op && result)
382 goto would_block;
383
384 result += sem_op;
385 if (result < 0)
386 goto would_block;
387 if (result > SEMVMX)
388 goto out_of_range;
389 if (sop->sem_flg & SEM_UNDO) {
390 int undo = un->semadj[sop->sem_num] - sem_op;
391 /*
392 * Exceeding the undo range is an error.
393 */
394 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
395 goto out_of_range;
396 }
397 curr->semval = result;
398 }
399
400 sop--;
401 while (sop >= sops) {
402 sma->sem_base[sop->sem_num].sempid = pid;
403 if (sop->sem_flg & SEM_UNDO)
404 un->semadj[sop->sem_num] -= sop->sem_op;
405 sop--;
406 }
407
408 sma->sem_otime = get_seconds();
409 return 0;
410
411out_of_range:
412 result = -ERANGE;
413 goto undo;
414
415would_block:
416 if (sop->sem_flg & IPC_NOWAIT)
417 result = -EAGAIN;
418 else
419 result = 1;
420
421undo:
422 sop--;
423 while (sop >= sops) {
424 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
425 sop--;
426 }
427
428 return result;
429}
430
431/* Go through the pending queue for the indicated semaphore
432 * looking for tasks that can be completed.
433 */
434static void update_queue (struct sem_array * sma)
435{
436 int error;
437 struct sem_queue * q;
438
439 q = sma->sem_pending;
440 while(q) {
441 error = try_atomic_semop(sma, q->sops, q->nsops,
442 q->undo, q->pid);
443
444 /* Does q->sleeper still need to sleep? */
445 if (error <= 0) {
446 struct sem_queue *n;
447 remove_from_queue(sma,q);
448 q->status = IN_WAKEUP;
449 /*
450 * Continue scanning. The next operation
451 * that must be checked depends on the type of the
452 * completed operation:
453 * - if the operation modified the array, then
454 * restart from the head of the queue and
455 * check for threads that might be waiting
456 * for semaphore values to become 0.
457 * - if the operation didn't modify the array,
458 * then just continue.
459 */
460 if (q->alter)
461 n = sma->sem_pending;
462 else
463 n = q->next;
464 wake_up_process(q->sleeper);
465 /* hands-off: q will disappear immediately after
466 * writing q->status.
467 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800468 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 q->status = error;
470 q = n;
471 } else {
472 q = q->next;
473 }
474 }
475}
476
477/* The following counts are associated to each semaphore:
478 * semncnt number of tasks waiting on semval being nonzero
479 * semzcnt number of tasks waiting on semval being zero
480 * This model assumes that a task waits on exactly one semaphore.
481 * Since semaphore operations are to be performed atomically, tasks actually
482 * wait on a whole sequence of semaphores simultaneously.
483 * The counts we return here are a rough approximation, but still
484 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
485 */
486static int count_semncnt (struct sem_array * sma, ushort semnum)
487{
488 int semncnt;
489 struct sem_queue * q;
490
491 semncnt = 0;
492 for (q = sma->sem_pending; q; q = q->next) {
493 struct sembuf * sops = q->sops;
494 int nsops = q->nsops;
495 int i;
496 for (i = 0; i < nsops; i++)
497 if (sops[i].sem_num == semnum
498 && (sops[i].sem_op < 0)
499 && !(sops[i].sem_flg & IPC_NOWAIT))
500 semncnt++;
501 }
502 return semncnt;
503}
504static int count_semzcnt (struct sem_array * sma, ushort semnum)
505{
506 int semzcnt;
507 struct sem_queue * q;
508
509 semzcnt = 0;
510 for (q = sma->sem_pending; q; q = q->next) {
511 struct sembuf * sops = q->sops;
512 int nsops = q->nsops;
513 int i;
514 for (i = 0; i < nsops; i++)
515 if (sops[i].sem_num == semnum
516 && (sops[i].sem_op == 0)
517 && !(sops[i].sem_flg & IPC_NOWAIT))
518 semzcnt++;
519 }
520 return semzcnt;
521}
522
Nadia Derbey3e148c72007-10-18 23:40:54 -0700523/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
524 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
525 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800527static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct sem_undo *un;
530 struct sem_queue *q;
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800531 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Invalidate the existing undo structures for this semaphore set.
534 * (They will be freed without any further action in exit_sem()
535 * or during the next semop.)
536 */
537 for (un = sma->undo; un; un = un->id_next)
538 un->semid = -1;
539
540 /* Wake up all pending processes and let them fail with EIDRM. */
541 q = sma->sem_pending;
542 while(q) {
543 struct sem_queue *n;
544 /* lazy remove_from_queue: we are killing the whole queue */
545 q->prev = NULL;
546 n = q->next;
547 q->status = IN_WAKEUP;
548 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100549 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 q->status = -EIDRM; /* hands-off q */
551 q = n;
552 }
553
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700554 /* Remove the semaphore set from the IDR */
555 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 sem_unlock(sma);
557
Kirill Korotaeve3893532006-10-02 02:18:22 -0700558 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 security_sem_free(sma);
560 ipc_rcu_putref(sma);
561}
562
563static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
564{
565 switch(version) {
566 case IPC_64:
567 return copy_to_user(buf, in, sizeof(*in));
568 case IPC_OLD:
569 {
570 struct semid_ds out;
571
572 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
573
574 out.sem_otime = in->sem_otime;
575 out.sem_ctime = in->sem_ctime;
576 out.sem_nsems = in->sem_nsems;
577
578 return copy_to_user(buf, &out, sizeof(out));
579 }
580 default:
581 return -EINVAL;
582 }
583}
584
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800585static int semctl_nolock(struct ipc_namespace *ns, int semid,
586 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 int err = -EINVAL;
589 struct sem_array *sma;
590
591 switch(cmd) {
592 case IPC_INFO:
593 case SEM_INFO:
594 {
595 struct seminfo seminfo;
596 int max_id;
597
598 err = security_sem_semctl(NULL, cmd);
599 if (err)
600 return err;
601
602 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700603 seminfo.semmni = ns->sc_semmni;
604 seminfo.semmns = ns->sc_semmns;
605 seminfo.semmsl = ns->sc_semmsl;
606 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 seminfo.semvmx = SEMVMX;
608 seminfo.semmnu = SEMMNU;
609 seminfo.semmap = SEMMAP;
610 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700611 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700613 seminfo.semusz = sem_ids(ns).in_use;
614 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } else {
616 seminfo.semusz = SEMUSZ;
617 seminfo.semaem = SEMAEM;
618 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700619 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700620 up_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
622 return -EFAULT;
623 return (max_id < 0) ? 0: max_id;
624 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800625 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 case SEM_STAT:
627 {
628 struct semid64_ds tbuf;
629 int id;
630
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800631 if (cmd == SEM_STAT) {
632 sma = sem_lock(ns, semid);
633 if (IS_ERR(sma))
634 return PTR_ERR(sma);
635 id = sma->sem_perm.id;
636 } else {
637 sma = sem_lock_check(ns, semid);
638 if (IS_ERR(sma))
639 return PTR_ERR(sma);
640 id = 0;
641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 err = -EACCES;
644 if (ipcperms (&sma->sem_perm, S_IRUGO))
645 goto out_unlock;
646
647 err = security_sem_semctl(sma, cmd);
648 if (err)
649 goto out_unlock;
650
Nadia Derbey023a5352007-10-18 23:40:51 -0700651 memset(&tbuf, 0, sizeof(tbuf));
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
654 tbuf.sem_otime = sma->sem_otime;
655 tbuf.sem_ctime = sma->sem_ctime;
656 tbuf.sem_nsems = sma->sem_nsems;
657 sem_unlock(sma);
658 if (copy_semid_to_user (arg.buf, &tbuf, version))
659 return -EFAULT;
660 return id;
661 }
662 default:
663 return -EINVAL;
664 }
665 return err;
666out_unlock:
667 sem_unlock(sma);
668 return err;
669}
670
Kirill Korotaeve3893532006-10-02 02:18:22 -0700671static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
672 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct sem_array *sma;
675 struct sem* curr;
676 int err;
677 ushort fast_sem_io[SEMMSL_FAST];
678 ushort* sem_io = fast_sem_io;
679 int nsems;
680
Nadia Derbey023a5352007-10-18 23:40:51 -0700681 sma = sem_lock_check(ns, semid);
682 if (IS_ERR(sma))
683 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 nsems = sma->sem_nsems;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 err = -EACCES;
688 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
689 goto out_unlock;
690
691 err = security_sem_semctl(sma, cmd);
692 if (err)
693 goto out_unlock;
694
695 err = -EACCES;
696 switch (cmd) {
697 case GETALL:
698 {
699 ushort __user *array = arg.array;
700 int i;
701
702 if(nsems > SEMMSL_FAST) {
703 ipc_rcu_getref(sma);
704 sem_unlock(sma);
705
706 sem_io = ipc_alloc(sizeof(ushort)*nsems);
707 if(sem_io == NULL) {
708 ipc_lock_by_ptr(&sma->sem_perm);
709 ipc_rcu_putref(sma);
710 sem_unlock(sma);
711 return -ENOMEM;
712 }
713
714 ipc_lock_by_ptr(&sma->sem_perm);
715 ipc_rcu_putref(sma);
716 if (sma->sem_perm.deleted) {
717 sem_unlock(sma);
718 err = -EIDRM;
719 goto out_free;
720 }
721 }
722
723 for (i = 0; i < sma->sem_nsems; i++)
724 sem_io[i] = sma->sem_base[i].semval;
725 sem_unlock(sma);
726 err = 0;
727 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
728 err = -EFAULT;
729 goto out_free;
730 }
731 case SETALL:
732 {
733 int i;
734 struct sem_undo *un;
735
736 ipc_rcu_getref(sma);
737 sem_unlock(sma);
738
739 if(nsems > SEMMSL_FAST) {
740 sem_io = ipc_alloc(sizeof(ushort)*nsems);
741 if(sem_io == NULL) {
742 ipc_lock_by_ptr(&sma->sem_perm);
743 ipc_rcu_putref(sma);
744 sem_unlock(sma);
745 return -ENOMEM;
746 }
747 }
748
749 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
750 ipc_lock_by_ptr(&sma->sem_perm);
751 ipc_rcu_putref(sma);
752 sem_unlock(sma);
753 err = -EFAULT;
754 goto out_free;
755 }
756
757 for (i = 0; i < nsems; i++) {
758 if (sem_io[i] > SEMVMX) {
759 ipc_lock_by_ptr(&sma->sem_perm);
760 ipc_rcu_putref(sma);
761 sem_unlock(sma);
762 err = -ERANGE;
763 goto out_free;
764 }
765 }
766 ipc_lock_by_ptr(&sma->sem_perm);
767 ipc_rcu_putref(sma);
768 if (sma->sem_perm.deleted) {
769 sem_unlock(sma);
770 err = -EIDRM;
771 goto out_free;
772 }
773
774 for (i = 0; i < nsems; i++)
775 sma->sem_base[i].semval = sem_io[i];
776 for (un = sma->undo; un; un = un->id_next)
777 for (i = 0; i < nsems; i++)
778 un->semadj[i] = 0;
779 sma->sem_ctime = get_seconds();
780 /* maybe some queued-up processes were waiting for this */
781 update_queue(sma);
782 err = 0;
783 goto out_unlock;
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /* 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 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700885 sma = sem_lock_check_down(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700886 if (IS_ERR(sma))
887 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400890
891 err = audit_ipc_obj(ipcp);
892 if (err)
893 goto out_unlock;
894
Linda Knippersac032212006-05-16 22:03:48 -0400895 if (cmd == IPC_SET) {
896 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
897 if (err)
898 goto out_unlock;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (current->euid != ipcp->cuid &&
901 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
902 err=-EPERM;
903 goto out_unlock;
904 }
905
906 err = security_sem_semctl(sma, cmd);
907 if (err)
908 goto out_unlock;
909
910 switch(cmd){
911 case IPC_RMID:
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800912 freeary(ns, ipcp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 err = 0;
914 break;
915 case IPC_SET:
916 ipcp->uid = setbuf.uid;
917 ipcp->gid = setbuf.gid;
918 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
919 | (setbuf.mode & S_IRWXUGO);
920 sma->sem_ctime = get_seconds();
921 sem_unlock(sma);
922 err = 0;
923 break;
924 default:
925 sem_unlock(sma);
926 err = -EINVAL;
927 break;
928 }
929 return err;
930
931out_unlock:
932 sem_unlock(sma);
933 return err;
934}
935
936asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
937{
938 int err = -EINVAL;
939 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700940 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 if (semid < 0)
943 return -EINVAL;
944
945 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700946 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 switch(cmd) {
949 case IPC_INFO:
950 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800951 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case SEM_STAT:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -0800953 err = semctl_nolock(ns, semid, cmd, version, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return err;
955 case GETALL:
956 case GETVAL:
957 case GETPID:
958 case GETNCNT:
959 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 case SETVAL:
961 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700962 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return err;
964 case IPC_RMID:
965 case IPC_SET:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700966 down_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700967 err = semctl_down(ns,semid,semnum,cmd,version,arg);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700968 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return err;
970 default:
971 return -EINVAL;
972 }
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/* If the task doesn't already have a undo_list, then allocate one
976 * here. We guarantee there is only one thread using this undo list,
977 * and current is THE ONE
978 *
979 * If this allocation and assignment succeeds, but later
980 * portions of this code fail, there is no need to free the sem_undo_list.
981 * Just let it stay associated with the task, and it'll be freed later
982 * at exit time.
983 *
984 * This can block, so callers must hold no locks.
985 */
986static inline int get_undo_list(struct sem_undo_list **undo_listp)
987{
988 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 undo_list = current->sysvsem.undo_list;
991 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -0700992 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (undo_list == NULL)
994 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +0200995 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 atomic_set(&undo_list->refcnt, 1);
997 current->sysvsem.undo_list = undo_list;
998 }
999 *undo_listp = undo_list;
1000 return 0;
1001}
1002
1003static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1004{
1005 struct sem_undo **last, *un;
1006
1007 last = &ulp->proc_list;
1008 un = *last;
1009 while(un != NULL) {
1010 if(un->semid==semid)
1011 break;
1012 if(un->semid==-1) {
1013 *last=un->proc_next;
1014 kfree(un);
1015 } else {
1016 last=&un->proc_next;
1017 }
1018 un=*last;
1019 }
1020 return un;
1021}
1022
Kirill Korotaeve3893532006-10-02 02:18:22 -07001023static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 struct sem_array *sma;
1026 struct sem_undo_list *ulp;
1027 struct sem_undo *un, *new;
1028 int nsems;
1029 int error;
1030
1031 error = get_undo_list(&ulp);
1032 if (error)
1033 return ERR_PTR(error);
1034
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001035 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001037 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (likely(un!=NULL))
1039 goto out;
1040
1041 /* no undo structure around - allocate one. */
Nadia Derbey023a5352007-10-18 23:40:51 -07001042 sma = sem_lock_check(ns, semid);
1043 if (IS_ERR(sma))
1044 return ERR_PTR(PTR_ERR(sma));
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 nsems = sma->sem_nsems;
1047 ipc_rcu_getref(sma);
1048 sem_unlock(sma);
1049
Burman Yan4668edc2006-12-06 20:38:51 -08001050 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!new) {
1052 ipc_lock_by_ptr(&sma->sem_perm);
1053 ipc_rcu_putref(sma);
1054 sem_unlock(sma);
1055 return ERR_PTR(-ENOMEM);
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 new->semadj = (short *) &new[1];
1058 new->semid = semid;
1059
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001060 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 un = lookup_undo(ulp, semid);
1062 if (un) {
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001063 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 kfree(new);
1065 ipc_lock_by_ptr(&sma->sem_perm);
1066 ipc_rcu_putref(sma);
1067 sem_unlock(sma);
1068 goto out;
1069 }
1070 ipc_lock_by_ptr(&sma->sem_perm);
1071 ipc_rcu_putref(sma);
1072 if (sma->sem_perm.deleted) {
1073 sem_unlock(sma);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001074 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 kfree(new);
1076 un = ERR_PTR(-EIDRM);
1077 goto out;
1078 }
1079 new->proc_next = ulp->proc_list;
1080 ulp->proc_list = new;
1081 new->id_next = sma->undo;
1082 sma->undo = new;
1083 sem_unlock(sma);
1084 un = new;
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001085 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086out:
1087 return un;
1088}
1089
1090asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1091 unsigned nsops, const struct timespec __user *timeout)
1092{
1093 int error = -EINVAL;
1094 struct sem_array *sma;
1095 struct sembuf fast_sops[SEMOPM_FAST];
1096 struct sembuf* sops = fast_sops, *sop;
1097 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001098 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct sem_queue queue;
1100 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001101 struct ipc_namespace *ns;
1102
1103 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (nsops < 1 || semid < 0)
1106 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001107 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return -E2BIG;
1109 if(nsops > SEMOPM_FAST) {
1110 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1111 if(sops==NULL)
1112 return -ENOMEM;
1113 }
1114 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1115 error=-EFAULT;
1116 goto out_free;
1117 }
1118 if (timeout) {
1119 struct timespec _timeout;
1120 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1121 error = -EFAULT;
1122 goto out_free;
1123 }
1124 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1125 _timeout.tv_nsec >= 1000000000L) {
1126 error = -EINVAL;
1127 goto out_free;
1128 }
1129 jiffies_left = timespec_to_jiffies(&_timeout);
1130 }
1131 max = 0;
1132 for (sop = sops; sop < sops + nsops; sop++) {
1133 if (sop->sem_num >= max)
1134 max = sop->sem_num;
1135 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001136 undos = 1;
1137 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 alter = 1;
1139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141retry_undos:
1142 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001143 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (IS_ERR(un)) {
1145 error = PTR_ERR(un);
1146 goto out_free;
1147 }
1148 } else
1149 un = NULL;
1150
Nadia Derbey023a5352007-10-18 23:40:51 -07001151 sma = sem_lock_check(ns, semid);
1152 if (IS_ERR(sma)) {
1153 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001155 }
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /*
Nadia Derbey023a5352007-10-18 23:40:51 -07001158 * semid identifiers are not unique - find_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 * allocated an undo structure, it was invalidated by an RMID
1160 * and now a new array with received the same id. Check and retry.
1161 */
1162 if (un && un->semid == -1) {
1163 sem_unlock(sma);
1164 goto retry_undos;
1165 }
1166 error = -EFBIG;
1167 if (max >= sma->sem_nsems)
1168 goto out_unlock_free;
1169
1170 error = -EACCES;
1171 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1172 goto out_unlock_free;
1173
1174 error = security_sem_semop(sma, sops, nsops, alter);
1175 if (error)
1176 goto out_unlock_free;
1177
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001178 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (error <= 0) {
1180 if (alter && error == 0)
1181 update_queue (sma);
1182 goto out_unlock_free;
1183 }
1184
1185 /* We need to sleep on this operation, so we put the current
1186 * task into the pending queue and go to sleep.
1187 */
1188
1189 queue.sma = sma;
1190 queue.sops = sops;
1191 queue.nsops = nsops;
1192 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001193 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 queue.id = semid;
1195 queue.alter = alter;
1196 if (alter)
1197 append_to_queue(sma ,&queue);
1198 else
1199 prepend_to_queue(sma ,&queue);
1200
1201 queue.status = -EINTR;
1202 queue.sleeper = current;
1203 current->state = TASK_INTERRUPTIBLE;
1204 sem_unlock(sma);
1205
1206 if (timeout)
1207 jiffies_left = schedule_timeout(jiffies_left);
1208 else
1209 schedule();
1210
1211 error = queue.status;
1212 while(unlikely(error == IN_WAKEUP)) {
1213 cpu_relax();
1214 error = queue.status;
1215 }
1216
1217 if (error != -EINTR) {
1218 /* fast path: update_queue already obtained all requested
1219 * resources */
1220 goto out_free;
1221 }
1222
Kirill Korotaeve3893532006-10-02 02:18:22 -07001223 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001224 if (IS_ERR(sma)) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001225 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 error = -EIDRM;
1227 goto out_free;
1228 }
1229
1230 /*
1231 * If queue.status != -EINTR we are woken up by another process
1232 */
1233 error = queue.status;
1234 if (error != -EINTR) {
1235 goto out_unlock_free;
1236 }
1237
1238 /*
1239 * If an interrupt occurred we have to clean up the queue
1240 */
1241 if (timeout && jiffies_left == 0)
1242 error = -EAGAIN;
1243 remove_from_queue(sma,&queue);
1244 goto out_unlock_free;
1245
1246out_unlock_free:
1247 sem_unlock(sma);
1248out_free:
1249 if(sops != fast_sops)
1250 kfree(sops);
1251 return error;
1252}
1253
1254asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1255{
1256 return sys_semtimedop(semid, tsops, nsops, NULL);
1257}
1258
1259/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1260 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
1262
1263int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1264{
1265 struct sem_undo_list *undo_list;
1266 int error;
1267
1268 if (clone_flags & CLONE_SYSVSEM) {
1269 error = get_undo_list(&undo_list);
1270 if (error)
1271 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 atomic_inc(&undo_list->refcnt);
1273 tsk->sysvsem.undo_list = undo_list;
1274 } else
1275 tsk->sysvsem.undo_list = NULL;
1276
1277 return 0;
1278}
1279
1280/*
1281 * add semadj values to semaphores, free undo structures.
1282 * undo structures are not freed when semaphore arrays are destroyed
1283 * so some of them may be out of date.
1284 * IMPLEMENTATION NOTE: There is some confusion over whether the
1285 * set of adjustments that needs to be done should be done in an atomic
1286 * manner or not. That is, if we are attempting to decrement the semval
1287 * should we queue up and wait until we can do so legally?
1288 * The original implementation attempted to do this (queue and wait).
1289 * The current implementation does not do so. The POSIX standard
1290 * and SVID should be consulted to determine what behavior is mandated.
1291 */
1292void exit_sem(struct task_struct *tsk)
1293{
1294 struct sem_undo_list *undo_list;
1295 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001296 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 undo_list = tsk->sysvsem.undo_list;
1299 if (!undo_list)
1300 return;
1301
1302 if (!atomic_dec_and_test(&undo_list->refcnt))
1303 return;
1304
Kirill Korotaeve3893532006-10-02 02:18:22 -07001305 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 /* There's no need to hold the semundo list lock, as current
1307 * is the last task exiting for this undo list.
1308 */
1309 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1310 struct sem_array *sma;
1311 int nsems, i;
1312 struct sem_undo *un, **unp;
1313 int semid;
1314
1315 semid = u->semid;
1316
1317 if(semid == -1)
1318 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001319 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001320 if (IS_ERR(sma))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 continue;
1322
1323 if (u->semid == -1)
1324 goto next_entry;
1325
Nadia Derbey1b531f22007-10-18 23:40:55 -07001326 BUG_ON(sem_checkid(sma, u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* remove u from the sma->undo list */
1329 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1330 if (u == un)
1331 goto found;
1332 }
1333 printk ("exit_sem undo list error id=%d\n", u->semid);
1334 goto next_entry;
1335found:
1336 *unp = un->id_next;
1337 /* perform adjustments registered in u */
1338 nsems = sma->sem_nsems;
1339 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001340 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001342 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 /*
1344 * Range checks of the new semaphore value,
1345 * not defined by sus:
1346 * - Some unices ignore the undo entirely
1347 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1348 * - some cap the value (e.g. FreeBSD caps
1349 * at 0, but doesn't enforce SEMVMX)
1350 *
1351 * Linux caps the semaphore value, both at 0
1352 * and at SEMVMX.
1353 *
1354 * Manfred <manfred@colorfullife.com>
1355 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001356 if (semaphore->semval < 0)
1357 semaphore->semval = 0;
1358 if (semaphore->semval > SEMVMX)
1359 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001360 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 }
1363 sma->sem_otime = get_seconds();
1364 /* maybe some queued-up processes were waiting for this */
1365 update_queue(sma);
1366next_entry:
1367 sem_unlock(sma);
1368 }
1369 kfree(undo_list);
1370}
1371
1372#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001373static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Mike Waychison19b49462005-09-06 15:17:10 -07001375 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Mike Waychison19b49462005-09-06 15:17:10 -07001377 return seq_printf(s,
1378 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1379 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001380 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001381 sma->sem_perm.mode,
1382 sma->sem_nsems,
1383 sma->sem_perm.uid,
1384 sma->sem_perm.gid,
1385 sma->sem_perm.cuid,
1386 sma->sem_perm.cgid,
1387 sma->sem_otime,
1388 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390#endif