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