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