blob: 40ab34d832a611a28c70bdf3f9e997906ebe3829 [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>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <asm/uaccess.h>
87#include "util.h"
88
Kirill Korotaeve3893532006-10-02 02:18:22 -070089#define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Kirill Korotaeve3893532006-10-02 02:18:22 -070091#define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
Nadia Derbey1b531f22007-10-18 23:40:55 -070092#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
93#define sem_buildid(id, seq) ipc_buildid(id, seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kirill Korotaeve3893532006-10-02 02:18:22 -070095static struct ipc_ids init_sem_ids;
96
Nadia Derbey7748dbf2007-10-18 23:40:49 -070097static int newary(struct ipc_namespace *, struct ipc_params *);
Nadia Derbey7ca7e562007-10-18 23:40:48 -070098static void freeary(struct ipc_namespace *, struct sem_array *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700100static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
103#define SEMMSL_FAST 256 /* 512 bytes on stack */
104#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
105
106/*
107 * linked list protection:
108 * sem_undo.id_next,
109 * sem_array.sem_pending{,last},
110 * sem_array.sem_undo: sem_lock() for read/write
111 * sem_undo.proc_next: only "current" is allowed to read/write that field.
112 *
113 */
114
Kirill Korotaeve3893532006-10-02 02:18:22 -0700115#define sc_semmsl sem_ctls[0]
116#define sc_semmns sem_ctls[1]
117#define sc_semopm sem_ctls[2]
118#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -0700120static void __sem_init_ns(struct ipc_namespace *ns, struct ipc_ids *ids)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700121{
122 ns->ids[IPC_SEM_IDS] = ids;
123 ns->sc_semmsl = SEMMSL;
124 ns->sc_semmns = SEMMNS;
125 ns->sc_semopm = SEMOPM;
126 ns->sc_semmni = SEMMNI;
127 ns->used_sems = 0;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700128 ipc_init_ids(ids);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700129}
130
Kirill Korotaeve3893532006-10-02 02:18:22 -0700131int sem_init_ns(struct ipc_namespace *ns)
132{
133 struct ipc_ids *ids;
134
135 ids = kmalloc(sizeof(struct ipc_ids), GFP_KERNEL);
136 if (ids == NULL)
137 return -ENOMEM;
138
139 __sem_init_ns(ns, ids);
140 return 0;
141}
142
143void sem_exit_ns(struct ipc_namespace *ns)
144{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700145 struct sem_array *sma;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700146 int next_id;
147 int total, in_use;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700148
Nadia Derbey3e148c72007-10-18 23:40:54 -0700149 down_write(&sem_ids(ns).rw_mutex);
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700150
151 in_use = sem_ids(ns).in_use;
152
153 for (total = 0, next_id = 0; total < in_use; next_id++) {
154 sma = idr_find(&sem_ids(ns).ipcs_idr, next_id);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700155 if (sma == NULL)
156 continue;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700157 ipc_lock_by_ptr(&sma->sem_perm);
158 freeary(ns, sma);
159 total++;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700160 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700161 up_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700162
163 kfree(ns->ids[IPC_SEM_IDS]);
164 ns->ids[IPC_SEM_IDS] = NULL;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167void __init sem_init (void)
168{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700169 __sem_init_ns(&init_ipc_ns, &init_sem_ids);
Mike Waychison19b49462005-09-06 15:17:10 -0700170 ipc_init_proc_interface("sysvipc/sem",
171 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700172 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Nadia Derbey3e148c72007-10-18 23:40:54 -0700175/*
176 * This routine is called in the paths where the rw_mutex is held to protect
177 * access to the idr tree.
178 */
179static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns,
180 int id)
181{
182 struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id);
183
184 return container_of(ipcp, struct sem_array, sem_perm);
185}
186
187/*
188 * sem_lock_(check_) routines are called in the paths where the rw_mutex
189 * is not held.
190 */
Nadia Derbey023a5352007-10-18 23:40:51 -0700191static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id)
192{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700193 struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id);
194
195 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700196}
197
198static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns,
199 int id)
200{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700201 struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id);
202
203 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700204}
205
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700206static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
207{
208 ipc_rmid(&sem_ids(ns), &s->sem_perm);
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Lockless wakeup algorithm:
213 * Without the check/retry algorithm a lockless wakeup is possible:
214 * - queue.status is initialized to -EINTR before blocking.
215 * - wakeup is performed by
216 * * unlinking the queue entry from sma->sem_pending
217 * * setting queue.status to IN_WAKEUP
218 * This is the notification for the blocked thread that a
219 * result value is imminent.
220 * * call wake_up_process
221 * * set queue.status to the final value.
222 * - the previously blocked thread checks queue.status:
223 * * if it's IN_WAKEUP, then it must wait until the value changes
224 * * if it's not -EINTR, then the operation was completed by
225 * update_queue. semtimedop can return queue.status without
Ingo Molnar5f921ae2006-03-26 01:37:17 -0800226 * performing any operation on the sem array.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * * otherwise it must acquire the spinlock and check what's up.
228 *
229 * The two-stage algorithm is necessary to protect against the following
230 * races:
231 * - if queue.status is set after wake_up_process, then the woken up idle
232 * thread could race forward and try (and fail) to acquire sma->lock
233 * before update_queue had a chance to set queue.status
234 * - if queue.status is written before wake_up_process and if the
235 * blocked process is woken up by a signal between writing
236 * queue.status and the wake_up_process, then the woken up
237 * process could return from semtimedop and die by calling
238 * sys_exit before wake_up_process is called. Then wake_up_process
239 * will oops, because the task structure is already invalid.
240 * (yes, this happened on s390 with sysv msg).
241 *
242 */
243#define IN_WAKEUP 1
244
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700245/**
246 * newary - Create a new semaphore set
247 * @ns: namespace
248 * @params: ptr to the structure that contains key, semflg and nsems
249 *
Nadia Derbey3e148c72007-10-18 23:40:54 -0700250 * Called with sem_ids.rw_mutex held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700251 */
252
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700253static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int id;
256 int retval;
257 struct sem_array *sma;
258 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700259 key_t key = params->key;
260 int nsems = params->u.nsems;
261 int semflg = params->flg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 if (!nsems)
264 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700265 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ENOSPC;
267
268 size = sizeof (*sma) + nsems * sizeof (struct sem);
269 sma = ipc_rcu_alloc(size);
270 if (!sma) {
271 return -ENOMEM;
272 }
273 memset (sma, 0, size);
274
275 sma->sem_perm.mode = (semflg & S_IRWXUGO);
276 sma->sem_perm.key = key;
277
278 sma->sem_perm.security = NULL;
279 retval = security_sem_alloc(sma);
280 if (retval) {
281 ipc_rcu_putref(sma);
282 return retval;
283 }
284
Kirill Korotaeve3893532006-10-02 02:18:22 -0700285 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if(id == -1) {
287 security_sem_free(sma);
288 ipc_rcu_putref(sma);
289 return -ENOSPC;
290 }
Kirill Korotaeve3893532006-10-02 02:18:22 -0700291 ns->used_sems += nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Nadia Derbey1b531f22007-10-18 23:40:55 -0700293 sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 sma->sem_base = (struct sem *) &sma[1];
295 /* sma->sem_pending = NULL; */
296 sma->sem_pending_last = &sma->sem_pending;
297 /* sma->undo = NULL; */
298 sma->sem_nsems = nsems;
299 sma->sem_ctime = get_seconds();
300 sem_unlock(sma);
301
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700302 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700305
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700306/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700307 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700308 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700309static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700311 struct sem_array *sma;
312
313 sma = container_of(ipcp, struct sem_array, sem_perm);
314 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700315}
316
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700317/*
Nadia Derbey3e148c72007-10-18 23:40:54 -0700318 * Called with sem_ids.rw_mutex and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700319 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700320static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
321 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700322{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700323 struct sem_array *sma;
324
325 sma = container_of(ipcp, struct sem_array, sem_perm);
326 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700327 return -EINVAL;
328
329 return 0;
330}
331
332asmlinkage long sys_semget(key_t key, int nsems, int semflg)
333{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700334 struct ipc_namespace *ns;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700335 struct ipc_ops sem_ops;
336 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Kirill Korotaeve3893532006-10-02 02:18:22 -0700338 ns = current->nsproxy->ipc_ns;
339
340 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700342
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700343 sem_ops.getnew = newary;
344 sem_ops.associate = sem_security;
345 sem_ops.more_checks = sem_more_checks;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700346
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700347 sem_params.key = key;
348 sem_params.flg = semflg;
349 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700350
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700351 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/* Manage the doubly linked list sma->sem_pending as a FIFO:
355 * insert new queue elements at the tail sma->sem_pending_last.
356 */
357static inline void append_to_queue (struct sem_array * sma,
358 struct sem_queue * q)
359{
360 *(q->prev = sma->sem_pending_last) = q;
361 *(sma->sem_pending_last = &q->next) = NULL;
362}
363
364static inline void prepend_to_queue (struct sem_array * sma,
365 struct sem_queue * q)
366{
367 q->next = sma->sem_pending;
368 *(q->prev = &sma->sem_pending) = q;
369 if (q->next)
370 q->next->prev = &q->next;
371 else /* sma->sem_pending_last == &sma->sem_pending */
372 sma->sem_pending_last = &q->next;
373}
374
375static inline void remove_from_queue (struct sem_array * sma,
376 struct sem_queue * q)
377{
378 *(q->prev) = q->next;
379 if (q->next)
380 q->next->prev = q->prev;
381 else /* sma->sem_pending_last == &q->next */
382 sma->sem_pending_last = q->prev;
383 q->prev = NULL; /* mark as removed */
384}
385
386/*
387 * Determine whether a sequence of semaphore operations would succeed
388 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
389 */
390
391static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
392 int nsops, struct sem_undo *un, int pid)
393{
394 int result, sem_op;
395 struct sembuf *sop;
396 struct sem * curr;
397
398 for (sop = sops; sop < sops + nsops; sop++) {
399 curr = sma->sem_base + sop->sem_num;
400 sem_op = sop->sem_op;
401 result = curr->semval;
402
403 if (!sem_op && result)
404 goto would_block;
405
406 result += sem_op;
407 if (result < 0)
408 goto would_block;
409 if (result > SEMVMX)
410 goto out_of_range;
411 if (sop->sem_flg & SEM_UNDO) {
412 int undo = un->semadj[sop->sem_num] - sem_op;
413 /*
414 * Exceeding the undo range is an error.
415 */
416 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
417 goto out_of_range;
418 }
419 curr->semval = result;
420 }
421
422 sop--;
423 while (sop >= sops) {
424 sma->sem_base[sop->sem_num].sempid = pid;
425 if (sop->sem_flg & SEM_UNDO)
426 un->semadj[sop->sem_num] -= sop->sem_op;
427 sop--;
428 }
429
430 sma->sem_otime = get_seconds();
431 return 0;
432
433out_of_range:
434 result = -ERANGE;
435 goto undo;
436
437would_block:
438 if (sop->sem_flg & IPC_NOWAIT)
439 result = -EAGAIN;
440 else
441 result = 1;
442
443undo:
444 sop--;
445 while (sop >= sops) {
446 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
447 sop--;
448 }
449
450 return result;
451}
452
453/* Go through the pending queue for the indicated semaphore
454 * looking for tasks that can be completed.
455 */
456static void update_queue (struct sem_array * sma)
457{
458 int error;
459 struct sem_queue * q;
460
461 q = sma->sem_pending;
462 while(q) {
463 error = try_atomic_semop(sma, q->sops, q->nsops,
464 q->undo, q->pid);
465
466 /* Does q->sleeper still need to sleep? */
467 if (error <= 0) {
468 struct sem_queue *n;
469 remove_from_queue(sma,q);
470 q->status = IN_WAKEUP;
471 /*
472 * Continue scanning. The next operation
473 * that must be checked depends on the type of the
474 * completed operation:
475 * - if the operation modified the array, then
476 * restart from the head of the queue and
477 * check for threads that might be waiting
478 * for semaphore values to become 0.
479 * - if the operation didn't modify the array,
480 * then just continue.
481 */
482 if (q->alter)
483 n = sma->sem_pending;
484 else
485 n = q->next;
486 wake_up_process(q->sleeper);
487 /* hands-off: q will disappear immediately after
488 * writing q->status.
489 */
Linus Torvalds1224b372005-12-24 12:19:38 -0800490 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 q->status = error;
492 q = n;
493 } else {
494 q = q->next;
495 }
496 }
497}
498
499/* The following counts are associated to each semaphore:
500 * semncnt number of tasks waiting on semval being nonzero
501 * semzcnt number of tasks waiting on semval being zero
502 * This model assumes that a task waits on exactly one semaphore.
503 * Since semaphore operations are to be performed atomically, tasks actually
504 * wait on a whole sequence of semaphores simultaneously.
505 * The counts we return here are a rough approximation, but still
506 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
507 */
508static int count_semncnt (struct sem_array * sma, ushort semnum)
509{
510 int semncnt;
511 struct sem_queue * q;
512
513 semncnt = 0;
514 for (q = sma->sem_pending; q; q = q->next) {
515 struct sembuf * sops = q->sops;
516 int nsops = q->nsops;
517 int i;
518 for (i = 0; i < nsops; i++)
519 if (sops[i].sem_num == semnum
520 && (sops[i].sem_op < 0)
521 && !(sops[i].sem_flg & IPC_NOWAIT))
522 semncnt++;
523 }
524 return semncnt;
525}
526static int count_semzcnt (struct sem_array * sma, ushort semnum)
527{
528 int semzcnt;
529 struct sem_queue * q;
530
531 semzcnt = 0;
532 for (q = sma->sem_pending; q; q = q->next) {
533 struct sembuf * sops = q->sops;
534 int nsops = q->nsops;
535 int i;
536 for (i = 0; i < nsops; i++)
537 if (sops[i].sem_num == semnum
538 && (sops[i].sem_op == 0)
539 && !(sops[i].sem_flg & IPC_NOWAIT))
540 semzcnt++;
541 }
542 return semzcnt;
543}
544
Nadia Derbey3e148c72007-10-18 23:40:54 -0700545/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
546 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
547 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700549static void freeary(struct ipc_namespace *ns, struct sem_array *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct sem_undo *un;
552 struct sem_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Invalidate the existing undo structures for this semaphore set.
555 * (They will be freed without any further action in exit_sem()
556 * or during the next semop.)
557 */
558 for (un = sma->undo; un; un = un->id_next)
559 un->semid = -1;
560
561 /* Wake up all pending processes and let them fail with EIDRM. */
562 q = sma->sem_pending;
563 while(q) {
564 struct sem_queue *n;
565 /* lazy remove_from_queue: we are killing the whole queue */
566 q->prev = NULL;
567 n = q->next;
568 q->status = IN_WAKEUP;
569 wake_up_process(q->sleeper); /* doesn't sleep */
Manfred Spraul6003a932005-12-23 23:57:41 +0100570 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 q->status = -EIDRM; /* hands-off q */
572 q = n;
573 }
574
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700575 /* Remove the semaphore set from the IDR */
576 sem_rmid(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 sem_unlock(sma);
578
Kirill Korotaeve3893532006-10-02 02:18:22 -0700579 ns->used_sems -= sma->sem_nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 security_sem_free(sma);
581 ipc_rcu_putref(sma);
582}
583
584static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
585{
586 switch(version) {
587 case IPC_64:
588 return copy_to_user(buf, in, sizeof(*in));
589 case IPC_OLD:
590 {
591 struct semid_ds out;
592
593 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
594
595 out.sem_otime = in->sem_otime;
596 out.sem_ctime = in->sem_ctime;
597 out.sem_nsems = in->sem_nsems;
598
599 return copy_to_user(buf, &out, sizeof(out));
600 }
601 default:
602 return -EINVAL;
603 }
604}
605
Kirill Korotaeve3893532006-10-02 02:18:22 -0700606static int semctl_nolock(struct ipc_namespace *ns, int semid, int semnum,
607 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 int err = -EINVAL;
610 struct sem_array *sma;
611
612 switch(cmd) {
613 case IPC_INFO:
614 case SEM_INFO:
615 {
616 struct seminfo seminfo;
617 int max_id;
618
619 err = security_sem_semctl(NULL, cmd);
620 if (err)
621 return err;
622
623 memset(&seminfo,0,sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -0700624 seminfo.semmni = ns->sc_semmni;
625 seminfo.semmns = ns->sc_semmns;
626 seminfo.semmsl = ns->sc_semmsl;
627 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 seminfo.semvmx = SEMVMX;
629 seminfo.semmnu = SEMMNU;
630 seminfo.semmap = SEMMAP;
631 seminfo.semume = SEMUME;
Nadia Derbey3e148c72007-10-18 23:40:54 -0700632 down_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -0700634 seminfo.semusz = sem_ids(ns).in_use;
635 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 } else {
637 seminfo.semusz = SEMUSZ;
638 seminfo.semaem = SEMAEM;
639 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700640 max_id = ipc_get_maxid(&sem_ids(ns));
Nadia Derbey3e148c72007-10-18 23:40:54 -0700641 up_read(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
643 return -EFAULT;
644 return (max_id < 0) ? 0: max_id;
645 }
646 case SEM_STAT:
647 {
648 struct semid64_ds tbuf;
649 int id;
650
Kirill Korotaeve3893532006-10-02 02:18:22 -0700651 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700652 if (IS_ERR(sma))
653 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 err = -EACCES;
656 if (ipcperms (&sma->sem_perm, S_IRUGO))
657 goto out_unlock;
658
659 err = security_sem_semctl(sma, cmd);
660 if (err)
661 goto out_unlock;
662
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700663 id = sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Nadia Derbey023a5352007-10-18 23:40:51 -0700665 memset(&tbuf, 0, sizeof(tbuf));
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
668 tbuf.sem_otime = sma->sem_otime;
669 tbuf.sem_ctime = sma->sem_ctime;
670 tbuf.sem_nsems = sma->sem_nsems;
671 sem_unlock(sma);
672 if (copy_semid_to_user (arg.buf, &tbuf, version))
673 return -EFAULT;
674 return id;
675 }
676 default:
677 return -EINVAL;
678 }
679 return err;
680out_unlock:
681 sem_unlock(sma);
682 return err;
683}
684
Kirill Korotaeve3893532006-10-02 02:18:22 -0700685static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
686 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct sem_array *sma;
689 struct sem* curr;
690 int err;
691 ushort fast_sem_io[SEMMSL_FAST];
692 ushort* sem_io = fast_sem_io;
693 int nsems;
694
Nadia Derbey023a5352007-10-18 23:40:51 -0700695 sma = sem_lock_check(ns, semid);
696 if (IS_ERR(sma))
697 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 nsems = sma->sem_nsems;
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 err = -EACCES;
702 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
703 goto out_unlock;
704
705 err = security_sem_semctl(sma, cmd);
706 if (err)
707 goto out_unlock;
708
709 err = -EACCES;
710 switch (cmd) {
711 case GETALL:
712 {
713 ushort __user *array = arg.array;
714 int i;
715
716 if(nsems > SEMMSL_FAST) {
717 ipc_rcu_getref(sma);
718 sem_unlock(sma);
719
720 sem_io = ipc_alloc(sizeof(ushort)*nsems);
721 if(sem_io == NULL) {
722 ipc_lock_by_ptr(&sma->sem_perm);
723 ipc_rcu_putref(sma);
724 sem_unlock(sma);
725 return -ENOMEM;
726 }
727
728 ipc_lock_by_ptr(&sma->sem_perm);
729 ipc_rcu_putref(sma);
730 if (sma->sem_perm.deleted) {
731 sem_unlock(sma);
732 err = -EIDRM;
733 goto out_free;
734 }
735 }
736
737 for (i = 0; i < sma->sem_nsems; i++)
738 sem_io[i] = sma->sem_base[i].semval;
739 sem_unlock(sma);
740 err = 0;
741 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
742 err = -EFAULT;
743 goto out_free;
744 }
745 case SETALL:
746 {
747 int i;
748 struct sem_undo *un;
749
750 ipc_rcu_getref(sma);
751 sem_unlock(sma);
752
753 if(nsems > SEMMSL_FAST) {
754 sem_io = ipc_alloc(sizeof(ushort)*nsems);
755 if(sem_io == NULL) {
756 ipc_lock_by_ptr(&sma->sem_perm);
757 ipc_rcu_putref(sma);
758 sem_unlock(sma);
759 return -ENOMEM;
760 }
761 }
762
763 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
764 ipc_lock_by_ptr(&sma->sem_perm);
765 ipc_rcu_putref(sma);
766 sem_unlock(sma);
767 err = -EFAULT;
768 goto out_free;
769 }
770
771 for (i = 0; i < nsems; i++) {
772 if (sem_io[i] > SEMVMX) {
773 ipc_lock_by_ptr(&sma->sem_perm);
774 ipc_rcu_putref(sma);
775 sem_unlock(sma);
776 err = -ERANGE;
777 goto out_free;
778 }
779 }
780 ipc_lock_by_ptr(&sma->sem_perm);
781 ipc_rcu_putref(sma);
782 if (sma->sem_perm.deleted) {
783 sem_unlock(sma);
784 err = -EIDRM;
785 goto out_free;
786 }
787
788 for (i = 0; i < nsems; i++)
789 sma->sem_base[i].semval = sem_io[i];
790 for (un = sma->undo; un; un = un->id_next)
791 for (i = 0; i < nsems; i++)
792 un->semadj[i] = 0;
793 sma->sem_ctime = get_seconds();
794 /* maybe some queued-up processes were waiting for this */
795 update_queue(sma);
796 err = 0;
797 goto out_unlock;
798 }
799 case IPC_STAT:
800 {
801 struct semid64_ds tbuf;
802 memset(&tbuf,0,sizeof(tbuf));
803 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
804 tbuf.sem_otime = sma->sem_otime;
805 tbuf.sem_ctime = sma->sem_ctime;
806 tbuf.sem_nsems = sma->sem_nsems;
807 sem_unlock(sma);
808 if (copy_semid_to_user (arg.buf, &tbuf, version))
809 return -EFAULT;
810 return 0;
811 }
812 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
813 }
814 err = -EINVAL;
815 if(semnum < 0 || semnum >= nsems)
816 goto out_unlock;
817
818 curr = &sma->sem_base[semnum];
819
820 switch (cmd) {
821 case GETVAL:
822 err = curr->semval;
823 goto out_unlock;
824 case GETPID:
825 err = curr->sempid;
826 goto out_unlock;
827 case GETNCNT:
828 err = count_semncnt(sma,semnum);
829 goto out_unlock;
830 case GETZCNT:
831 err = count_semzcnt(sma,semnum);
832 goto out_unlock;
833 case SETVAL:
834 {
835 int val = arg.val;
836 struct sem_undo *un;
837 err = -ERANGE;
838 if (val > SEMVMX || val < 0)
839 goto out_unlock;
840
841 for (un = sma->undo; un; un = un->id_next)
842 un->semadj[semnum] = 0;
843 curr->semval = val;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700844 curr->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 sma->sem_ctime = get_seconds();
846 /* maybe some queued-up processes were waiting for this */
847 update_queue(sma);
848 err = 0;
849 goto out_unlock;
850 }
851 }
852out_unlock:
853 sem_unlock(sma);
854out_free:
855 if(sem_io != fast_sem_io)
856 ipc_free(sem_io, sizeof(ushort)*nsems);
857 return err;
858}
859
860struct sem_setbuf {
861 uid_t uid;
862 gid_t gid;
863 mode_t mode;
864};
865
866static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version)
867{
868 switch(version) {
869 case IPC_64:
870 {
871 struct semid64_ds tbuf;
872
873 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
874 return -EFAULT;
875
876 out->uid = tbuf.sem_perm.uid;
877 out->gid = tbuf.sem_perm.gid;
878 out->mode = tbuf.sem_perm.mode;
879
880 return 0;
881 }
882 case IPC_OLD:
883 {
884 struct semid_ds tbuf_old;
885
886 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
887 return -EFAULT;
888
889 out->uid = tbuf_old.sem_perm.uid;
890 out->gid = tbuf_old.sem_perm.gid;
891 out->mode = tbuf_old.sem_perm.mode;
892
893 return 0;
894 }
895 default:
896 return -EINVAL;
897 }
898}
899
Kirill Korotaeve3893532006-10-02 02:18:22 -0700900static int semctl_down(struct ipc_namespace *ns, int semid, int semnum,
901 int cmd, int version, union semun arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct sem_array *sma;
904 int err;
Jeff Garzik8e1c0912007-07-17 05:40:59 -0400905 struct sem_setbuf uninitialized_var(setbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct kern_ipc_perm *ipcp;
907
908 if(cmd == IPC_SET) {
909 if(copy_semid_from_user (&setbuf, arg.buf, version))
910 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Nadia Derbey3e148c72007-10-18 23:40:54 -0700912 sma = sem_lock_check_down(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -0700913 if (IS_ERR(sma))
914 return PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 ipcp = &sma->sem_perm;
Steve Grubb073115d2006-04-02 17:07:33 -0400917
918 err = audit_ipc_obj(ipcp);
919 if (err)
920 goto out_unlock;
921
Linda Knippersac032212006-05-16 22:03:48 -0400922 if (cmd == IPC_SET) {
923 err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode);
924 if (err)
925 goto out_unlock;
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (current->euid != ipcp->cuid &&
928 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
929 err=-EPERM;
930 goto out_unlock;
931 }
932
933 err = security_sem_semctl(sma, cmd);
934 if (err)
935 goto out_unlock;
936
937 switch(cmd){
938 case IPC_RMID:
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700939 freeary(ns, sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 err = 0;
941 break;
942 case IPC_SET:
943 ipcp->uid = setbuf.uid;
944 ipcp->gid = setbuf.gid;
945 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
946 | (setbuf.mode & S_IRWXUGO);
947 sma->sem_ctime = get_seconds();
948 sem_unlock(sma);
949 err = 0;
950 break;
951 default:
952 sem_unlock(sma);
953 err = -EINVAL;
954 break;
955 }
956 return err;
957
958out_unlock:
959 sem_unlock(sma);
960 return err;
961}
962
963asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
964{
965 int err = -EINVAL;
966 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700967 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (semid < 0)
970 return -EINVAL;
971
972 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700973 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 switch(cmd) {
976 case IPC_INFO:
977 case SEM_INFO:
978 case SEM_STAT:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700979 err = semctl_nolock(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return err;
981 case GETALL:
982 case GETVAL:
983 case GETPID:
984 case GETNCNT:
985 case GETZCNT:
986 case IPC_STAT:
987 case SETVAL:
988 case SETALL:
Kirill Korotaeve3893532006-10-02 02:18:22 -0700989 err = semctl_main(ns,semid,semnum,cmd,version,arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return err;
991 case IPC_RMID:
992 case IPC_SET:
Nadia Derbey3e148c72007-10-18 23:40:54 -0700993 down_write(&sem_ids(ns).rw_mutex);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700994 err = semctl_down(ns,semid,semnum,cmd,version,arg);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700995 up_write(&sem_ids(ns).rw_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return err;
997 default:
998 return -EINVAL;
999 }
1000}
1001
1002static inline void lock_semundo(void)
1003{
1004 struct sem_undo_list *undo_list;
1005
1006 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001007 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock(&undo_list->lock);
1009}
1010
1011/* This code has an interaction with copy_semundo().
1012 * Consider; two tasks are sharing the undo_list. task1
1013 * acquires the undo_list lock in lock_semundo(). If task2 now
1014 * exits before task1 releases the lock (by calling
1015 * unlock_semundo()), then task1 will never call spin_unlock().
1016 * This leave the sem_undo_list in a locked state. If task1 now creats task3
1017 * and once again shares the sem_undo_list, the sem_undo_list will still be
1018 * locked, and future SEM_UNDO operations will deadlock. This case is
1019 * dealt with in copy_semundo() by having it reinitialize the spin lock when
1020 * the refcnt goes from 1 to 2.
1021 */
1022static inline void unlock_semundo(void)
1023{
1024 struct sem_undo_list *undo_list;
1025
1026 undo_list = current->sysvsem.undo_list;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001027 if (undo_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 spin_unlock(&undo_list->lock);
1029}
1030
1031
1032/* If the task doesn't already have a undo_list, then allocate one
1033 * here. We guarantee there is only one thread using this undo list,
1034 * and current is THE ONE
1035 *
1036 * If this allocation and assignment succeeds, but later
1037 * portions of this code fail, there is no need to free the sem_undo_list.
1038 * Just let it stay associated with the task, and it'll be freed later
1039 * at exit time.
1040 *
1041 * This can block, so callers must hold no locks.
1042 */
1043static inline int get_undo_list(struct sem_undo_list **undo_listp)
1044{
1045 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 undo_list = current->sysvsem.undo_list;
1048 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001049 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (undo_list == NULL)
1051 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001052 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 atomic_set(&undo_list->refcnt, 1);
1054 current->sysvsem.undo_list = undo_list;
1055 }
1056 *undo_listp = undo_list;
1057 return 0;
1058}
1059
1060static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1061{
1062 struct sem_undo **last, *un;
1063
1064 last = &ulp->proc_list;
1065 un = *last;
1066 while(un != NULL) {
1067 if(un->semid==semid)
1068 break;
1069 if(un->semid==-1) {
1070 *last=un->proc_next;
1071 kfree(un);
1072 } else {
1073 last=&un->proc_next;
1074 }
1075 un=*last;
1076 }
1077 return un;
1078}
1079
Kirill Korotaeve3893532006-10-02 02:18:22 -07001080static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct sem_array *sma;
1083 struct sem_undo_list *ulp;
1084 struct sem_undo *un, *new;
1085 int nsems;
1086 int error;
1087
1088 error = get_undo_list(&ulp);
1089 if (error)
1090 return ERR_PTR(error);
1091
1092 lock_semundo();
1093 un = lookup_undo(ulp, semid);
1094 unlock_semundo();
1095 if (likely(un!=NULL))
1096 goto out;
1097
1098 /* no undo structure around - allocate one. */
Nadia Derbey023a5352007-10-18 23:40:51 -07001099 sma = sem_lock_check(ns, semid);
1100 if (IS_ERR(sma))
1101 return ERR_PTR(PTR_ERR(sma));
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 nsems = sma->sem_nsems;
1104 ipc_rcu_getref(sma);
1105 sem_unlock(sma);
1106
Burman Yan4668edc2006-12-06 20:38:51 -08001107 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (!new) {
1109 ipc_lock_by_ptr(&sma->sem_perm);
1110 ipc_rcu_putref(sma);
1111 sem_unlock(sma);
1112 return ERR_PTR(-ENOMEM);
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 new->semadj = (short *) &new[1];
1115 new->semid = semid;
1116
1117 lock_semundo();
1118 un = lookup_undo(ulp, semid);
1119 if (un) {
1120 unlock_semundo();
1121 kfree(new);
1122 ipc_lock_by_ptr(&sma->sem_perm);
1123 ipc_rcu_putref(sma);
1124 sem_unlock(sma);
1125 goto out;
1126 }
1127 ipc_lock_by_ptr(&sma->sem_perm);
1128 ipc_rcu_putref(sma);
1129 if (sma->sem_perm.deleted) {
1130 sem_unlock(sma);
1131 unlock_semundo();
1132 kfree(new);
1133 un = ERR_PTR(-EIDRM);
1134 goto out;
1135 }
1136 new->proc_next = ulp->proc_list;
1137 ulp->proc_list = new;
1138 new->id_next = sma->undo;
1139 sma->undo = new;
1140 sem_unlock(sma);
1141 un = new;
1142 unlock_semundo();
1143out:
1144 return un;
1145}
1146
1147asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops,
1148 unsigned nsops, const struct timespec __user *timeout)
1149{
1150 int error = -EINVAL;
1151 struct sem_array *sma;
1152 struct sembuf fast_sops[SEMOPM_FAST];
1153 struct sembuf* sops = fast_sops, *sop;
1154 struct sem_undo *un;
Manfred Spraulb78755a2005-06-23 00:10:06 -07001155 int undos = 0, alter = 0, max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct sem_queue queue;
1157 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001158 struct ipc_namespace *ns;
1159
1160 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (nsops < 1 || semid < 0)
1163 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001164 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return -E2BIG;
1166 if(nsops > SEMOPM_FAST) {
1167 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1168 if(sops==NULL)
1169 return -ENOMEM;
1170 }
1171 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1172 error=-EFAULT;
1173 goto out_free;
1174 }
1175 if (timeout) {
1176 struct timespec _timeout;
1177 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1178 error = -EFAULT;
1179 goto out_free;
1180 }
1181 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1182 _timeout.tv_nsec >= 1000000000L) {
1183 error = -EINVAL;
1184 goto out_free;
1185 }
1186 jiffies_left = timespec_to_jiffies(&_timeout);
1187 }
1188 max = 0;
1189 for (sop = sops; sop < sops + nsops; sop++) {
1190 if (sop->sem_num >= max)
1191 max = sop->sem_num;
1192 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001193 undos = 1;
1194 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 alter = 1;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198retry_undos:
1199 if (undos) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001200 un = find_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (IS_ERR(un)) {
1202 error = PTR_ERR(un);
1203 goto out_free;
1204 }
1205 } else
1206 un = NULL;
1207
Nadia Derbey023a5352007-10-18 23:40:51 -07001208 sma = sem_lock_check(ns, semid);
1209 if (IS_ERR(sma)) {
1210 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001212 }
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 /*
Nadia Derbey023a5352007-10-18 23:40:51 -07001215 * semid identifiers are not unique - find_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * allocated an undo structure, it was invalidated by an RMID
1217 * and now a new array with received the same id. Check and retry.
1218 */
1219 if (un && un->semid == -1) {
1220 sem_unlock(sma);
1221 goto retry_undos;
1222 }
1223 error = -EFBIG;
1224 if (max >= sma->sem_nsems)
1225 goto out_unlock_free;
1226
1227 error = -EACCES;
1228 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1229 goto out_unlock_free;
1230
1231 error = security_sem_semop(sma, sops, nsops, alter);
1232 if (error)
1233 goto out_unlock_free;
1234
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001235 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (error <= 0) {
1237 if (alter && error == 0)
1238 update_queue (sma);
1239 goto out_unlock_free;
1240 }
1241
1242 /* We need to sleep on this operation, so we put the current
1243 * task into the pending queue and go to sleep.
1244 */
1245
1246 queue.sma = sma;
1247 queue.sops = sops;
1248 queue.nsops = nsops;
1249 queue.undo = un;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001250 queue.pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 queue.id = semid;
1252 queue.alter = alter;
1253 if (alter)
1254 append_to_queue(sma ,&queue);
1255 else
1256 prepend_to_queue(sma ,&queue);
1257
1258 queue.status = -EINTR;
1259 queue.sleeper = current;
1260 current->state = TASK_INTERRUPTIBLE;
1261 sem_unlock(sma);
1262
1263 if (timeout)
1264 jiffies_left = schedule_timeout(jiffies_left);
1265 else
1266 schedule();
1267
1268 error = queue.status;
1269 while(unlikely(error == IN_WAKEUP)) {
1270 cpu_relax();
1271 error = queue.status;
1272 }
1273
1274 if (error != -EINTR) {
1275 /* fast path: update_queue already obtained all requested
1276 * resources */
1277 goto out_free;
1278 }
1279
Kirill Korotaeve3893532006-10-02 02:18:22 -07001280 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001281 if (IS_ERR(sma)) {
Eric Sesterhenn27315c92006-03-26 18:28:38 +02001282 BUG_ON(queue.prev != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 error = -EIDRM;
1284 goto out_free;
1285 }
1286
1287 /*
1288 * If queue.status != -EINTR we are woken up by another process
1289 */
1290 error = queue.status;
1291 if (error != -EINTR) {
1292 goto out_unlock_free;
1293 }
1294
1295 /*
1296 * If an interrupt occurred we have to clean up the queue
1297 */
1298 if (timeout && jiffies_left == 0)
1299 error = -EAGAIN;
1300 remove_from_queue(sma,&queue);
1301 goto out_unlock_free;
1302
1303out_unlock_free:
1304 sem_unlock(sma);
1305out_free:
1306 if(sops != fast_sops)
1307 kfree(sops);
1308 return error;
1309}
1310
1311asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops)
1312{
1313 return sys_semtimedop(semid, tsops, nsops, NULL);
1314}
1315
1316/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1317 * parent and child tasks.
1318 *
1319 * See the notes above unlock_semundo() regarding the spin_lock_init()
1320 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1321 * because of the reasoning in the comment above unlock_semundo.
1322 */
1323
1324int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1325{
1326 struct sem_undo_list *undo_list;
1327 int error;
1328
1329 if (clone_flags & CLONE_SYSVSEM) {
1330 error = get_undo_list(&undo_list);
1331 if (error)
1332 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 atomic_inc(&undo_list->refcnt);
1334 tsk->sysvsem.undo_list = undo_list;
1335 } else
1336 tsk->sysvsem.undo_list = NULL;
1337
1338 return 0;
1339}
1340
1341/*
1342 * add semadj values to semaphores, free undo structures.
1343 * undo structures are not freed when semaphore arrays are destroyed
1344 * so some of them may be out of date.
1345 * IMPLEMENTATION NOTE: There is some confusion over whether the
1346 * set of adjustments that needs to be done should be done in an atomic
1347 * manner or not. That is, if we are attempting to decrement the semval
1348 * should we queue up and wait until we can do so legally?
1349 * The original implementation attempted to do this (queue and wait).
1350 * The current implementation does not do so. The POSIX standard
1351 * and SVID should be consulted to determine what behavior is mandated.
1352 */
1353void exit_sem(struct task_struct *tsk)
1354{
1355 struct sem_undo_list *undo_list;
1356 struct sem_undo *u, **up;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001357 struct ipc_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 undo_list = tsk->sysvsem.undo_list;
1360 if (!undo_list)
1361 return;
1362
1363 if (!atomic_dec_and_test(&undo_list->refcnt))
1364 return;
1365
Kirill Korotaeve3893532006-10-02 02:18:22 -07001366 ns = tsk->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* There's no need to hold the semundo list lock, as current
1368 * is the last task exiting for this undo list.
1369 */
1370 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1371 struct sem_array *sma;
1372 int nsems, i;
1373 struct sem_undo *un, **unp;
1374 int semid;
1375
1376 semid = u->semid;
1377
1378 if(semid == -1)
1379 continue;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001380 sma = sem_lock(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001381 if (IS_ERR(sma))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 continue;
1383
1384 if (u->semid == -1)
1385 goto next_entry;
1386
Nadia Derbey1b531f22007-10-18 23:40:55 -07001387 BUG_ON(sem_checkid(sma, u->semid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /* remove u from the sma->undo list */
1390 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1391 if (u == un)
1392 goto found;
1393 }
1394 printk ("exit_sem undo list error id=%d\n", u->semid);
1395 goto next_entry;
1396found:
1397 *unp = un->id_next;
1398 /* perform adjustments registered in u */
1399 nsems = sma->sem_nsems;
1400 for (i = 0; i < nsems; i++) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001401 struct sem * semaphore = &sma->sem_base[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 if (u->semadj[i]) {
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001403 semaphore->semval += u->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
1405 * Range checks of the new semaphore value,
1406 * not defined by sus:
1407 * - Some unices ignore the undo entirely
1408 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1409 * - some cap the value (e.g. FreeBSD caps
1410 * at 0, but doesn't enforce SEMVMX)
1411 *
1412 * Linux caps the semaphore value, both at 0
1413 * and at SEMVMX.
1414 *
1415 * Manfred <manfred@colorfullife.com>
1416 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08001417 if (semaphore->semval < 0)
1418 semaphore->semval = 0;
1419 if (semaphore->semval > SEMVMX)
1420 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001421 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423 }
1424 sma->sem_otime = get_seconds();
1425 /* maybe some queued-up processes were waiting for this */
1426 update_queue(sma);
1427next_entry:
1428 sem_unlock(sma);
1429 }
1430 kfree(undo_list);
1431}
1432
1433#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07001434static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Mike Waychison19b49462005-09-06 15:17:10 -07001436 struct sem_array *sma = it;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Mike Waychison19b49462005-09-06 15:17:10 -07001438 return seq_printf(s,
1439 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1440 sma->sem_perm.key,
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001441 sma->sem_perm.id,
Mike Waychison19b49462005-09-06 15:17:10 -07001442 sma->sem_perm.mode,
1443 sma->sem_nsems,
1444 sma->sem_perm.uid,
1445 sma->sem_perm.gid,
1446 sma->sem_perm.cuid,
1447 sma->sem_perm.cgid,
1448 sma->sem_otime,
1449 sma->sem_ctime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451#endif