blob: 84c701fe5004f461ff27d7d073ee268180def732 [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
Kirill Korotaeve3893532006-10-02 02:18:22 -070090#define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Kirill Korotaeve3893532006-10-02 02:18:22 -070092#define sem_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
Kirill Korotaeve3893532006-10-02 02:18:22 -070096static struct ipc_ids init_sem_ids;
97
Nadia Derbey7748dbf2007-10-18 23:40:49 -070098static int newary(struct ipc_namespace *, struct ipc_params *);
Nadia Derbey7ca7e562007-10-18 23:40:48 -070099static void freeary(struct ipc_namespace *, struct sem_array *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700101static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
103
104#define SEMMSL_FAST 256 /* 512 bytes on stack */
105#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
106
107/*
108 * linked list protection:
109 * sem_undo.id_next,
110 * sem_array.sem_pending{,last},
111 * sem_array.sem_undo: sem_lock() for read/write
112 * sem_undo.proc_next: only "current" is allowed to read/write that field.
113 *
114 */
115
Kirill Korotaeve3893532006-10-02 02:18:22 -0700116#define sc_semmsl sem_ctls[0]
117#define sc_semmns sem_ctls[1]
118#define sc_semopm sem_ctls[2]
119#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -0700121static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700122{
123 ns->ids[IPC_SEM_IDS] = ids;
124 ns->sc_semmsl = SEMMSL;
125 ns->sc_semmns = SEMMNS;
126 ns->sc_semopm = SEMOPM;
127 ns->sc_semmni = SEMMNI;
128 ns->used_sems = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700129 ipc_init_ids(ids);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700130}
131
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800132#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700133int sem_init_ns(struct ipc_namespace *ns)
134{
135 struct ipc_ids *ids;
136
137 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
138 if (ids == NULL)
139 return -ENOMEM;
140
141 __sem_init_ns(ns, ids);
142 return 0;
143}
144
145void sem_exit_ns(struct ipc_namespace *ns)
146{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700147 struct sem_array *sma;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800148 struct kern_ipc_perm *perm;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700149 int next_id;
150 int total, in_use;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700151
Nadia Derbey3e148c72007-10-18 23:40:54 -0700152 down_write(&sem_ids(ns).rw_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++) {
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800157 perm = idr_find(&sem_ids(ns).ipcs_idr, next_id);
158 if (perm == NULL)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700159 continue;
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800160 ipc_lock_by_ptr(perm);
161 sma = container_of(perm, struct sem_array, sem_perm);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700162 freeary(ns, sma);
163 total++;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700164 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700165 up_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700166
167 kfree(ns->ids[IPC_SEM_IDS]);
168 ns->ids[IPC_SEM_IDS] = NULL;
169}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172void __init sem_init (void)
173{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700174 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700175 ipc_init_proc_interface("sysvipc/sem",
176 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700177 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Nadia Derbey3e148c72007-10-18 23:40:54 -0700180/*
181 * This routine is called in the paths where the rw_mutex is held to protect
182 * access to the idr tree.
183 */
184static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
185 int id)
186{
187 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
188
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800189 if (IS_ERR(ipcp))
190 return (struct sem_array *)ipcp;
191
Nadia Derbey3e148c72007-10-18 23:40:54 -0700192 return container_of(ipcp, struct sem_array, sem_perm);
193}
194
195/*
196 * sem_lock_(check_) routines are called in the paths where the rw_mutex
197 * is not held.
198 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700199static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
200{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700201 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
202
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800203 if (IS_ERR(ipcp))
204 return (struct sem_array *)ipcp;
205
Nadia Derbey03f02c72007-10-18 23:40:51 -0700206 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700207}
208
209static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
210 int id)
211{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700212 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
213
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800214 if (IS_ERR(ipcp))
215 return (struct sem_array *)ipcp;
216
Nadia Derbey03f02c72007-10-18 23:40:51 -0700217 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700218}
219
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700220static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
221{
222 ipc_rmid(&sem_ids(ns), &s->sem_perm);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * Lockless wakeup algorithm:
227 * Without the check/retry algorithm a lockless wakeup is possible:
228 * - queue.status is initialized to -EINTR before blocking.
229 * - wakeup is performed by
230 * * unlinking the queue entry from sma->sem_pending
231 * * setting queue.status to IN_WAKEUP
232 * This is the notification for the blocked thread that a
233 * result value is imminent.
234 * * call wake_up_process
235 * * set queue.status to the final value.
236 * - the previously blocked thread checks queue.status:
237 * * if it's IN_WAKEUP, then it must wait until the value changes
238 * * if it's not -EINTR, then the operation was completed by
239 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800240 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * * otherwise it must acquire the spinlock and check what's up.
242 *
243 * The two-stage algorithm is necessary to protect against the following
244 * races:
245 * - if queue.status is set after wake_up_process, then the woken up idle
246 * thread could race forward and try (and fail) to acquire sma->lock
247 * before update_queue had a chance to set queue.status
248 * - if queue.status is written before wake_up_process and if the
249 * blocked process is woken up by a signal between writing
250 * queue.status and the wake_up_process, then the woken up
251 * process could return from semtimedop and die by calling
252 * sys_exit before wake_up_process is called. Then wake_up_process
253 * will oops, because the task structure is already invalid.
254 * (yes, this happened on s390 with sysv msg).
255 *
256 */
257#define IN_WAKEUP 1
258
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700259/**
260 * newary - Create a new semaphore set
261 * @ns: namespace
262 * @params: ptr to the structure that contains key, semflg and nsems
263 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700264 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700265 */
266
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700267static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 int id;
270 int retval;
271 struct sem_array *sma;
272 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700273 key_t key = params->key;
274 int nsems = params->u.nsems;
275 int semflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if (!nsems)
278 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700279 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return -ENOSPC;
281
282 size = sizeof (*sma) + nsems * sizeof (struct sem);
283 sma = ipc_rcu_alloc(size);
284 if (!sma) {
285 return -ENOMEM;
286 }
287 memset (sma, 0, size);
288
289 sma->sem_perm.mode = (semflg & S_IRWXUGO);
290 sma->sem_perm.key = key;
291
292 sma->sem_perm.security = NULL;
293 retval = security_sem_alloc(sma);
294 if (retval) {
295 ipc_rcu_putref(sma);
296 return retval;
297 }
298
Kirill Korotaeve3893532006-10-02 02:18:22 -0700299 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700300 if (id < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 security_sem_free(sma);
302 ipc_rcu_putref(sma);
Pierre Peiffer283bb7f2007-10-18 23:40:57 -0700303 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700305 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Nadia Derbey1b531f22007-10-18 23:40:55 -0700307 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 sma->sem_base = (struct sem *) &sma[1];
309 /* sma->sem_pending = NULL; */
310 sma->sem_pending_last = &sma->sem_pending;
311 /* sma->undo = NULL; */
312 sma->sem_nsems = nsems;
313 sma->sem_ctime = get_seconds();
314 sem_unlock(sma);
315
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700316 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700319
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700320/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700321 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700322 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700323static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700325 struct sem_array *sma;
326
327 sma = container_of(ipcp, struct sem_array, sem_perm);
328 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700329}
330
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700331/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700332 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700333 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700334static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
335 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700336{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700337 struct sem_array *sma;
338
339 sma = container_of(ipcp, struct sem_array, sem_perm);
340 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700341 return -EINVAL;
342
343 return 0;
344}
345
346asmlinkage long sys_semget(key_t key, int nsems, int semflg)
347{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700348 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700349 struct ipc_ops sem_ops;
350 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Kirill Korotaeve3893532006-10-02 02:18:22 -0700352 ns = current->nsproxy->ipc_ns;
353
354 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700356
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700357 sem_ops.getnew = newary;
358 sem_ops.associate = sem_security;
359 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700360
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700361 sem_params.key = key;
362 sem_params.flg = semflg;
363 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700364
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700365 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/* Manage the doubly linked list sma->sem_pending as a FIFO:
369 * insert new queue elements at the tail sma->sem_pending_last.
370 */
371static inline void append_to_queue (struct sem_array * sma,
372 struct sem_queue * q)
373{
374 *(q->prev = sma->sem_pending_last) = q;
375 *(sma->sem_pending_last = &q->next) = NULL;
376}
377
378static inline void prepend_to_queue (struct sem_array * sma,
379 struct sem_queue * q)
380{
381 q->next = sma->sem_pending;
382 *(q->prev = &sma->sem_pending) = q;
383 if (q->next)
384 q->next->prev = &q->next;
385 else /* sma->sem_pending_last == &sma->sem_pending */
386 sma->sem_pending_last = &q->next;
387}
388
389static inline void remove_from_queue (struct sem_array * sma,
390 struct sem_queue * q)
391{
392 *(q->prev) = q->next;
393 if (q->next)
394 q->next->prev = q->prev;
395 else /* sma->sem_pending_last == &q->next */
396 sma->sem_pending_last = q->prev;
397 q->prev = NULL; /* mark as removed */
398}
399
400/*
401 * Determine whether a sequence of semaphore operations would succeed
402 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
403 */
404
405static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
406 int nsops, struct sem_undo *un, int pid)
407{
408 int result, sem_op;
409 struct sembuf *sop;
410 struct sem * curr;
411
412 for (sop = sops; sop < sops + nsops; sop++) {
413 curr = sma->sem_base + sop->sem_num;
414 sem_op = sop->sem_op;
415 result = curr->semval;
416
417 if (!sem_op && result)
418 goto would_block;
419
420 result += sem_op;
421 if (result < 0)
422 goto would_block;
423 if (result > SEMVMX)
424 goto out_of_range;
425 if (sop->sem_flg & SEM_UNDO) {
426 int undo = un->semadj[sop->sem_num] - sem_op;
427 /*
428 * Exceeding the undo range is an error.
429 */
430 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
431 goto out_of_range;
432 }
433 curr->semval = result;
434 }
435
436 sop--;
437 while (sop >= sops) {
438 sma->sem_base[sop->sem_num].sempid = pid;
439 if (sop->sem_flg & SEM_UNDO)
440 un->semadj[sop->sem_num] -= sop->sem_op;
441 sop--;
442 }
443
444 sma->sem_otime = get_seconds();
445 return 0;
446
447out_of_range:
448 result = -ERANGE;
449 goto undo;
450
451would_block:
452 if (sop->sem_flg & IPC_NOWAIT)
453 result = -EAGAIN;
454 else
455 result = 1;
456
457undo:
458 sop--;
459 while (sop >= sops) {
460 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
461 sop--;
462 }
463
464 return result;
465}
466
467/* Go through the pending queue for the indicated semaphore
468 * looking for tasks that can be completed.
469 */
470static void update_queue (struct sem_array * sma)
471{
472 int error;
473 struct sem_queue * q;
474
475 q = sma->sem_pending;
476 while(q) {
477 error = try_atomic_semop(sma, q->sops, q->nsops,
478 q->undo, q->pid);
479
480 /* Does q->sleeper still need to sleep? */
481 if (error <= 0) {
482 struct sem_queue *n;
483 remove_from_queue(sma,q);
484 q->status = IN_WAKEUP;
485 /*
486 * Continue scanning. The next operation
487 * that must be checked depends on the type of the
488 * completed operation:
489 * - if the operation modified the array, then
490 * restart from the head of the queue and
491 * check for threads that might be waiting
492 * for semaphore values to become 0.
493 * - if the operation didn't modify the array,
494 * then just continue.
495 */
496 if (q->alter)
497 n = sma->sem_pending;
498 else
499 n = q->next;
500 wake_up_process(q->sleeper);
501 /* hands-off: q will disappear immediately after
502 * writing q->status.
503 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800504 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 q->status = error;
506 q = n;
507 } else {
508 q = q->next;
509 }
510 }
511}
512
513/* The following counts are associated to each semaphore:
514 * semncnt number of tasks waiting on semval being nonzero
515 * semzcnt number of tasks waiting on semval being zero
516 * This model assumes that a task waits on exactly one semaphore.
517 * Since semaphore operations are to be performed atomically, tasks actually
518 * wait on a whole sequence of semaphores simultaneously.
519 * The counts we return here are a rough approximation, but still
520 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
521 */
522static int count_semncnt (struct sem_array * sma, ushort semnum)
523{
524 int semncnt;
525 struct sem_queue * q;
526
527 semncnt = 0;
528 for (q = sma->sem_pending; q; q = q->next) {
529 struct sembuf * sops = q->sops;
530 int nsops = q->nsops;
531 int i;
532 for (i = 0; i < nsops; i++)
533 if (sops[i].sem_num == semnum
534 && (sops[i].sem_op < 0)
535 && !(sops[i].sem_flg & IPC_NOWAIT))
536 semncnt++;
537 }
538 return semncnt;
539}
540static int count_semzcnt (struct sem_array * sma, ushort semnum)
541{
542 int semzcnt;
543 struct sem_queue * q;
544
545 semzcnt = 0;
546 for (q = sma->sem_pending; q; q = q->next) {
547 struct sembuf * sops = q->sops;
548 int nsops = q->nsops;
549 int i;
550 for (i = 0; i < nsops; i++)
551 if (sops[i].sem_num == semnum
552 && (sops[i].sem_op == 0)
553 && !(sops[i].sem_flg & IPC_NOWAIT))
554 semzcnt++;
555 }
556 return semzcnt;
557}
558
Nadia Derbey3e148c72007-10-18 23:40:54 -0700559/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
560 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
561 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700563static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 struct sem_undo *un;
566 struct sem_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Invalidate the existing undo structures for this semaphore set.
569 * (They will be freed without any further action in exit_sem()
570 * or during the next semop.)
571 */
572 for (un = sma->undo; un; un = un->id_next)
573 un->semid = -1;
574
575 /* Wake up all pending processes and let them fail with EIDRM. */
576 q = sma->sem_pending;
577 while(q) {
578 struct sem_queue *n;
579 /* lazy remove_from_queue: we are killing the whole queue */
580 q->prev = NULL;
581 n = q->next;
582 q->status = IN_WAKEUP;
583 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100584 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 q->status = -EIDRM; /* hands-off q */
586 q = n;
587 }
588
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700589 /* Remove the semaphore set from the IDR */
590 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sem_unlock(sma);
592
Kirill Korotaeve3893532006-10-02 02:18:22 -0700593 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 security_sem_free(sma);
595 ipc_rcu_putref(sma);
596}
597
598static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
599{
600 switch(version) {
601 case IPC_64:
602 return copy_to_user(buf, in, sizeof(*in));
603 case IPC_OLD:
604 {
605 struct semid_ds out;
606
607 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
608
609 out.sem_otime = in->sem_otime;
610 out.sem_ctime = in->sem_ctime;
611 out.sem_nsems = in->sem_nsems;
612
613 return copy_to_user(buf, &out, sizeof(out));
614 }
615 default:
616 return -EINVAL;
617 }
618}
619
Kirill Korotaeve3893532006-10-02 02:18:22 -0700620static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
621 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 int err = -EINVAL;
624 struct sem_array *sma;
625
626 switch(cmd) {
627 case IPC_INFO:
628 case SEM_INFO:
629 {
630 struct seminfo seminfo;
631 int max_id;
632
633 err = security_sem_semctl(NULL, cmd);
634 if (err)
635 return err;
636
637 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700638 seminfo.semmni = ns->sc_semmni;
639 seminfo.semmns = ns->sc_semmns;
640 seminfo.semmsl = ns->sc_semmsl;
641 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 seminfo.semvmx = SEMVMX;
643 seminfo.semmnu = SEMMNU;
644 seminfo.semmap = SEMMAP;
645 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700646 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700648 seminfo.semusz = sem_ids(ns).in_use;
649 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 } else {
651 seminfo.semusz = SEMUSZ;
652 seminfo.semaem = SEMAEM;
653 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700654 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700655 up_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
657 return -EFAULT;
658 return (max_id < 0) ? 0: max_id;
659 }
660 case SEM_STAT:
661 {
662 struct semid64_ds tbuf;
663 int id;
664
Kirill Korotaeve3893532006-10-02 02:18:22 -0700665 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700666 if (IS_ERR(sma))
667 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 err = -EACCES;
670 if (ipcperms (&sma->sem_perm, S_IRUGO))
671 goto out_unlock;
672
673 err = security_sem_semctl(sma, cmd);
674 if (err)
675 goto out_unlock;
676
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700677 id = sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Nadia Derbey023a5352007-10-18 23:40:51 -0700679 memset(&tbuf, 0, sizeof(tbuf));
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
682 tbuf.sem_otime = sma->sem_otime;
683 tbuf.sem_ctime = sma->sem_ctime;
684 tbuf.sem_nsems = sma->sem_nsems;
685 sem_unlock(sma);
686 if (copy_semid_to_user (arg.buf, &tbuf, version))
687 return -EFAULT;
688 return id;
689 }
690 default:
691 return -EINVAL;
692 }
693 return err;
694out_unlock:
695 sem_unlock(sma);
696 return err;
697}
698
Kirill Korotaeve3893532006-10-02 02:18:22 -0700699static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
700 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 struct sem_array *sma;
703 struct sem* curr;
704 int err;
705 ushort fast_sem_io[SEMMSL_FAST];
706 ushort* sem_io = fast_sem_io;
707 int nsems;
708
Nadia Derbey023a5352007-10-18 23:40:51 -0700709 sma = sem_lock_check(ns, semid);
710 if (IS_ERR(sma))
711 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 nsems = sma->sem_nsems;
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 err = -EACCES;
716 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
717 goto out_unlock;
718
719 err = security_sem_semctl(sma, cmd);
720 if (err)
721 goto out_unlock;
722
723 err = -EACCES;
724 switch (cmd) {
725 case GETALL:
726 {
727 ushort __user *array = arg.array;
728 int i;
729
730 if(nsems > SEMMSL_FAST) {
731 ipc_rcu_getref(sma);
732 sem_unlock(sma);
733
734 sem_io = ipc_alloc(sizeof(ushort)*nsems);
735 if(sem_io == NULL) {
736 ipc_lock_by_ptr(&sma->sem_perm);
737 ipc_rcu_putref(sma);
738 sem_unlock(sma);
739 return -ENOMEM;
740 }
741
742 ipc_lock_by_ptr(&sma->sem_perm);
743 ipc_rcu_putref(sma);
744 if (sma->sem_perm.deleted) {
745 sem_unlock(sma);
746 err = -EIDRM;
747 goto out_free;
748 }
749 }
750
751 for (i = 0; i < sma->sem_nsems; i++)
752 sem_io[i] = sma->sem_base[i].semval;
753 sem_unlock(sma);
754 err = 0;
755 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
756 err = -EFAULT;
757 goto out_free;
758 }
759 case SETALL:
760 {
761 int i;
762 struct sem_undo *un;
763
764 ipc_rcu_getref(sma);
765 sem_unlock(sma);
766
767 if(nsems > SEMMSL_FAST) {
768 sem_io = ipc_alloc(sizeof(ushort)*nsems);
769 if(sem_io == NULL) {
770 ipc_lock_by_ptr(&sma->sem_perm);
771 ipc_rcu_putref(sma);
772 sem_unlock(sma);
773 return -ENOMEM;
774 }
775 }
776
777 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
778 ipc_lock_by_ptr(&sma->sem_perm);
779 ipc_rcu_putref(sma);
780 sem_unlock(sma);
781 err = -EFAULT;
782 goto out_free;
783 }
784
785 for (i = 0; i < nsems; i++) {
786 if (sem_io[i] > SEMVMX) {
787 ipc_lock_by_ptr(&sma->sem_perm);
788 ipc_rcu_putref(sma);
789 sem_unlock(sma);
790 err = -ERANGE;
791 goto out_free;
792 }
793 }
794 ipc_lock_by_ptr(&sma->sem_perm);
795 ipc_rcu_putref(sma);
796 if (sma->sem_perm.deleted) {
797 sem_unlock(sma);
798 err = -EIDRM;
799 goto out_free;
800 }
801
802 for (i = 0; i < nsems; i++)
803 sma->sem_base[i].semval = sem_io[i];
804 for (un = sma->undo; un; un = un->id_next)
805 for (i = 0; i < nsems; i++)
806 un->semadj[i] = 0;
807 sma->sem_ctime = get_seconds();
808 /* maybe some queued-up processes were waiting for this */
809 update_queue(sma);
810 err = 0;
811 goto out_unlock;
812 }
813 case IPC_STAT:
814 {
815 struct semid64_ds tbuf;
816 memset(&tbuf,0,sizeof(tbuf));
817 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
818 tbuf.sem_otime = sma->sem_otime;
819 tbuf.sem_ctime = sma->sem_ctime;
820 tbuf.sem_nsems = sma->sem_nsems;
821 sem_unlock(sma);
822 if (copy_semid_to_user (arg.buf, &tbuf, version))
823 return -EFAULT;
824 return 0;
825 }
826 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
827 }
828 err = -EINVAL;
829 if(semnum < 0 || semnum >= nsems)
830 goto out_unlock;
831
832 curr = &sma->sem_base[semnum];
833
834 switch (cmd) {
835 case GETVAL:
836 err = curr->semval;
837 goto out_unlock;
838 case GETPID:
839 err = curr->sempid;
840 goto out_unlock;
841 case GETNCNT:
842 err = count_semncnt(sma,semnum);
843 goto out_unlock;
844 case GETZCNT:
845 err = count_semzcnt(sma,semnum);
846 goto out_unlock;
847 case SETVAL:
848 {
849 int val = arg.val;
850 struct sem_undo *un;
851 err = -ERANGE;
852 if (val > SEMVMX || val < 0)
853 goto out_unlock;
854
855 for (un = sma->undo; un; un = un->id_next)
856 un->semadj[semnum] = 0;
857 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700858 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 sma->sem_ctime = get_seconds();
860 /* maybe some queued-up processes were waiting for this */
861 update_queue(sma);
862 err = 0;
863 goto out_unlock;
864 }
865 }
866out_unlock:
867 sem_unlock(sma);
868out_free:
869 if(sem_io != fast_sem_io)
870 ipc_free(sem_io, sizeof(ushort)*nsems);
871 return err;
872}
873
874struct sem_setbuf {
875 uid_t uid;
876 gid_t gid;
877 mode_t mode;
878};
879
880static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
881{
882 switch(version) {
883 case IPC_64:
884 {
885 struct semid64_ds tbuf;
886
887 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
888 return -EFAULT;
889
890 out->uid = tbuf.sem_perm.uid;
891 out->gid = tbuf.sem_perm.gid;
892 out->mode = tbuf.sem_perm.mode;
893
894 return 0;
895 }
896 case IPC_OLD:
897 {
898 struct semid_ds tbuf_old;
899
900 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
901 return -EFAULT;
902
903 out->uid = tbuf_old.sem_perm.uid;
904 out->gid = tbuf_old.sem_perm.gid;
905 out->mode = tbuf_old.sem_perm.mode;
906
907 return 0;
908 }
909 default:
910 return -EINVAL;
911 }
912}
913
Kirill Korotaeve3893532006-10-02 02:18:22 -0700914static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
915 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct sem_array *sma;
918 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400919 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct kern_ipc_perm *ipcp;
921
922 if(cmd == IPC_SET) {
923 if(copy_semid_from_user (&setbuf, arg.buf, version))
924 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700926 sma = sem_lock_check_down(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700927 if (IS_ERR(sma))
928 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400931
932 err = audit_ipc_obj(ipcp);
933 if (err)
934 goto out_unlock;
935
Linda Knippersac032212006-05-16 22:03:48 -0400936 if (cmd == IPC_SET) {
937 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
938 if (err)
939 goto out_unlock;
940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (current->euid != ipcp->cuid &&
942 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
943 err=-EPERM;
944 goto out_unlock;
945 }
946
947 err = security_sem_semctl(sma, cmd);
948 if (err)
949 goto out_unlock;
950
951 switch(cmd){
952 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700953 freeary(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 err = 0;
955 break;
956 case IPC_SET:
957 ipcp->uid = setbuf.uid;
958 ipcp->gid = setbuf.gid;
959 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
960 | (setbuf.mode & S_IRWXUGO);
961 sma->sem_ctime = get_seconds();
962 sem_unlock(sma);
963 err = 0;
964 break;
965 default:
966 sem_unlock(sma);
967 err = -EINVAL;
968 break;
969 }
970 return err;
971
972out_unlock:
973 sem_unlock(sma);
974 return err;
975}
976
977asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
978{
979 int err = -EINVAL;
980 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700981 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (semid < 0)
984 return -EINVAL;
985
986 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700987 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 switch(cmd) {
990 case IPC_INFO:
991 case SEM_INFO:
992 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700993 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return err;
995 case GETALL:
996 case GETVAL:
997 case GETPID:
998 case GETNCNT:
999 case GETZCNT:
1000 case IPC_STAT:
1001 case SETVAL:
1002 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -07001003 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return err;
1005 case IPC_RMID:
1006 case IPC_SET:
Nadia Derbey3e148c72007-10-18 23:40:54 -07001007 down_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001008 err = semctl_down(ns,semid,semnum,cmd,version,arg);
Nadia Derbey3e148c72007-10-18 23:40:54 -07001009 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return err;
1011 default:
1012 return -EINVAL;
1013 }
1014}
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016/* If the task doesn't already have a undo_list, then allocate one
1017 * here. We guarantee there is only one thread using this undo list,
1018 * and current is THE ONE
1019 *
1020 * If this allocation and assignment succeeds, but later
1021 * portions of this code fail, there is no need to free the sem_undo_list.
1022 * Just let it stay associated with the task, and it'll be freed later
1023 * at exit time.
1024 *
1025 * This can block, so callers must hold no locks.
1026 */
1027static inline int get_undo_list(struct sem_undo_list **undo_listp)
1028{
1029 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 undo_list = current->sysvsem.undo_list;
1032 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001033 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (undo_list == NULL)
1035 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001036 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 atomic_set(&undo_list->refcnt, 1);
1038 current->sysvsem.undo_list = undo_list;
1039 }
1040 *undo_listp = undo_list;
1041 return 0;
1042}
1043
1044static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1045{
1046 struct sem_undo **last, *un;
1047
1048 last = &ulp->proc_list;
1049 un = *last;
1050 while(un != NULL) {
1051 if(un->semid==semid)
1052 break;
1053 if(un->semid==-1) {
1054 *last=un->proc_next;
1055 kfree(un);
1056 } else {
1057 last=&un->proc_next;
1058 }
1059 un=*last;
1060 }
1061 return un;
1062}
1063
Kirill Korotaeve3893532006-10-02 02:18:22 -07001064static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct sem_array *sma;
1067 struct sem_undo_list *ulp;
1068 struct sem_undo *un, *new;
1069 int nsems;
1070 int error;
1071
1072 error = get_undo_list(&ulp);
1073 if (error)
1074 return ERR_PTR(error);
1075
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001076 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001078 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (likely(un!=NULL))
1080 goto out;
1081
1082 /* no undo structure around - allocate one. */
Nadia Derbey023a5352007-10-18 23:40:51 -07001083 sma = sem_lock_check(ns, semid);
1084 if (IS_ERR(sma))
1085 return ERR_PTR(PTR_ERR(sma));
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 nsems = sma->sem_nsems;
1088 ipc_rcu_getref(sma);
1089 sem_unlock(sma);
1090
Burman Yan4668edc2006-12-06 20:38:51 -08001091 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!new) {
1093 ipc_lock_by_ptr(&sma->sem_perm);
1094 ipc_rcu_putref(sma);
1095 sem_unlock(sma);
1096 return ERR_PTR(-ENOMEM);
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 new->semadj = (short *) &new[1];
1099 new->semid = semid;
1100
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001101 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 un = lookup_undo(ulp, semid);
1103 if (un) {
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001104 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 kfree(new);
1106 ipc_lock_by_ptr(&sma->sem_perm);
1107 ipc_rcu_putref(sma);
1108 sem_unlock(sma);
1109 goto out;
1110 }
1111 ipc_lock_by_ptr(&sma->sem_perm);
1112 ipc_rcu_putref(sma);
1113 if (sma->sem_perm.deleted) {
1114 sem_unlock(sma);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001115 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 kfree(new);
1117 un = ERR_PTR(-EIDRM);
1118 goto out;
1119 }
1120 new->proc_next = ulp->proc_list;
1121 ulp->proc_list = new;
1122 new->id_next = sma->undo;
1123 sma->undo = new;
1124 sem_unlock(sma);
1125 un = new;
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001126 spin_unlock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127out:
1128 return un;
1129}
1130
1131asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1132 unsigned nsops, const struct timespec __user *timeout)
1133{
1134 int error = -EINVAL;
1135 struct sem_array *sma;
1136 struct sembuf fast_sops[SEMOPM_FAST];
1137 struct sembuf* sops = fast_sops, *sop;
1138 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001139 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct sem_queue queue;
1141 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001142 struct ipc_namespace *ns;
1143
1144 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (nsops < 1 || semid < 0)
1147 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001148 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return -E2BIG;
1150 if(nsops > SEMOPM_FAST) {
1151 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1152 if(sops==NULL)
1153 return -ENOMEM;
1154 }
1155 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1156 error=-EFAULT;
1157 goto out_free;
1158 }
1159 if (timeout) {
1160 struct timespec _timeout;
1161 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1162 error = -EFAULT;
1163 goto out_free;
1164 }
1165 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1166 _timeout.tv_nsec >= 1000000000L) {
1167 error = -EINVAL;
1168 goto out_free;
1169 }
1170 jiffies_left = timespec_to_jiffies(&_timeout);
1171 }
1172 max = 0;
1173 for (sop = sops; sop < sops + nsops; sop++) {
1174 if (sop->sem_num >= max)
1175 max = sop->sem_num;
1176 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001177 undos = 1;
1178 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 alter = 1;
1180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182retry_undos:
1183 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001184 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (IS_ERR(un)) {
1186 error = PTR_ERR(un);
1187 goto out_free;
1188 }
1189 } else
1190 un = NULL;
1191
Nadia Derbey023a5352007-10-18 23:40:51 -07001192 sma = sem_lock_check(ns, semid);
1193 if (IS_ERR(sma)) {
1194 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001196 }
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /*
Nadia Derbey023a5352007-10-18 23:40:51 -07001199 * semid identifiers are not unique - find_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * allocated an undo structure, it was invalidated by an RMID
1201 * and now a new array with received the same id. Check and retry.
1202 */
1203 if (un && un->semid == -1) {
1204 sem_unlock(sma);
1205 goto retry_undos;
1206 }
1207 error = -EFBIG;
1208 if (max >= sma->sem_nsems)
1209 goto out_unlock_free;
1210
1211 error = -EACCES;
1212 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1213 goto out_unlock_free;
1214
1215 error = security_sem_semop(sma, sops, nsops, alter);
1216 if (error)
1217 goto out_unlock_free;
1218
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001219 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (error <= 0) {
1221 if (alter && error == 0)
1222 update_queue (sma);
1223 goto out_unlock_free;
1224 }
1225
1226 /* We need to sleep on this operation, so we put the current
1227 * task into the pending queue and go to sleep.
1228 */
1229
1230 queue.sma = sma;
1231 queue.sops = sops;
1232 queue.nsops = nsops;
1233 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001234 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 queue.id = semid;
1236 queue.alter = alter;
1237 if (alter)
1238 append_to_queue(sma ,&queue);
1239 else
1240 prepend_to_queue(sma ,&queue);
1241
1242 queue.status = -EINTR;
1243 queue.sleeper = current;
1244 current->state = TASK_INTERRUPTIBLE;
1245 sem_unlock(sma);
1246
1247 if (timeout)
1248 jiffies_left = schedule_timeout(jiffies_left);
1249 else
1250 schedule();
1251
1252 error = queue.status;
1253 while(unlikely(error == IN_WAKEUP)) {
1254 cpu_relax();
1255 error = queue.status;
1256 }
1257
1258 if (error != -EINTR) {
1259 /* fast path: update_queue already obtained all requested
1260 * resources */
1261 goto out_free;
1262 }
1263
Kirill Korotaeve3893532006-10-02 02:18:22 -07001264 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001265 if (IS_ERR(sma)) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001266 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 error = -EIDRM;
1268 goto out_free;
1269 }
1270
1271 /*
1272 * If queue.status != -EINTR we are woken up by another process
1273 */
1274 error = queue.status;
1275 if (error != -EINTR) {
1276 goto out_unlock_free;
1277 }
1278
1279 /*
1280 * If an interrupt occurred we have to clean up the queue
1281 */
1282 if (timeout && jiffies_left == 0)
1283 error = -EAGAIN;
1284 remove_from_queue(sma,&queue);
1285 goto out_unlock_free;
1286
1287out_unlock_free:
1288 sem_unlock(sma);
1289out_free:
1290 if(sops != fast_sops)
1291 kfree(sops);
1292 return error;
1293}
1294
1295asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1296{
1297 return sys_semtimedop(semid, tsops, nsops, NULL);
1298}
1299
1300/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1301 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
1303
1304int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1305{
1306 struct sem_undo_list *undo_list;
1307 int error;
1308
1309 if (clone_flags & CLONE_SYSVSEM) {
1310 error = get_undo_list(&undo_list);
1311 if (error)
1312 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 atomic_inc(&undo_list->refcnt);
1314 tsk->sysvsem.undo_list = undo_list;
1315 } else
1316 tsk->sysvsem.undo_list = NULL;
1317
1318 return 0;
1319}
1320
1321/*
1322 * add semadj values to semaphores, free undo structures.
1323 * undo structures are not freed when semaphore arrays are destroyed
1324 * so some of them may be out of date.
1325 * IMPLEMENTATION NOTE: There is some confusion over whether the
1326 * set of adjustments that needs to be done should be done in an atomic
1327 * manner or not. That is, if we are attempting to decrement the semval
1328 * should we queue up and wait until we can do so legally?
1329 * The original implementation attempted to do this (queue and wait).
1330 * The current implementation does not do so. The POSIX standard
1331 * and SVID should be consulted to determine what behavior is mandated.
1332 */
1333void exit_sem(struct task_struct *tsk)
1334{
1335 struct sem_undo_list *undo_list;
1336 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001337 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 undo_list = tsk->sysvsem.undo_list;
1340 if (!undo_list)
1341 return;
1342
1343 if (!atomic_dec_and_test(&undo_list->refcnt))
1344 return;
1345
Kirill Korotaeve3893532006-10-02 02:18:22 -07001346 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* There's no need to hold the semundo list lock, as current
1348 * is the last task exiting for this undo list.
1349 */
1350 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1351 struct sem_array *sma;
1352 int nsems, i;
1353 struct sem_undo *un, **unp;
1354 int semid;
1355
1356 semid = u->semid;
1357
1358 if(semid == -1)
1359 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001360 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001361 if (IS_ERR(sma))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 continue;
1363
1364 if (u->semid == -1)
1365 goto next_entry;
1366
Nadia Derbey1b531f22007-10-18 23:40:55 -07001367 BUG_ON(sem_checkid(sma, u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 /* remove u from the sma->undo list */
1370 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1371 if (u == un)
1372 goto found;
1373 }
1374 printk ("exit_sem undo list error id=%d\n", u->semid);
1375 goto next_entry;
1376found:
1377 *unp = un->id_next;
1378 /* perform adjustments registered in u */
1379 nsems = sma->sem_nsems;
1380 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001381 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001383 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /*
1385 * Range checks of the new semaphore value,
1386 * not defined by sus:
1387 * - Some unices ignore the undo entirely
1388 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1389 * - some cap the value (e.g. FreeBSD caps
1390 * at 0, but doesn't enforce SEMVMX)
1391 *
1392 * Linux caps the semaphore value, both at 0
1393 * and at SEMVMX.
1394 *
1395 * Manfred <manfred@colorfullife.com>
1396 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001397 if (semaphore->semval < 0)
1398 semaphore->semval = 0;
1399 if (semaphore->semval > SEMVMX)
1400 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001401 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 }
1404 sma->sem_otime = get_seconds();
1405 /* maybe some queued-up processes were waiting for this */
1406 update_queue(sma);
1407next_entry:
1408 sem_unlock(sma);
1409 }
1410 kfree(undo_list);
1411}
1412
1413#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001414static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Mike Waychison19b49462005-09-06 15:17:10 -07001416 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Mike Waychison19b49462005-09-06 15:17:10 -07001418 return seq_printf(s,
1419 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1420 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001421 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001422 sma->sem_perm.mode,
1423 sma->sem_nsems,
1424 sma->sem_perm.uid,
1425 sma->sem_perm.gid,
1426 sma->sem_perm.cuid,
1427 sma->sem_perm.cgid,
1428 sma->sem_otime,
1429 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431#endif