ipc: move rcu_read_unlock() out of sem_unlock() and into callers
[deliverable/linux.git] / ipc / sem.c
CommitLineData
1da177e4
LT
1/*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
1da177e4
LT
6 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
7 *
8 * SMP-threaded, sysctl's added
624dffcb 9 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
1da177e4 10 * Enforced range limit on SEM_UNDO
046c6884 11 * (c) 2001 Red Hat Inc
1da177e4
LT
12 * Lockless wakeup
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
c5cf6359
MS
14 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
073115d6
SG
16 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
e3893534
KK
19 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
c5cf6359
MS
23 *
24 * Implementation notes: (May 2010)
25 * This file implements System V semaphores.
26 *
27 * User space visible behavior:
28 * - FIFO ordering for semop() operations (just FIFO, not starvation
29 * protection)
30 * - multiple semaphore operations that alter the same semaphore in
31 * one semop() are handled.
32 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
33 * SETALL calls.
34 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
35 * - undo adjustments at process exit are limited to 0..SEMVMX.
36 * - namespace are supported.
37 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
38 * to /proc/sys/kernel/sem.
39 * - statistics about the usage are reported in /proc/sysvipc/sem.
40 *
41 * Internals:
42 * - scalability:
43 * - all global variables are read-mostly.
44 * - semop() calls and semctl(RMID) are synchronized by RCU.
45 * - most operations do write operations (actually: spin_lock calls) to
46 * the per-semaphore array structure.
47 * Thus: Perfect SMP scaling between independent semaphore arrays.
48 * If multiple semaphores in one array are used, then cache line
49 * trashing on the semaphore array spinlock will limit the scaling.
50 * - semncnt and semzcnt are calculated on demand in count_semncnt() and
51 * count_semzcnt()
52 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
57 * dropping all locks. (see wake_up_sem_queue_prepare(),
58 * wake_up_sem_queue_do())
59 * - All work is done by the waker, the woken up task does not have to do
60 * anything - not even acquiring a lock or dropping a refcount.
61 * - A woken up task may not even touch the semaphore array anymore, it may
62 * have been destroyed already by a semctl(RMID).
63 * - The synchronizations between wake-ups due to a timeout/signal and a
64 * wake-up due to a completed semaphore operation is achieved by using an
65 * intermediate state (IN_WAKEUP).
66 * - UNDO values are stored in an array (one per process and per
67 * semaphore array, lazily allocated). For backwards compatibility, multiple
68 * modes for the UNDO variables are supported (per process, per thread)
69 * (see copy_semundo, CLONE_SYSVSEM)
70 * - There are two lists of the pending operations: a per-array list
71 * and per-semaphore list (stored in the array). This allows to achieve FIFO
72 * ordering without always scanning all pending operations.
73 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
1da177e4
LT
74 */
75
1da177e4
LT
76#include <linux/slab.h>
77#include <linux/spinlock.h>
78#include <linux/init.h>
79#include <linux/proc_fs.h>
80#include <linux/time.h>
1da177e4
LT
81#include <linux/security.h>
82#include <linux/syscalls.h>
83#include <linux/audit.h>
c59ede7b 84#include <linux/capability.h>
19b4946c 85#include <linux/seq_file.h>
3e148c79 86#include <linux/rwsem.h>
e3893534 87#include <linux/nsproxy.h>
ae5e1b22 88#include <linux/ipc_namespace.h>
5f921ae9 89
1da177e4
LT
90#include <asm/uaccess.h>
91#include "util.h"
92
e57940d7
MS
93/* One semaphore structure for each semaphore in the system. */
94struct sem {
95 int semval; /* current value */
96 int sempid; /* pid of last operation */
6062a8dc 97 spinlock_t lock; /* spinlock for fine-grained semtimedop */
e57940d7
MS
98 struct list_head sem_pending; /* pending single-sop operations */
99};
100
101/* One queue for each sleeping process in the system. */
102struct sem_queue {
e57940d7
MS
103 struct list_head list; /* queue of pending operations */
104 struct task_struct *sleeper; /* this process */
105 struct sem_undo *undo; /* undo structure */
106 int pid; /* process id of requesting process */
107 int status; /* completion status of operation */
108 struct sembuf *sops; /* array of pending operations */
109 int nsops; /* number of operations */
110 int alter; /* does *sops alter the array? */
111};
112
113/* Each task has a list of undo requests. They are executed automatically
114 * when the process exits.
115 */
116struct sem_undo {
117 struct list_head list_proc; /* per-process list: *
118 * all undos from one process
119 * rcu protected */
120 struct rcu_head rcu; /* rcu struct for sem_undo */
121 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
122 struct list_head list_id; /* per semaphore array list:
123 * all undos for one array */
124 int semid; /* semaphore set identifier */
125 short *semadj; /* array of adjustments */
126 /* one per semaphore */
127};
128
129/* sem_undo_list controls shared access to the list of sem_undo structures
130 * that may be shared among all a CLONE_SYSVSEM task group.
131 */
132struct sem_undo_list {
133 atomic_t refcnt;
134 spinlock_t lock;
135 struct list_head list_proc;
136};
137
138
ed2ddbf8 139#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
e3893534 140
1b531f21 141#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
1da177e4 142
7748dbfa 143static int newary(struct ipc_namespace *, struct ipc_params *);
01b8b07a 144static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
1da177e4 145#ifdef CONFIG_PROC_FS
19b4946c 146static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
1da177e4
LT
147#endif
148
149#define SEMMSL_FAST 256 /* 512 bytes on stack */
150#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
151
152/*
153 * linked list protection:
154 * sem_undo.id_next,
155 * sem_array.sem_pending{,last},
156 * sem_array.sem_undo: sem_lock() for read/write
157 * sem_undo.proc_next: only "current" is allowed to read/write that field.
158 *
159 */
160
e3893534
KK
161#define sc_semmsl sem_ctls[0]
162#define sc_semmns sem_ctls[1]
163#define sc_semopm sem_ctls[2]
164#define sc_semmni sem_ctls[3]
165
ed2ddbf8 166void sem_init_ns(struct ipc_namespace *ns)
e3893534 167{
e3893534
KK
168 ns->sc_semmsl = SEMMSL;
169 ns->sc_semmns = SEMMNS;
170 ns->sc_semopm = SEMOPM;
171 ns->sc_semmni = SEMMNI;
172 ns->used_sems = 0;
ed2ddbf8 173 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
e3893534
KK
174}
175
ae5e1b22 176#ifdef CONFIG_IPC_NS
e3893534
KK
177void sem_exit_ns(struct ipc_namespace *ns)
178{
01b8b07a 179 free_ipcs(ns, &sem_ids(ns), freeary);
7d6feeb2 180 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
e3893534 181}
ae5e1b22 182#endif
1da177e4
LT
183
184void __init sem_init (void)
185{
ed2ddbf8 186 sem_init_ns(&init_ipc_ns);
19b4946c
MW
187 ipc_init_proc_interface("sysvipc/sem",
188 " key semid perms nsems uid gid cuid cgid otime ctime\n",
e3893534 189 IPC_SEM_IDS, sysvipc_sem_proc_show);
1da177e4
LT
190}
191
6062a8dc
RR
192/*
193 * If the request contains only one semaphore operation, and there are
194 * no complex transactions pending, lock only the semaphore involved.
195 * Otherwise, lock the entire semaphore array, since we either have
196 * multiple semaphores in our own semops, or we need to look at
197 * semaphores from other pending complex operations.
198 *
199 * Carefully guard against sma->complex_count changing between zero
200 * and non-zero while we are spinning for the lock. The value of
201 * sma->complex_count cannot change while we are holding the lock,
202 * so sem_unlock should be fine.
203 *
204 * The global lock path checks that all the local locks have been released,
205 * checking each local lock once. This means that the local lock paths
206 * cannot start their critical sections while the global lock is held.
207 */
208static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
209 int nsops)
210{
211 int locknum;
212 again:
213 if (nsops == 1 && !sma->complex_count) {
214 struct sem *sem = sma->sem_base + sops->sem_num;
215
216 /* Lock just the semaphore we are interested in. */
217 spin_lock(&sem->lock);
218
219 /*
220 * If sma->complex_count was set while we were spinning,
221 * we may need to look at things we did not lock here.
222 */
223 if (unlikely(sma->complex_count)) {
224 spin_unlock(&sem->lock);
225 goto lock_array;
226 }
227
228 /*
229 * Another process is holding the global lock on the
230 * sem_array; we cannot enter our critical section,
231 * but have to wait for the global lock to be released.
232 */
233 if (unlikely(spin_is_locked(&sma->sem_perm.lock))) {
234 spin_unlock(&sem->lock);
235 spin_unlock_wait(&sma->sem_perm.lock);
236 goto again;
237 }
238
239 locknum = sops->sem_num;
240 } else {
241 int i;
242 /*
243 * Lock the semaphore array, and wait for all of the
244 * individual semaphore locks to go away. The code
245 * above ensures no new single-lock holders will enter
246 * their critical section while the array lock is held.
247 */
248 lock_array:
249 spin_lock(&sma->sem_perm.lock);
250 for (i = 0; i < sma->sem_nsems; i++) {
251 struct sem *sem = sma->sem_base + i;
252 spin_unlock_wait(&sem->lock);
253 }
254 locknum = -1;
255 }
256 return locknum;
257}
258
259static inline void sem_unlock(struct sem_array *sma, int locknum)
260{
261 if (locknum == -1) {
262 spin_unlock(&sma->sem_perm.lock);
263 } else {
264 struct sem *sem = sma->sem_base + locknum;
265 spin_unlock(&sem->lock);
266 }
6062a8dc
RR
267}
268
3e148c79
ND
269/*
270 * sem_lock_(check_) routines are called in the paths where the rw_mutex
271 * is not held.
272 */
6062a8dc
RR
273static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
274 int id, struct sembuf *sops, int nsops, int *locknum)
023a5355 275{
c460b662
RR
276 struct kern_ipc_perm *ipcp;
277 struct sem_array *sma;
03f02c76 278
c460b662
RR
279 rcu_read_lock();
280 ipcp = ipc_obtain_object(&sem_ids(ns), id);
281 if (IS_ERR(ipcp)) {
282 sma = ERR_CAST(ipcp);
283 goto err;
284 }
b1ed88b4 285
6062a8dc
RR
286 sma = container_of(ipcp, struct sem_array, sem_perm);
287 *locknum = sem_lock(sma, sops, nsops);
c460b662
RR
288
289 /* ipc_rmid() may have already freed the ID while sem_lock
290 * was spinning: verify that the structure is still valid
291 */
292 if (!ipcp->deleted)
293 return container_of(ipcp, struct sem_array, sem_perm);
294
6062a8dc 295 sem_unlock(sma, *locknum);
c460b662
RR
296 sma = ERR_PTR(-EINVAL);
297err:
298 rcu_read_unlock();
299 return sma;
023a5355
ND
300}
301
16df3674
DB
302static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
303{
304 struct kern_ipc_perm *ipcp = ipc_obtain_object(&sem_ids(ns), id);
305
306 if (IS_ERR(ipcp))
307 return ERR_CAST(ipcp);
308
309 return container_of(ipcp, struct sem_array, sem_perm);
310}
311
16df3674
DB
312static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
313 int id)
314{
315 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
316
317 if (IS_ERR(ipcp))
318 return ERR_CAST(ipcp);
b1ed88b4 319
03f02c76 320 return container_of(ipcp, struct sem_array, sem_perm);
023a5355
ND
321}
322
6ff37972
PP
323static inline void sem_lock_and_putref(struct sem_array *sma)
324{
6062a8dc
RR
325 rcu_read_lock();
326 sem_lock(sma, NULL, -1);
6ff37972
PP
327 ipc_rcu_putref(sma);
328}
329
6ff37972
PP
330static inline void sem_putref(struct sem_array *sma)
331{
6062a8dc
RR
332 sem_lock_and_putref(sma);
333 sem_unlock(sma, -1);
6d49dab8 334 rcu_read_unlock();
6ff37972
PP
335}
336
7ca7e564
ND
337static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
338{
339 ipc_rmid(&sem_ids(ns), &s->sem_perm);
340}
341
1da177e4
LT
342/*
343 * Lockless wakeup algorithm:
344 * Without the check/retry algorithm a lockless wakeup is possible:
345 * - queue.status is initialized to -EINTR before blocking.
346 * - wakeup is performed by
347 * * unlinking the queue entry from sma->sem_pending
348 * * setting queue.status to IN_WAKEUP
349 * This is the notification for the blocked thread that a
350 * result value is imminent.
351 * * call wake_up_process
352 * * set queue.status to the final value.
353 * - the previously blocked thread checks queue.status:
354 * * if it's IN_WAKEUP, then it must wait until the value changes
355 * * if it's not -EINTR, then the operation was completed by
356 * update_queue. semtimedop can return queue.status without
5f921ae9 357 * performing any operation on the sem array.
1da177e4
LT
358 * * otherwise it must acquire the spinlock and check what's up.
359 *
360 * The two-stage algorithm is necessary to protect against the following
361 * races:
362 * - if queue.status is set after wake_up_process, then the woken up idle
363 * thread could race forward and try (and fail) to acquire sma->lock
364 * before update_queue had a chance to set queue.status
365 * - if queue.status is written before wake_up_process and if the
366 * blocked process is woken up by a signal between writing
367 * queue.status and the wake_up_process, then the woken up
368 * process could return from semtimedop and die by calling
369 * sys_exit before wake_up_process is called. Then wake_up_process
370 * will oops, because the task structure is already invalid.
371 * (yes, this happened on s390 with sysv msg).
372 *
373 */
374#define IN_WAKEUP 1
375
f4566f04
ND
376/**
377 * newary - Create a new semaphore set
378 * @ns: namespace
379 * @params: ptr to the structure that contains key, semflg and nsems
380 *
3e148c79 381 * Called with sem_ids.rw_mutex held (as a writer)
f4566f04
ND
382 */
383
7748dbfa 384static int newary(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4
LT
385{
386 int id;
387 int retval;
388 struct sem_array *sma;
389 int size;
7748dbfa
ND
390 key_t key = params->key;
391 int nsems = params->u.nsems;
392 int semflg = params->flg;
b97e820f 393 int i;
1da177e4
LT
394
395 if (!nsems)
396 return -EINVAL;
e3893534 397 if (ns->used_sems + nsems > ns->sc_semmns)
1da177e4
LT
398 return -ENOSPC;
399
400 size = sizeof (*sma) + nsems * sizeof (struct sem);
401 sma = ipc_rcu_alloc(size);
402 if (!sma) {
403 return -ENOMEM;
404 }
405 memset (sma, 0, size);
406
407 sma->sem_perm.mode = (semflg & S_IRWXUGO);
408 sma->sem_perm.key = key;
409
410 sma->sem_perm.security = NULL;
411 retval = security_sem_alloc(sma);
412 if (retval) {
413 ipc_rcu_putref(sma);
414 return retval;
415 }
416
e3893534 417 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
283bb7fa 418 if (id < 0) {
1da177e4
LT
419 security_sem_free(sma);
420 ipc_rcu_putref(sma);
283bb7fa 421 return id;
1da177e4 422 }
e3893534 423 ns->used_sems += nsems;
1da177e4
LT
424
425 sma->sem_base = (struct sem *) &sma[1];
b97e820f 426
6062a8dc 427 for (i = 0; i < nsems; i++) {
b97e820f 428 INIT_LIST_HEAD(&sma->sem_base[i].sem_pending);
6062a8dc
RR
429 spin_lock_init(&sma->sem_base[i].lock);
430 }
b97e820f
MS
431
432 sma->complex_count = 0;
a1193f8e 433 INIT_LIST_HEAD(&sma->sem_pending);
4daa28f6 434 INIT_LIST_HEAD(&sma->list_id);
1da177e4
LT
435 sma->sem_nsems = nsems;
436 sma->sem_ctime = get_seconds();
6062a8dc 437 sem_unlock(sma, -1);
6d49dab8 438 rcu_read_unlock();
1da177e4 439
7ca7e564 440 return sma->sem_perm.id;
1da177e4
LT
441}
442
7748dbfa 443
f4566f04 444/*
3e148c79 445 * Called with sem_ids.rw_mutex and ipcp locked.
f4566f04 446 */
03f02c76 447static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
7748dbfa 448{
03f02c76
ND
449 struct sem_array *sma;
450
451 sma = container_of(ipcp, struct sem_array, sem_perm);
452 return security_sem_associate(sma, semflg);
7748dbfa
ND
453}
454
f4566f04 455/*
3e148c79 456 * Called with sem_ids.rw_mutex and ipcp locked.
f4566f04 457 */
03f02c76
ND
458static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
459 struct ipc_params *params)
7748dbfa 460{
03f02c76
ND
461 struct sem_array *sma;
462
463 sma = container_of(ipcp, struct sem_array, sem_perm);
464 if (params->u.nsems > sma->sem_nsems)
7748dbfa
ND
465 return -EINVAL;
466
467 return 0;
468}
469
d5460c99 470SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
1da177e4 471{
e3893534 472 struct ipc_namespace *ns;
7748dbfa
ND
473 struct ipc_ops sem_ops;
474 struct ipc_params sem_params;
e3893534
KK
475
476 ns = current->nsproxy->ipc_ns;
1da177e4 477
e3893534 478 if (nsems < 0 || nsems > ns->sc_semmsl)
1da177e4 479 return -EINVAL;
7ca7e564 480
7748dbfa
ND
481 sem_ops.getnew = newary;
482 sem_ops.associate = sem_security;
483 sem_ops.more_checks = sem_more_checks;
484
485 sem_params.key = key;
486 sem_params.flg = semflg;
487 sem_params.u.nsems = nsems;
1da177e4 488
7748dbfa 489 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
1da177e4
LT
490}
491
1da177e4
LT
492/*
493 * Determine whether a sequence of semaphore operations would succeed
494 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
495 */
496
497static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
498 int nsops, struct sem_undo *un, int pid)
499{
500 int result, sem_op;
501 struct sembuf *sop;
502 struct sem * curr;
503
504 for (sop = sops; sop < sops + nsops; sop++) {
505 curr = sma->sem_base + sop->sem_num;
506 sem_op = sop->sem_op;
507 result = curr->semval;
508
509 if (!sem_op && result)
510 goto would_block;
511
512 result += sem_op;
513 if (result < 0)
514 goto would_block;
515 if (result > SEMVMX)
516 goto out_of_range;
517 if (sop->sem_flg & SEM_UNDO) {
518 int undo = un->semadj[sop->sem_num] - sem_op;
519 /*
520 * Exceeding the undo range is an error.
521 */
522 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
523 goto out_of_range;
524 }
525 curr->semval = result;
526 }
527
528 sop--;
529 while (sop >= sops) {
530 sma->sem_base[sop->sem_num].sempid = pid;
531 if (sop->sem_flg & SEM_UNDO)
532 un->semadj[sop->sem_num] -= sop->sem_op;
533 sop--;
534 }
535
1da177e4
LT
536 return 0;
537
538out_of_range:
539 result = -ERANGE;
540 goto undo;
541
542would_block:
543 if (sop->sem_flg & IPC_NOWAIT)
544 result = -EAGAIN;
545 else
546 result = 1;
547
548undo:
549 sop--;
550 while (sop >= sops) {
551 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
552 sop--;
553 }
554
555 return result;
556}
557
0a2b9d4c
MS
558/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
559 * @q: queue entry that must be signaled
560 * @error: Error value for the signal
561 *
562 * Prepare the wake-up of the queue entry q.
d4212093 563 */
0a2b9d4c
MS
564static void wake_up_sem_queue_prepare(struct list_head *pt,
565 struct sem_queue *q, int error)
d4212093 566{
0a2b9d4c
MS
567 if (list_empty(pt)) {
568 /*
569 * Hold preempt off so that we don't get preempted and have the
570 * wakee busy-wait until we're scheduled back on.
571 */
572 preempt_disable();
573 }
d4212093 574 q->status = IN_WAKEUP;
0a2b9d4c
MS
575 q->pid = error;
576
9f1bc2c9 577 list_add_tail(&q->list, pt);
0a2b9d4c
MS
578}
579
580/**
581 * wake_up_sem_queue_do(pt) - do the actual wake-up
582 * @pt: list of tasks to be woken up
583 *
584 * Do the actual wake-up.
585 * The function is called without any locks held, thus the semaphore array
586 * could be destroyed already and the tasks can disappear as soon as the
587 * status is set to the actual return code.
588 */
589static void wake_up_sem_queue_do(struct list_head *pt)
590{
591 struct sem_queue *q, *t;
592 int did_something;
593
594 did_something = !list_empty(pt);
9f1bc2c9 595 list_for_each_entry_safe(q, t, pt, list) {
0a2b9d4c
MS
596 wake_up_process(q->sleeper);
597 /* q can disappear immediately after writing q->status. */
598 smp_wmb();
599 q->status = q->pid;
600 }
601 if (did_something)
602 preempt_enable();
d4212093
NP
603}
604
b97e820f
MS
605static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
606{
607 list_del(&q->list);
9f1bc2c9 608 if (q->nsops > 1)
b97e820f
MS
609 sma->complex_count--;
610}
611
fd5db422
MS
612/** check_restart(sma, q)
613 * @sma: semaphore array
614 * @q: the operation that just completed
615 *
616 * update_queue is O(N^2) when it restarts scanning the whole queue of
617 * waiting operations. Therefore this function checks if the restart is
618 * really necessary. It is called after a previously waiting operation
619 * was completed.
620 */
621static int check_restart(struct sem_array *sma, struct sem_queue *q)
622{
623 struct sem *curr;
624 struct sem_queue *h;
625
626 /* if the operation didn't modify the array, then no restart */
627 if (q->alter == 0)
628 return 0;
629
630 /* pending complex operations are too difficult to analyse */
631 if (sma->complex_count)
632 return 1;
633
634 /* we were a sleeping complex operation. Too difficult */
635 if (q->nsops > 1)
636 return 1;
637
638 curr = sma->sem_base + q->sops[0].sem_num;
639
640 /* No-one waits on this queue */
641 if (list_empty(&curr->sem_pending))
642 return 0;
643
644 /* the new semaphore value */
645 if (curr->semval) {
646 /* It is impossible that someone waits for the new value:
647 * - q is a previously sleeping simple operation that
648 * altered the array. It must be a decrement, because
649 * simple increments never sleep.
650 * - The value is not 0, thus wait-for-zero won't proceed.
651 * - If there are older (higher priority) decrements
652 * in the queue, then they have observed the original
653 * semval value and couldn't proceed. The operation
654 * decremented to value - thus they won't proceed either.
655 */
656 BUG_ON(q->sops[0].sem_op >= 0);
657 return 0;
658 }
659 /*
660 * semval is 0. Check if there are wait-for-zero semops.
9f1bc2c9 661 * They must be the first entries in the per-semaphore queue
fd5db422 662 */
9f1bc2c9 663 h = list_first_entry(&curr->sem_pending, struct sem_queue, list);
fd5db422
MS
664 BUG_ON(h->nsops != 1);
665 BUG_ON(h->sops[0].sem_num != q->sops[0].sem_num);
666
667 /* Yes, there is a wait-for-zero semop. Restart */
668 if (h->sops[0].sem_op == 0)
669 return 1;
670
671 /* Again - no-one is waiting for the new value. */
672 return 0;
673}
674
636c6be8
MS
675
676/**
677 * update_queue(sma, semnum): Look for tasks that can be completed.
678 * @sma: semaphore array.
679 * @semnum: semaphore that was modified.
0a2b9d4c 680 * @pt: list head for the tasks that must be woken up.
636c6be8
MS
681 *
682 * update_queue must be called after a semaphore in a semaphore array
9f1bc2c9
RR
683 * was modified. If multiple semaphores were modified, update_queue must
684 * be called with semnum = -1, as well as with the number of each modified
685 * semaphore.
0a2b9d4c
MS
686 * The tasks that must be woken up are added to @pt. The return code
687 * is stored in q->pid.
688 * The function return 1 if at least one semop was completed successfully.
1da177e4 689 */
0a2b9d4c 690static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
1da177e4 691{
636c6be8
MS
692 struct sem_queue *q;
693 struct list_head *walk;
694 struct list_head *pending_list;
0a2b9d4c 695 int semop_completed = 0;
636c6be8 696
9f1bc2c9 697 if (semnum == -1)
636c6be8 698 pending_list = &sma->sem_pending;
9f1bc2c9 699 else
636c6be8 700 pending_list = &sma->sem_base[semnum].sem_pending;
9cad200c
NP
701
702again:
636c6be8
MS
703 walk = pending_list->next;
704 while (walk != pending_list) {
fd5db422 705 int error, restart;
636c6be8 706
9f1bc2c9 707 q = container_of(walk, struct sem_queue, list);
636c6be8 708 walk = walk->next;
1da177e4 709
d987f8b2
MS
710 /* If we are scanning the single sop, per-semaphore list of
711 * one semaphore and that semaphore is 0, then it is not
712 * necessary to scan the "alter" entries: simple increments
713 * that affect only one entry succeed immediately and cannot
714 * be in the per semaphore pending queue, and decrements
715 * cannot be successful if the value is already 0.
716 */
717 if (semnum != -1 && sma->sem_base[semnum].semval == 0 &&
718 q->alter)
719 break;
720
1da177e4
LT
721 error = try_atomic_semop(sma, q->sops, q->nsops,
722 q->undo, q->pid);
723
724 /* Does q->sleeper still need to sleep? */
9cad200c
NP
725 if (error > 0)
726 continue;
727
b97e820f 728 unlink_queue(sma, q);
9cad200c 729
0a2b9d4c 730 if (error) {
fd5db422 731 restart = 0;
0a2b9d4c
MS
732 } else {
733 semop_completed = 1;
fd5db422 734 restart = check_restart(sma, q);
0a2b9d4c 735 }
fd5db422 736
0a2b9d4c 737 wake_up_sem_queue_prepare(pt, q, error);
fd5db422 738 if (restart)
9cad200c 739 goto again;
1da177e4 740 }
0a2b9d4c 741 return semop_completed;
1da177e4
LT
742}
743
0a2b9d4c
MS
744/**
745 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
fd5db422
MS
746 * @sma: semaphore array
747 * @sops: operations that were performed
748 * @nsops: number of operations
0a2b9d4c
MS
749 * @otime: force setting otime
750 * @pt: list head of the tasks that must be woken up.
fd5db422
MS
751 *
752 * do_smart_update() does the required called to update_queue, based on the
753 * actual changes that were performed on the semaphore array.
0a2b9d4c
MS
754 * Note that the function does not do the actual wake-up: the caller is
755 * responsible for calling wake_up_sem_queue_do(@pt).
756 * It is safe to perform this call after dropping all locks.
fd5db422 757 */
0a2b9d4c
MS
758static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
759 int otime, struct list_head *pt)
fd5db422
MS
760{
761 int i;
762
763 if (sma->complex_count || sops == NULL) {
0a2b9d4c
MS
764 if (update_queue(sma, -1, pt))
765 otime = 1;
9f1bc2c9
RR
766 }
767
768 if (!sops) {
769 /* No semops; something special is going on. */
770 for (i = 0; i < sma->sem_nsems; i++) {
771 if (update_queue(sma, i, pt))
772 otime = 1;
773 }
0a2b9d4c 774 goto done;
fd5db422
MS
775 }
776
9f1bc2c9 777 /* Check the semaphores that were modified. */
fd5db422
MS
778 for (i = 0; i < nsops; i++) {
779 if (sops[i].sem_op > 0 ||
780 (sops[i].sem_op < 0 &&
781 sma->sem_base[sops[i].sem_num].semval == 0))
0a2b9d4c
MS
782 if (update_queue(sma, sops[i].sem_num, pt))
783 otime = 1;
fd5db422 784 }
0a2b9d4c
MS
785done:
786 if (otime)
787 sma->sem_otime = get_seconds();
fd5db422
MS
788}
789
790
1da177e4
LT
791/* The following counts are associated to each semaphore:
792 * semncnt number of tasks waiting on semval being nonzero
793 * semzcnt number of tasks waiting on semval being zero
794 * This model assumes that a task waits on exactly one semaphore.
795 * Since semaphore operations are to be performed atomically, tasks actually
796 * wait on a whole sequence of semaphores simultaneously.
797 * The counts we return here are a rough approximation, but still
798 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
799 */
800static int count_semncnt (struct sem_array * sma, ushort semnum)
801{
802 int semncnt;
803 struct sem_queue * q;
804
805 semncnt = 0;
a1193f8e 806 list_for_each_entry(q, &sma->sem_pending, list) {
1da177e4
LT
807 struct sembuf * sops = q->sops;
808 int nsops = q->nsops;
809 int i;
810 for (i = 0; i < nsops; i++)
811 if (sops[i].sem_num == semnum
812 && (sops[i].sem_op < 0)
813 && !(sops[i].sem_flg & IPC_NOWAIT))
814 semncnt++;
815 }
816 return semncnt;
817}
a1193f8e 818
1da177e4
LT
819static int count_semzcnt (struct sem_array * sma, ushort semnum)
820{
821 int semzcnt;
822 struct sem_queue * q;
823
824 semzcnt = 0;
a1193f8e 825 list_for_each_entry(q, &sma->sem_pending, list) {
1da177e4
LT
826 struct sembuf * sops = q->sops;
827 int nsops = q->nsops;
828 int i;
829 for (i = 0; i < nsops; i++)
830 if (sops[i].sem_num == semnum
831 && (sops[i].sem_op == 0)
832 && !(sops[i].sem_flg & IPC_NOWAIT))
833 semzcnt++;
834 }
835 return semzcnt;
836}
837
3e148c79
ND
838/* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
839 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
840 * remains locked on exit.
1da177e4 841 */
01b8b07a 842static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1da177e4 843{
380af1b3
MS
844 struct sem_undo *un, *tu;
845 struct sem_queue *q, *tq;
01b8b07a 846 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
0a2b9d4c 847 struct list_head tasks;
9f1bc2c9 848 int i;
1da177e4 849
380af1b3 850 /* Free the existing undo structures for this semaphore set. */
4daa28f6 851 assert_spin_locked(&sma->sem_perm.lock);
380af1b3
MS
852 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
853 list_del(&un->list_id);
854 spin_lock(&un->ulp->lock);
1da177e4 855 un->semid = -1;
380af1b3
MS
856 list_del_rcu(&un->list_proc);
857 spin_unlock(&un->ulp->lock);
693a8b6e 858 kfree_rcu(un, rcu);
380af1b3 859 }
1da177e4
LT
860
861 /* Wake up all pending processes and let them fail with EIDRM. */
0a2b9d4c 862 INIT_LIST_HEAD(&tasks);
380af1b3 863 list_for_each_entry_safe(q, tq, &sma->sem_pending, list) {
b97e820f 864 unlink_queue(sma, q);
0a2b9d4c 865 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1da177e4 866 }
9f1bc2c9
RR
867 for (i = 0; i < sma->sem_nsems; i++) {
868 struct sem *sem = sma->sem_base + i;
869 list_for_each_entry_safe(q, tq, &sem->sem_pending, list) {
870 unlink_queue(sma, q);
871 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
872 }
873 }
1da177e4 874
7ca7e564
ND
875 /* Remove the semaphore set from the IDR */
876 sem_rmid(ns, sma);
6062a8dc 877 sem_unlock(sma, -1);
6d49dab8 878 rcu_read_unlock();
1da177e4 879
0a2b9d4c 880 wake_up_sem_queue_do(&tasks);
e3893534 881 ns->used_sems -= sma->sem_nsems;
1da177e4
LT
882 security_sem_free(sma);
883 ipc_rcu_putref(sma);
884}
885
886static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
887{
888 switch(version) {
889 case IPC_64:
890 return copy_to_user(buf, in, sizeof(*in));
891 case IPC_OLD:
892 {
893 struct semid_ds out;
894
982f7c2b
DR
895 memset(&out, 0, sizeof(out));
896
1da177e4
LT
897 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
898
899 out.sem_otime = in->sem_otime;
900 out.sem_ctime = in->sem_ctime;
901 out.sem_nsems = in->sem_nsems;
902
903 return copy_to_user(buf, &out, sizeof(out));
904 }
905 default:
906 return -EINVAL;
907 }
908}
909
4b9fcb0e 910static int semctl_nolock(struct ipc_namespace *ns, int semid,
e1fd1f49 911 int cmd, int version, void __user *p)
1da177e4 912{
e5cc9c7b 913 int err;
1da177e4
LT
914 struct sem_array *sma;
915
916 switch(cmd) {
917 case IPC_INFO:
918 case SEM_INFO:
919 {
920 struct seminfo seminfo;
921 int max_id;
922
923 err = security_sem_semctl(NULL, cmd);
924 if (err)
925 return err;
926
927 memset(&seminfo,0,sizeof(seminfo));
e3893534
KK
928 seminfo.semmni = ns->sc_semmni;
929 seminfo.semmns = ns->sc_semmns;
930 seminfo.semmsl = ns->sc_semmsl;
931 seminfo.semopm = ns->sc_semopm;
1da177e4
LT
932 seminfo.semvmx = SEMVMX;
933 seminfo.semmnu = SEMMNU;
934 seminfo.semmap = SEMMAP;
935 seminfo.semume = SEMUME;
3e148c79 936 down_read(&sem_ids(ns).rw_mutex);
1da177e4 937 if (cmd == SEM_INFO) {
e3893534
KK
938 seminfo.semusz = sem_ids(ns).in_use;
939 seminfo.semaem = ns->used_sems;
1da177e4
LT
940 } else {
941 seminfo.semusz = SEMUSZ;
942 seminfo.semaem = SEMAEM;
943 }
7ca7e564 944 max_id = ipc_get_maxid(&sem_ids(ns));
3e148c79 945 up_read(&sem_ids(ns).rw_mutex);
e1fd1f49 946 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1da177e4
LT
947 return -EFAULT;
948 return (max_id < 0) ? 0: max_id;
949 }
4b9fcb0e 950 case IPC_STAT:
1da177e4
LT
951 case SEM_STAT:
952 {
953 struct semid64_ds tbuf;
16df3674
DB
954 int id = 0;
955
956 memset(&tbuf, 0, sizeof(tbuf));
1da177e4 957
4b9fcb0e 958 if (cmd == SEM_STAT) {
16df3674
DB
959 rcu_read_lock();
960 sma = sem_obtain_object(ns, semid);
961 if (IS_ERR(sma)) {
962 err = PTR_ERR(sma);
963 goto out_unlock;
964 }
4b9fcb0e
PP
965 id = sma->sem_perm.id;
966 } else {
16df3674
DB
967 rcu_read_lock();
968 sma = sem_obtain_object_check(ns, semid);
969 if (IS_ERR(sma)) {
970 err = PTR_ERR(sma);
971 goto out_unlock;
972 }
4b9fcb0e 973 }
1da177e4
LT
974
975 err = -EACCES;
b0e77598 976 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1da177e4
LT
977 goto out_unlock;
978
979 err = security_sem_semctl(sma, cmd);
980 if (err)
981 goto out_unlock;
982
1da177e4
LT
983 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
984 tbuf.sem_otime = sma->sem_otime;
985 tbuf.sem_ctime = sma->sem_ctime;
986 tbuf.sem_nsems = sma->sem_nsems;
16df3674 987 rcu_read_unlock();
e1fd1f49 988 if (copy_semid_to_user(p, &tbuf, version))
1da177e4
LT
989 return -EFAULT;
990 return id;
991 }
992 default:
993 return -EINVAL;
994 }
1da177e4 995out_unlock:
16df3674 996 rcu_read_unlock();
1da177e4
LT
997 return err;
998}
999
e1fd1f49
AV
1000static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1001 unsigned long arg)
1002{
1003 struct sem_undo *un;
1004 struct sem_array *sma;
1005 struct sem* curr;
1006 int err;
e1fd1f49
AV
1007 struct list_head tasks;
1008 int val;
1009#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1010 /* big-endian 64bit */
1011 val = arg >> 32;
1012#else
1013 /* 32bit or little-endian 64bit */
1014 val = arg;
1015#endif
1016
6062a8dc
RR
1017 if (val > SEMVMX || val < 0)
1018 return -ERANGE;
e1fd1f49
AV
1019
1020 INIT_LIST_HEAD(&tasks);
e1fd1f49 1021
6062a8dc
RR
1022 rcu_read_lock();
1023 sma = sem_obtain_object_check(ns, semid);
1024 if (IS_ERR(sma)) {
1025 rcu_read_unlock();
1026 return PTR_ERR(sma);
1027 }
1028
1029 if (semnum < 0 || semnum >= sma->sem_nsems) {
1030 rcu_read_unlock();
1031 return -EINVAL;
1032 }
1033
1034
1035 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1036 rcu_read_unlock();
1037 return -EACCES;
1038 }
e1fd1f49
AV
1039
1040 err = security_sem_semctl(sma, SETVAL);
6062a8dc
RR
1041 if (err) {
1042 rcu_read_unlock();
1043 return -EACCES;
1044 }
e1fd1f49 1045
6062a8dc 1046 sem_lock(sma, NULL, -1);
e1fd1f49
AV
1047
1048 curr = &sma->sem_base[semnum];
1049
e1fd1f49
AV
1050 assert_spin_locked(&sma->sem_perm.lock);
1051 list_for_each_entry(un, &sma->list_id, list_id)
1052 un->semadj[semnum] = 0;
1053
1054 curr->semval = val;
1055 curr->sempid = task_tgid_vnr(current);
1056 sma->sem_ctime = get_seconds();
1057 /* maybe some queued-up processes were waiting for this */
1058 do_smart_update(sma, NULL, 0, 0, &tasks);
6062a8dc 1059 sem_unlock(sma, -1);
6d49dab8 1060 rcu_read_unlock();
e1fd1f49 1061 wake_up_sem_queue_do(&tasks);
6062a8dc 1062 return 0;
e1fd1f49
AV
1063}
1064
e3893534 1065static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
e1fd1f49 1066 int cmd, void __user *p)
1da177e4
LT
1067{
1068 struct sem_array *sma;
1069 struct sem* curr;
16df3674 1070 int err, nsems;
1da177e4
LT
1071 ushort fast_sem_io[SEMMSL_FAST];
1072 ushort* sem_io = fast_sem_io;
0a2b9d4c 1073 struct list_head tasks;
1da177e4 1074
16df3674
DB
1075 INIT_LIST_HEAD(&tasks);
1076
1077 rcu_read_lock();
1078 sma = sem_obtain_object_check(ns, semid);
1079 if (IS_ERR(sma)) {
1080 rcu_read_unlock();
023a5355 1081 return PTR_ERR(sma);
16df3674 1082 }
1da177e4
LT
1083
1084 nsems = sma->sem_nsems;
1085
1da177e4 1086 err = -EACCES;
b0e77598 1087 if (ipcperms(ns, &sma->sem_perm,
16df3674
DB
1088 cmd == SETALL ? S_IWUGO : S_IRUGO)) {
1089 rcu_read_unlock();
1090 goto out_wakeup;
1091 }
1da177e4
LT
1092
1093 err = security_sem_semctl(sma, cmd);
16df3674
DB
1094 if (err) {
1095 rcu_read_unlock();
1096 goto out_wakeup;
1097 }
1da177e4
LT
1098
1099 err = -EACCES;
1100 switch (cmd) {
1101 case GETALL:
1102 {
e1fd1f49 1103 ushort __user *array = p;
1da177e4
LT
1104 int i;
1105
ce857229 1106 sem_lock(sma, NULL, -1);
1da177e4 1107 if(nsems > SEMMSL_FAST) {
ce857229
AV
1108 if (!ipc_rcu_getref(sma)) {
1109 sem_unlock(sma, -1);
6d49dab8 1110 rcu_read_unlock();
ce857229
AV
1111 err = -EIDRM;
1112 goto out_free;
1113 }
1114 sem_unlock(sma, -1);
6d49dab8 1115 rcu_read_unlock();
1da177e4
LT
1116 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1117 if(sem_io == NULL) {
6ff37972 1118 sem_putref(sma);
1da177e4
LT
1119 return -ENOMEM;
1120 }
1121
6ff37972 1122 sem_lock_and_putref(sma);
1da177e4 1123 if (sma->sem_perm.deleted) {
6062a8dc 1124 sem_unlock(sma, -1);
6d49dab8 1125 rcu_read_unlock();
1da177e4
LT
1126 err = -EIDRM;
1127 goto out_free;
1128 }
ce857229 1129 }
1da177e4
LT
1130 for (i = 0; i < sma->sem_nsems; i++)
1131 sem_io[i] = sma->sem_base[i].semval;
6062a8dc 1132 sem_unlock(sma, -1);
6d49dab8 1133 rcu_read_unlock();
1da177e4
LT
1134 err = 0;
1135 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1136 err = -EFAULT;
1137 goto out_free;
1138 }
1139 case SETALL:
1140 {
1141 int i;
1142 struct sem_undo *un;
1143
6062a8dc
RR
1144 if (!ipc_rcu_getref(sma)) {
1145 rcu_read_unlock();
1146 return -EIDRM;
1147 }
16df3674 1148 rcu_read_unlock();
1da177e4
LT
1149
1150 if(nsems > SEMMSL_FAST) {
1151 sem_io = ipc_alloc(sizeof(ushort)*nsems);
1152 if(sem_io == NULL) {
6ff37972 1153 sem_putref(sma);
1da177e4
LT
1154 return -ENOMEM;
1155 }
1156 }
1157
e1fd1f49 1158 if (copy_from_user (sem_io, p, nsems*sizeof(ushort))) {
6ff37972 1159 sem_putref(sma);
1da177e4
LT
1160 err = -EFAULT;
1161 goto out_free;
1162 }
1163
1164 for (i = 0; i < nsems; i++) {
1165 if (sem_io[i] > SEMVMX) {
6ff37972 1166 sem_putref(sma);
1da177e4
LT
1167 err = -ERANGE;
1168 goto out_free;
1169 }
1170 }
6ff37972 1171 sem_lock_and_putref(sma);
1da177e4 1172 if (sma->sem_perm.deleted) {
6062a8dc 1173 sem_unlock(sma, -1);
6d49dab8 1174 rcu_read_unlock();
1da177e4
LT
1175 err = -EIDRM;
1176 goto out_free;
1177 }
1178
1179 for (i = 0; i < nsems; i++)
1180 sma->sem_base[i].semval = sem_io[i];
4daa28f6
MS
1181
1182 assert_spin_locked(&sma->sem_perm.lock);
1183 list_for_each_entry(un, &sma->list_id, list_id) {
1da177e4
LT
1184 for (i = 0; i < nsems; i++)
1185 un->semadj[i] = 0;
4daa28f6 1186 }
1da177e4
LT
1187 sma->sem_ctime = get_seconds();
1188 /* maybe some queued-up processes were waiting for this */
0a2b9d4c 1189 do_smart_update(sma, NULL, 0, 0, &tasks);
1da177e4
LT
1190 err = 0;
1191 goto out_unlock;
1192 }
e1fd1f49 1193 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1da177e4
LT
1194 }
1195 err = -EINVAL;
16df3674
DB
1196 if (semnum < 0 || semnum >= nsems) {
1197 rcu_read_unlock();
1198 goto out_wakeup;
1199 }
1da177e4 1200
6062a8dc 1201 sem_lock(sma, NULL, -1);
1da177e4
LT
1202 curr = &sma->sem_base[semnum];
1203
1204 switch (cmd) {
1205 case GETVAL:
1206 err = curr->semval;
1207 goto out_unlock;
1208 case GETPID:
1209 err = curr->sempid;
1210 goto out_unlock;
1211 case GETNCNT:
1212 err = count_semncnt(sma,semnum);
1213 goto out_unlock;
1214 case GETZCNT:
1215 err = count_semzcnt(sma,semnum);
1216 goto out_unlock;
1da177e4 1217 }
16df3674 1218
1da177e4 1219out_unlock:
6062a8dc 1220 sem_unlock(sma, -1);
6d49dab8 1221 rcu_read_unlock();
16df3674 1222out_wakeup:
0a2b9d4c 1223 wake_up_sem_queue_do(&tasks);
1da177e4
LT
1224out_free:
1225 if(sem_io != fast_sem_io)
1226 ipc_free(sem_io, sizeof(ushort)*nsems);
1227 return err;
1228}
1229
016d7132
PP
1230static inline unsigned long
1231copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
1da177e4
LT
1232{
1233 switch(version) {
1234 case IPC_64:
016d7132 1235 if (copy_from_user(out, buf, sizeof(*out)))
1da177e4 1236 return -EFAULT;
1da177e4 1237 return 0;
1da177e4
LT
1238 case IPC_OLD:
1239 {
1240 struct semid_ds tbuf_old;
1241
1242 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1243 return -EFAULT;
1244
016d7132
PP
1245 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1246 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1247 out->sem_perm.mode = tbuf_old.sem_perm.mode;
1da177e4
LT
1248
1249 return 0;
1250 }
1251 default:
1252 return -EINVAL;
1253 }
1254}
1255
522bb2a2
PP
1256/*
1257 * This function handles some semctl commands which require the rw_mutex
1258 * to be held in write mode.
1259 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1260 */
21a4826a 1261static int semctl_down(struct ipc_namespace *ns, int semid,
e1fd1f49 1262 int cmd, int version, void __user *p)
1da177e4
LT
1263{
1264 struct sem_array *sma;
1265 int err;
016d7132 1266 struct semid64_ds semid64;
1da177e4
LT
1267 struct kern_ipc_perm *ipcp;
1268
1269 if(cmd == IPC_SET) {
e1fd1f49 1270 if (copy_semid_from_user(&semid64, p, version))
1da177e4 1271 return -EFAULT;
1da177e4 1272 }
073115d6 1273
16df3674
DB
1274 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1275 &semid64.sem_perm, 0);
a5f75e7f
PP
1276 if (IS_ERR(ipcp))
1277 return PTR_ERR(ipcp);
073115d6 1278
a5f75e7f 1279 sma = container_of(ipcp, struct sem_array, sem_perm);
1da177e4
LT
1280
1281 err = security_sem_semctl(sma, cmd);
16df3674
DB
1282 if (err) {
1283 rcu_read_unlock();
1da177e4 1284 goto out_unlock;
16df3674 1285 }
1da177e4
LT
1286
1287 switch(cmd){
1288 case IPC_RMID:
6062a8dc 1289 sem_lock(sma, NULL, -1);
01b8b07a 1290 freeary(ns, ipcp);
522bb2a2 1291 goto out_up;
1da177e4 1292 case IPC_SET:
6062a8dc 1293 sem_lock(sma, NULL, -1);
1efdb69b
EB
1294 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1295 if (err)
1296 goto out_unlock;
1da177e4 1297 sma->sem_ctime = get_seconds();
1da177e4
LT
1298 break;
1299 default:
16df3674 1300 rcu_read_unlock();
1da177e4 1301 err = -EINVAL;
16df3674 1302 goto out_up;
1da177e4 1303 }
1da177e4
LT
1304
1305out_unlock:
6062a8dc 1306 sem_unlock(sma, -1);
6d49dab8 1307 rcu_read_unlock();
522bb2a2
PP
1308out_up:
1309 up_write(&sem_ids(ns).rw_mutex);
1da177e4
LT
1310 return err;
1311}
1312
e1fd1f49 1313SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1da177e4 1314{
1da177e4 1315 int version;
e3893534 1316 struct ipc_namespace *ns;
e1fd1f49 1317 void __user *p = (void __user *)arg;
1da177e4
LT
1318
1319 if (semid < 0)
1320 return -EINVAL;
1321
1322 version = ipc_parse_version(&cmd);
e3893534 1323 ns = current->nsproxy->ipc_ns;
1da177e4
LT
1324
1325 switch(cmd) {
1326 case IPC_INFO:
1327 case SEM_INFO:
4b9fcb0e 1328 case IPC_STAT:
1da177e4 1329 case SEM_STAT:
e1fd1f49 1330 return semctl_nolock(ns, semid, cmd, version, p);
1da177e4
LT
1331 case GETALL:
1332 case GETVAL:
1333 case GETPID:
1334 case GETNCNT:
1335 case GETZCNT:
1da177e4 1336 case SETALL:
e1fd1f49
AV
1337 return semctl_main(ns, semid, semnum, cmd, p);
1338 case SETVAL:
1339 return semctl_setval(ns, semid, semnum, arg);
1da177e4
LT
1340 case IPC_RMID:
1341 case IPC_SET:
e1fd1f49 1342 return semctl_down(ns, semid, cmd, version, p);
1da177e4
LT
1343 default:
1344 return -EINVAL;
1345 }
1346}
1347
1da177e4
LT
1348/* If the task doesn't already have a undo_list, then allocate one
1349 * here. We guarantee there is only one thread using this undo list,
1350 * and current is THE ONE
1351 *
1352 * If this allocation and assignment succeeds, but later
1353 * portions of this code fail, there is no need to free the sem_undo_list.
1354 * Just let it stay associated with the task, and it'll be freed later
1355 * at exit time.
1356 *
1357 * This can block, so callers must hold no locks.
1358 */
1359static inline int get_undo_list(struct sem_undo_list **undo_listp)
1360{
1361 struct sem_undo_list *undo_list;
1da177e4
LT
1362
1363 undo_list = current->sysvsem.undo_list;
1364 if (!undo_list) {
2453a306 1365 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1da177e4
LT
1366 if (undo_list == NULL)
1367 return -ENOMEM;
00a5dfdb 1368 spin_lock_init(&undo_list->lock);
1da177e4 1369 atomic_set(&undo_list->refcnt, 1);
4daa28f6
MS
1370 INIT_LIST_HEAD(&undo_list->list_proc);
1371
1da177e4
LT
1372 current->sysvsem.undo_list = undo_list;
1373 }
1374 *undo_listp = undo_list;
1375 return 0;
1376}
1377
bf17bb71 1378static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
1da177e4 1379{
bf17bb71 1380 struct sem_undo *un;
4daa28f6 1381
bf17bb71
NP
1382 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1383 if (un->semid == semid)
1384 return un;
1da177e4 1385 }
4daa28f6 1386 return NULL;
1da177e4
LT
1387}
1388
bf17bb71
NP
1389static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1390{
1391 struct sem_undo *un;
1392
1393 assert_spin_locked(&ulp->lock);
1394
1395 un = __lookup_undo(ulp, semid);
1396 if (un) {
1397 list_del_rcu(&un->list_proc);
1398 list_add_rcu(&un->list_proc, &ulp->list_proc);
1399 }
1400 return un;
1401}
1402
4daa28f6
MS
1403/**
1404 * find_alloc_undo - Lookup (and if not present create) undo array
1405 * @ns: namespace
1406 * @semid: semaphore array id
1407 *
1408 * The function looks up (and if not present creates) the undo structure.
1409 * The size of the undo structure depends on the size of the semaphore
1410 * array, thus the alloc path is not that straightforward.
380af1b3
MS
1411 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1412 * performs a rcu_read_lock().
4daa28f6
MS
1413 */
1414static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
1da177e4
LT
1415{
1416 struct sem_array *sma;
1417 struct sem_undo_list *ulp;
1418 struct sem_undo *un, *new;
6062a8dc 1419 int nsems, error;
1da177e4
LT
1420
1421 error = get_undo_list(&ulp);
1422 if (error)
1423 return ERR_PTR(error);
1424
380af1b3 1425 rcu_read_lock();
c530c6ac 1426 spin_lock(&ulp->lock);
1da177e4 1427 un = lookup_undo(ulp, semid);
c530c6ac 1428 spin_unlock(&ulp->lock);
1da177e4
LT
1429 if (likely(un!=NULL))
1430 goto out;
1431
1432 /* no undo structure around - allocate one. */
4daa28f6 1433 /* step 1: figure out the size of the semaphore array */
16df3674
DB
1434 sma = sem_obtain_object_check(ns, semid);
1435 if (IS_ERR(sma)) {
1436 rcu_read_unlock();
4de85cd6 1437 return ERR_CAST(sma);
16df3674 1438 }
023a5355 1439
1da177e4 1440 nsems = sma->sem_nsems;
6062a8dc
RR
1441 if (!ipc_rcu_getref(sma)) {
1442 rcu_read_unlock();
1443 un = ERR_PTR(-EIDRM);
1444 goto out;
1445 }
16df3674 1446 rcu_read_unlock();
1da177e4 1447
4daa28f6 1448 /* step 2: allocate new undo structure */
4668edc3 1449 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1da177e4 1450 if (!new) {
6ff37972 1451 sem_putref(sma);
1da177e4
LT
1452 return ERR_PTR(-ENOMEM);
1453 }
1da177e4 1454
380af1b3 1455 /* step 3: Acquire the lock on semaphore array */
6d49dab8 1456 /* This also does the rcu_read_lock() */
6ff37972 1457 sem_lock_and_putref(sma);
1da177e4 1458 if (sma->sem_perm.deleted) {
6062a8dc 1459 sem_unlock(sma, -1);
6d49dab8 1460 rcu_read_unlock();
1da177e4
LT
1461 kfree(new);
1462 un = ERR_PTR(-EIDRM);
1463 goto out;
1464 }
380af1b3
MS
1465 spin_lock(&ulp->lock);
1466
1467 /*
1468 * step 4: check for races: did someone else allocate the undo struct?
1469 */
1470 un = lookup_undo(ulp, semid);
1471 if (un) {
1472 kfree(new);
1473 goto success;
1474 }
4daa28f6
MS
1475 /* step 5: initialize & link new undo structure */
1476 new->semadj = (short *) &new[1];
380af1b3 1477 new->ulp = ulp;
4daa28f6
MS
1478 new->semid = semid;
1479 assert_spin_locked(&ulp->lock);
380af1b3 1480 list_add_rcu(&new->list_proc, &ulp->list_proc);
4daa28f6
MS
1481 assert_spin_locked(&sma->sem_perm.lock);
1482 list_add(&new->list_id, &sma->list_id);
380af1b3 1483 un = new;
4daa28f6 1484
380af1b3 1485success:
c530c6ac 1486 spin_unlock(&ulp->lock);
6062a8dc 1487 sem_unlock(sma, -1);
1da177e4
LT
1488out:
1489 return un;
1490}
1491
c61284e9
MS
1492
1493/**
1494 * get_queue_result - Retrieve the result code from sem_queue
1495 * @q: Pointer to queue structure
1496 *
1497 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1498 * q->status, then we must loop until the value is replaced with the final
1499 * value: This may happen if a task is woken up by an unrelated event (e.g.
1500 * signal) and in parallel the task is woken up by another task because it got
1501 * the requested semaphores.
1502 *
1503 * The function can be called with or without holding the semaphore spinlock.
1504 */
1505static int get_queue_result(struct sem_queue *q)
1506{
1507 int error;
1508
1509 error = q->status;
1510 while (unlikely(error == IN_WAKEUP)) {
1511 cpu_relax();
1512 error = q->status;
1513 }
1514
1515 return error;
1516}
1517
1518
d5460c99
HC
1519SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1520 unsigned, nsops, const struct timespec __user *, timeout)
1da177e4
LT
1521{
1522 int error = -EINVAL;
1523 struct sem_array *sma;
1524 struct sembuf fast_sops[SEMOPM_FAST];
1525 struct sembuf* sops = fast_sops, *sop;
1526 struct sem_undo *un;
6062a8dc 1527 int undos = 0, alter = 0, max, locknum;
1da177e4
LT
1528 struct sem_queue queue;
1529 unsigned long jiffies_left = 0;
e3893534 1530 struct ipc_namespace *ns;
0a2b9d4c 1531 struct list_head tasks;
e3893534
KK
1532
1533 ns = current->nsproxy->ipc_ns;
1da177e4
LT
1534
1535 if (nsops < 1 || semid < 0)
1536 return -EINVAL;
e3893534 1537 if (nsops > ns->sc_semopm)
1da177e4
LT
1538 return -E2BIG;
1539 if(nsops > SEMOPM_FAST) {
1540 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
1541 if(sops==NULL)
1542 return -ENOMEM;
1543 }
1544 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
1545 error=-EFAULT;
1546 goto out_free;
1547 }
1548 if (timeout) {
1549 struct timespec _timeout;
1550 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1551 error = -EFAULT;
1552 goto out_free;
1553 }
1554 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1555 _timeout.tv_nsec >= 1000000000L) {
1556 error = -EINVAL;
1557 goto out_free;
1558 }
1559 jiffies_left = timespec_to_jiffies(&_timeout);
1560 }
1561 max = 0;
1562 for (sop = sops; sop < sops + nsops; sop++) {
1563 if (sop->sem_num >= max)
1564 max = sop->sem_num;
1565 if (sop->sem_flg & SEM_UNDO)
b78755ab
MS
1566 undos = 1;
1567 if (sop->sem_op != 0)
1da177e4
LT
1568 alter = 1;
1569 }
1da177e4 1570
6062a8dc
RR
1571 INIT_LIST_HEAD(&tasks);
1572
1da177e4 1573 if (undos) {
6062a8dc 1574 /* On success, find_alloc_undo takes the rcu_read_lock */
4daa28f6 1575 un = find_alloc_undo(ns, semid);
1da177e4
LT
1576 if (IS_ERR(un)) {
1577 error = PTR_ERR(un);
1578 goto out_free;
1579 }
6062a8dc 1580 } else {
1da177e4 1581 un = NULL;
6062a8dc
RR
1582 rcu_read_lock();
1583 }
1da177e4 1584
16df3674 1585 sma = sem_obtain_object_check(ns, semid);
023a5355 1586 if (IS_ERR(sma)) {
6062a8dc 1587 rcu_read_unlock();
023a5355 1588 error = PTR_ERR(sma);
1da177e4 1589 goto out_free;
023a5355
ND
1590 }
1591
16df3674
DB
1592 error = -EFBIG;
1593 if (max >= sma->sem_nsems) {
1594 rcu_read_unlock();
1595 goto out_wakeup;
1596 }
1597
1598 error = -EACCES;
1599 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1600 rcu_read_unlock();
1601 goto out_wakeup;
1602 }
1603
1604 error = security_sem_semop(sma, sops, nsops, alter);
1605 if (error) {
1606 rcu_read_unlock();
1607 goto out_wakeup;
1608 }
1609
1da177e4 1610 /*
4daa28f6 1611 * semid identifiers are not unique - find_alloc_undo may have
1da177e4 1612 * allocated an undo structure, it was invalidated by an RMID
4daa28f6 1613 * and now a new array with received the same id. Check and fail.
25985edc 1614 * This case can be detected checking un->semid. The existence of
380af1b3 1615 * "un" itself is guaranteed by rcu.
1da177e4 1616 */
4daa28f6 1617 error = -EIDRM;
6062a8dc
RR
1618 locknum = sem_lock(sma, sops, nsops);
1619 if (un && un->semid == -1)
1620 goto out_unlock_free;
4daa28f6 1621
b488893a 1622 error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current));
1da177e4
LT
1623 if (error <= 0) {
1624 if (alter && error == 0)
0a2b9d4c 1625 do_smart_update(sma, sops, nsops, 1, &tasks);
636c6be8 1626
1da177e4
LT
1627 goto out_unlock_free;
1628 }
1629
1630 /* We need to sleep on this operation, so we put the current
1631 * task into the pending queue and go to sleep.
1632 */
1633
1da177e4
LT
1634 queue.sops = sops;
1635 queue.nsops = nsops;
1636 queue.undo = un;
b488893a 1637 queue.pid = task_tgid_vnr(current);
1da177e4 1638 queue.alter = alter;
1da177e4 1639
b97e820f
MS
1640 if (nsops == 1) {
1641 struct sem *curr;
1642 curr = &sma->sem_base[sops->sem_num];
1643
1644 if (alter)
9f1bc2c9 1645 list_add_tail(&queue.list, &curr->sem_pending);
b97e820f 1646 else
9f1bc2c9 1647 list_add(&queue.list, &curr->sem_pending);
b97e820f 1648 } else {
9f1bc2c9
RR
1649 if (alter)
1650 list_add_tail(&queue.list, &sma->sem_pending);
1651 else
1652 list_add(&queue.list, &sma->sem_pending);
b97e820f
MS
1653 sma->complex_count++;
1654 }
1655
1da177e4
LT
1656 queue.status = -EINTR;
1657 queue.sleeper = current;
0b0577f6
MS
1658
1659sleep_again:
1da177e4 1660 current->state = TASK_INTERRUPTIBLE;
6062a8dc 1661 sem_unlock(sma, locknum);
6d49dab8 1662 rcu_read_unlock();
1da177e4
LT
1663
1664 if (timeout)
1665 jiffies_left = schedule_timeout(jiffies_left);
1666 else
1667 schedule();
1668
c61284e9 1669 error = get_queue_result(&queue);
1da177e4
LT
1670
1671 if (error != -EINTR) {
1672 /* fast path: update_queue already obtained all requested
c61284e9
MS
1673 * resources.
1674 * Perform a smp_mb(): User space could assume that semop()
1675 * is a memory barrier: Without the mb(), the cpu could
1676 * speculatively read in user space stale data that was
1677 * overwritten by the previous owner of the semaphore.
1678 */
1679 smp_mb();
1680
1da177e4
LT
1681 goto out_free;
1682 }
1683
6062a8dc 1684 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
d694ad62
MS
1685
1686 /*
1687 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1688 */
1689 error = get_queue_result(&queue);
1690
1691 /*
1692 * Array removed? If yes, leave without sem_unlock().
1693 */
023a5355 1694 if (IS_ERR(sma)) {
1da177e4
LT
1695 goto out_free;
1696 }
1697
c61284e9 1698
1da177e4 1699 /*
d694ad62
MS
1700 * If queue.status != -EINTR we are woken up by another process.
1701 * Leave without unlink_queue(), but with sem_unlock().
1da177e4 1702 */
c61284e9 1703
1da177e4
LT
1704 if (error != -EINTR) {
1705 goto out_unlock_free;
1706 }
1707
1708 /*
1709 * If an interrupt occurred we have to clean up the queue
1710 */
1711 if (timeout && jiffies_left == 0)
1712 error = -EAGAIN;
0b0577f6
MS
1713
1714 /*
1715 * If the wakeup was spurious, just retry
1716 */
1717 if (error == -EINTR && !signal_pending(current))
1718 goto sleep_again;
1719
b97e820f 1720 unlink_queue(sma, &queue);
1da177e4
LT
1721
1722out_unlock_free:
6062a8dc 1723 sem_unlock(sma, locknum);
6d49dab8 1724 rcu_read_unlock();
16df3674 1725out_wakeup:
0a2b9d4c 1726 wake_up_sem_queue_do(&tasks);
1da177e4
LT
1727out_free:
1728 if(sops != fast_sops)
1729 kfree(sops);
1730 return error;
1731}
1732
d5460c99
HC
1733SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
1734 unsigned, nsops)
1da177e4
LT
1735{
1736 return sys_semtimedop(semid, tsops, nsops, NULL);
1737}
1738
1739/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1740 * parent and child tasks.
1da177e4
LT
1741 */
1742
1743int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1744{
1745 struct sem_undo_list *undo_list;
1746 int error;
1747
1748 if (clone_flags & CLONE_SYSVSEM) {
1749 error = get_undo_list(&undo_list);
1750 if (error)
1751 return error;
1da177e4
LT
1752 atomic_inc(&undo_list->refcnt);
1753 tsk->sysvsem.undo_list = undo_list;
1754 } else
1755 tsk->sysvsem.undo_list = NULL;
1756
1757 return 0;
1758}
1759
1760/*
1761 * add semadj values to semaphores, free undo structures.
1762 * undo structures are not freed when semaphore arrays are destroyed
1763 * so some of them may be out of date.
1764 * IMPLEMENTATION NOTE: There is some confusion over whether the
1765 * set of adjustments that needs to be done should be done in an atomic
1766 * manner or not. That is, if we are attempting to decrement the semval
1767 * should we queue up and wait until we can do so legally?
1768 * The original implementation attempted to do this (queue and wait).
1769 * The current implementation does not do so. The POSIX standard
1770 * and SVID should be consulted to determine what behavior is mandated.
1771 */
1772void exit_sem(struct task_struct *tsk)
1773{
4daa28f6 1774 struct sem_undo_list *ulp;
1da177e4 1775
4daa28f6
MS
1776 ulp = tsk->sysvsem.undo_list;
1777 if (!ulp)
1da177e4 1778 return;
9edff4ab 1779 tsk->sysvsem.undo_list = NULL;
1da177e4 1780
4daa28f6 1781 if (!atomic_dec_and_test(&ulp->refcnt))
1da177e4
LT
1782 return;
1783
380af1b3 1784 for (;;) {
1da177e4 1785 struct sem_array *sma;
380af1b3 1786 struct sem_undo *un;
0a2b9d4c 1787 struct list_head tasks;
6062a8dc 1788 int semid, i;
4daa28f6 1789
380af1b3 1790 rcu_read_lock();
05725f7e
JP
1791 un = list_entry_rcu(ulp->list_proc.next,
1792 struct sem_undo, list_proc);
380af1b3
MS
1793 if (&un->list_proc == &ulp->list_proc)
1794 semid = -1;
1795 else
1796 semid = un->semid;
4daa28f6 1797
6062a8dc
RR
1798 if (semid == -1) {
1799 rcu_read_unlock();
380af1b3 1800 break;
6062a8dc 1801 }
1da177e4 1802
6062a8dc 1803 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, un->semid);
380af1b3 1804 /* exit_sem raced with IPC_RMID, nothing to do */
6062a8dc
RR
1805 if (IS_ERR(sma)) {
1806 rcu_read_unlock();
380af1b3 1807 continue;
6062a8dc 1808 }
1da177e4 1809
6062a8dc 1810 sem_lock(sma, NULL, -1);
bf17bb71 1811 un = __lookup_undo(ulp, semid);
380af1b3
MS
1812 if (un == NULL) {
1813 /* exit_sem raced with IPC_RMID+semget() that created
1814 * exactly the same semid. Nothing to do.
1815 */
6062a8dc 1816 sem_unlock(sma, -1);
6d49dab8 1817 rcu_read_unlock();
380af1b3
MS
1818 continue;
1819 }
1820
1821 /* remove un from the linked lists */
4daa28f6
MS
1822 assert_spin_locked(&sma->sem_perm.lock);
1823 list_del(&un->list_id);
1824
380af1b3
MS
1825 spin_lock(&ulp->lock);
1826 list_del_rcu(&un->list_proc);
1827 spin_unlock(&ulp->lock);
1828
4daa28f6
MS
1829 /* perform adjustments registered in un */
1830 for (i = 0; i < sma->sem_nsems; i++) {
5f921ae9 1831 struct sem * semaphore = &sma->sem_base[i];
4daa28f6
MS
1832 if (un->semadj[i]) {
1833 semaphore->semval += un->semadj[i];
1da177e4
LT
1834 /*
1835 * Range checks of the new semaphore value,
1836 * not defined by sus:
1837 * - Some unices ignore the undo entirely
1838 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1839 * - some cap the value (e.g. FreeBSD caps
1840 * at 0, but doesn't enforce SEMVMX)
1841 *
1842 * Linux caps the semaphore value, both at 0
1843 * and at SEMVMX.
1844 *
1845 * Manfred <manfred@colorfullife.com>
1846 */
5f921ae9
IM
1847 if (semaphore->semval < 0)
1848 semaphore->semval = 0;
1849 if (semaphore->semval > SEMVMX)
1850 semaphore->semval = SEMVMX;
b488893a 1851 semaphore->sempid = task_tgid_vnr(current);
1da177e4
LT
1852 }
1853 }
1da177e4 1854 /* maybe some queued-up processes were waiting for this */
0a2b9d4c
MS
1855 INIT_LIST_HEAD(&tasks);
1856 do_smart_update(sma, NULL, 0, 1, &tasks);
6062a8dc 1857 sem_unlock(sma, -1);
6d49dab8 1858 rcu_read_unlock();
0a2b9d4c 1859 wake_up_sem_queue_do(&tasks);
380af1b3 1860
693a8b6e 1861 kfree_rcu(un, rcu);
1da177e4 1862 }
4daa28f6 1863 kfree(ulp);
1da177e4
LT
1864}
1865
1866#ifdef CONFIG_PROC_FS
19b4946c 1867static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1da177e4 1868{
1efdb69b 1869 struct user_namespace *user_ns = seq_user_ns(s);
19b4946c
MW
1870 struct sem_array *sma = it;
1871
1872 return seq_printf(s,
b97e820f 1873 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
19b4946c 1874 sma->sem_perm.key,
7ca7e564 1875 sma->sem_perm.id,
19b4946c
MW
1876 sma->sem_perm.mode,
1877 sma->sem_nsems,
1efdb69b
EB
1878 from_kuid_munged(user_ns, sma->sem_perm.uid),
1879 from_kgid_munged(user_ns, sma->sem_perm.gid),
1880 from_kuid_munged(user_ns, sma->sem_perm.cuid),
1881 from_kgid_munged(user_ns, sma->sem_perm.cgid),
19b4946c
MW
1882 sma->sem_otime,
1883 sma->sem_ctime);
1da177e4
LT
1884}
1885#endif
This page took 1.374348 seconds and 5 git commands to generate.