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