]> git.ipfire.org Git - people/arne_f/kernel.git/blame - ipc/sem.c
cls_matchall: use tcf_exts_get_net() before call_rcu()
[people/arne_f/kernel.git] / ipc / sem.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * linux/ipc/sem.c
4 * Copyright (C) 1992 Krishna Balasubramanian
5 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 *
1da177e4
LT
7 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
8 *
9 * SMP-threaded, sysctl's added
624dffcb 10 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
1da177e4 11 * Enforced range limit on SEM_UNDO
046c6884 12 * (c) 2001 Red Hat Inc
1da177e4
LT
13 * Lockless wakeup
14 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
9ae949fa 15 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
c5cf6359
MS
16 * Further wakeup optimizations, documentation
17 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
073115d6
SG
18 *
19 * support for audit of ipc object properties and permission changes
20 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
e3893534
KK
21 *
22 * namespaces support
23 * OpenVZ, SWsoft Inc.
24 * Pavel Emelianov <xemul@openvz.org>
c5cf6359
MS
25 *
26 * Implementation notes: (May 2010)
27 * This file implements System V semaphores.
28 *
29 * User space visible behavior:
30 * - FIFO ordering for semop() operations (just FIFO, not starvation
31 * protection)
32 * - multiple semaphore operations that alter the same semaphore in
33 * one semop() are handled.
34 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
35 * SETALL calls.
36 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
37 * - undo adjustments at process exit are limited to 0..SEMVMX.
38 * - namespace are supported.
39 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
40 * to /proc/sys/kernel/sem.
41 * - statistics about the usage are reported in /proc/sysvipc/sem.
42 *
43 * Internals:
44 * - scalability:
45 * - all global variables are read-mostly.
46 * - semop() calls and semctl(RMID) are synchronized by RCU.
47 * - most operations do write operations (actually: spin_lock calls) to
48 * the per-semaphore array structure.
49 * Thus: Perfect SMP scaling between independent semaphore arrays.
50 * If multiple semaphores in one array are used, then cache line
51 * trashing on the semaphore array spinlock will limit the scaling.
2f2ed41d 52 * - semncnt and semzcnt are calculated on demand in count_semcnt()
c5cf6359
MS
53 * - the task that performs a successful semop() scans the list of all
54 * sleeping tasks and completes any pending operations that can be fulfilled.
55 * Semaphores are actively given to waiting tasks (necessary for FIFO).
56 * (see update_queue())
57 * - To improve the scalability, the actual wake-up calls are performed after
9ae949fa 58 * dropping all locks. (see wake_up_sem_queue_prepare())
c5cf6359
MS
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).
c5cf6359
MS
63 * - UNDO values are stored in an array (one per process and per
64 * semaphore array, lazily allocated). For backwards compatibility, multiple
65 * modes for the UNDO variables are supported (per process, per thread)
66 * (see copy_semundo, CLONE_SYSVSEM)
67 * - There are two lists of the pending operations: a per-array list
68 * and per-semaphore list (stored in the array). This allows to achieve FIFO
69 * ordering without always scanning all pending operations.
70 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
1da177e4
LT
71 */
72
1da177e4
LT
73#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/init.h>
76#include <linux/proc_fs.h>
77#include <linux/time.h>
1da177e4
LT
78#include <linux/security.h>
79#include <linux/syscalls.h>
80#include <linux/audit.h>
c59ede7b 81#include <linux/capability.h>
19b4946c 82#include <linux/seq_file.h>
3e148c79 83#include <linux/rwsem.h>
e3893534 84#include <linux/nsproxy.h>
ae5e1b22 85#include <linux/ipc_namespace.h>
84f001e1 86#include <linux/sched/wake_q.h>
5f921ae9 87
7153e402 88#include <linux/uaccess.h>
1da177e4
LT
89#include "util.h"
90
e57940d7
MS
91
92/* One queue for each sleeping process in the system. */
93struct sem_queue {
e57940d7
MS
94 struct list_head list; /* queue of pending operations */
95 struct task_struct *sleeper; /* this process */
96 struct sem_undo *undo; /* undo structure */
97 int pid; /* process id of requesting process */
98 int status; /* completion status of operation */
99 struct sembuf *sops; /* array of pending operations */
ed247b7c 100 struct sembuf *blocking; /* the operation that blocked */
e57940d7 101 int nsops; /* number of operations */
4ce33ec2
DB
102 bool alter; /* does *sops alter the array? */
103 bool dupsop; /* sops on more than one sem_num */
e57940d7
MS
104};
105
106/* Each task has a list of undo requests. They are executed automatically
107 * when the process exits.
108 */
109struct sem_undo {
110 struct list_head list_proc; /* per-process list: *
111 * all undos from one process
112 * rcu protected */
113 struct rcu_head rcu; /* rcu struct for sem_undo */
114 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
115 struct list_head list_id; /* per semaphore array list:
116 * all undos for one array */
117 int semid; /* semaphore set identifier */
118 short *semadj; /* array of adjustments */
119 /* one per semaphore */
120};
121
122/* sem_undo_list controls shared access to the list of sem_undo structures
123 * that may be shared among all a CLONE_SYSVSEM task group.
124 */
125struct sem_undo_list {
f74370b8 126 refcount_t refcnt;
e57940d7
MS
127 spinlock_t lock;
128 struct list_head list_proc;
129};
130
131
ed2ddbf8 132#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
e3893534 133
7748dbfa 134static int newary(struct ipc_namespace *, struct ipc_params *);
01b8b07a 135static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
1da177e4 136#ifdef CONFIG_PROC_FS
19b4946c 137static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
1da177e4
LT
138#endif
139
140#define SEMMSL_FAST 256 /* 512 bytes on stack */
141#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
142
9de5ab8a
MS
143/*
144 * Switching from the mode suitable for simple ops
145 * to the mode for complex ops is costly. Therefore:
146 * use some hysteresis
147 */
148#define USE_GLOBAL_LOCK_HYSTERESIS 10
149
1da177e4 150/*
758a6ba3 151 * Locking:
5864a2fd 152 * a) global sem_lock() for read/write
1da177e4 153 * sem_undo.id_next,
758a6ba3 154 * sem_array.complex_count,
5864a2fd
MS
155 * sem_array.pending{_alter,_const},
156 * sem_array.sem_undo
46c0a8ca 157 *
5864a2fd 158 * b) global or semaphore sem_lock() for read/write:
1a233956 159 * sem_array.sems[i].pending_{const,alter}:
5864a2fd
MS
160 *
161 * c) special:
162 * sem_undo_list.list_proc:
163 * * undo_list->lock for write
164 * * rcu for read
9de5ab8a
MS
165 * use_global_lock:
166 * * global sem_lock() for write
167 * * either local or global sem_lock() for read.
168 *
169 * Memory ordering:
170 * Most ordering is enforced by using spin_lock() and spin_unlock().
171 * The special case is use_global_lock:
172 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
173 * using smp_store_release().
174 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
175 * smp_load_acquire().
176 * Setting it from 0 to non-zero must be ordered with regards to
177 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
178 * is inside a spin_lock() and after a write from 0 to non-zero a
179 * spin_lock()+spin_unlock() is done.
1da177e4
LT
180 */
181
e3893534
KK
182#define sc_semmsl sem_ctls[0]
183#define sc_semmns sem_ctls[1]
184#define sc_semopm sem_ctls[2]
185#define sc_semmni sem_ctls[3]
186
0cfb6aee 187int sem_init_ns(struct ipc_namespace *ns)
e3893534 188{
e3893534
KK
189 ns->sc_semmsl = SEMMSL;
190 ns->sc_semmns = SEMMNS;
191 ns->sc_semopm = SEMOPM;
192 ns->sc_semmni = SEMMNI;
193 ns->used_sems = 0;
0cfb6aee 194 return ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
e3893534
KK
195}
196
ae5e1b22 197#ifdef CONFIG_IPC_NS
e3893534
KK
198void sem_exit_ns(struct ipc_namespace *ns)
199{
01b8b07a 200 free_ipcs(ns, &sem_ids(ns), freeary);
7d6feeb2 201 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
0cfb6aee 202 rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
e3893534 203}
ae5e1b22 204#endif
1da177e4 205
0cfb6aee 206int __init sem_init(void)
1da177e4 207{
0cfb6aee
GK
208 const int err = sem_init_ns(&init_ipc_ns);
209
19b4946c
MW
210 ipc_init_proc_interface("sysvipc/sem",
211 " key semid perms nsems uid gid cuid cgid otime ctime\n",
e3893534 212 IPC_SEM_IDS, sysvipc_sem_proc_show);
0cfb6aee 213 return err;
1da177e4
LT
214}
215
f269f40a
MS
216/**
217 * unmerge_queues - unmerge queues, if possible.
218 * @sma: semaphore array
219 *
220 * The function unmerges the wait queues if complex_count is 0.
221 * It must be called prior to dropping the global semaphore array lock.
222 */
223static void unmerge_queues(struct sem_array *sma)
224{
225 struct sem_queue *q, *tq;
226
227 /* complex operations still around? */
228 if (sma->complex_count)
229 return;
230 /*
231 * We will switch back to simple mode.
232 * Move all pending operation back into the per-semaphore
233 * queues.
234 */
235 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
236 struct sem *curr;
1a233956 237 curr = &sma->sems[q->sops[0].sem_num];
f269f40a
MS
238
239 list_add_tail(&q->list, &curr->pending_alter);
240 }
241 INIT_LIST_HEAD(&sma->pending_alter);
242}
243
244/**
8001c858 245 * merge_queues - merge single semop queues into global queue
f269f40a
MS
246 * @sma: semaphore array
247 *
248 * This function merges all per-semaphore queues into the global queue.
249 * It is necessary to achieve FIFO ordering for the pending single-sop
250 * operations when a multi-semop operation must sleep.
251 * Only the alter operations must be moved, the const operations can stay.
252 */
253static void merge_queues(struct sem_array *sma)
254{
255 int i;
256 for (i = 0; i < sma->sem_nsems; i++) {
1a233956 257 struct sem *sem = &sma->sems[i];
f269f40a
MS
258
259 list_splice_init(&sem->pending_alter, &sma->pending_alter);
260 }
261}
262
53dad6d3
DB
263static void sem_rcu_free(struct rcu_head *head)
264{
dba4cdd3
MS
265 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
266 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
53dad6d3
DB
267
268 security_sem_free(sma);
e2029dfe 269 kvfree(sma);
53dad6d3
DB
270}
271
5e9d5275 272/*
5864a2fd 273 * Enter the mode suitable for non-simple operations:
5e9d5275 274 * Caller must own sem_perm.lock.
5e9d5275 275 */
5864a2fd 276static void complexmode_enter(struct sem_array *sma)
5e9d5275
MS
277{
278 int i;
279 struct sem *sem;
280
9de5ab8a
MS
281 if (sma->use_global_lock > 0) {
282 /*
283 * We are already in global lock mode.
284 * Nothing to do, just reset the
285 * counter until we return to simple mode.
286 */
287 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
6d07b68c
MS
288 return;
289 }
9de5ab8a 290 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
5864a2fd 291
5e9d5275 292 for (i = 0; i < sma->sem_nsems; i++) {
1a233956 293 sem = &sma->sems[i];
27d7be18
MS
294 spin_lock(&sem->lock);
295 spin_unlock(&sem->lock);
5e9d5275 296 }
5864a2fd
MS
297}
298
299/*
300 * Try to leave the mode that disallows simple operations:
301 * Caller must own sem_perm.lock.
302 */
303static void complexmode_tryleave(struct sem_array *sma)
304{
305 if (sma->complex_count) {
306 /* Complex ops are sleeping.
307 * We must stay in complex mode
308 */
309 return;
310 }
9de5ab8a
MS
311 if (sma->use_global_lock == 1) {
312 /*
313 * Immediately after setting use_global_lock to 0,
314 * a simple op can start. Thus: all memory writes
315 * performed by the current operation must be visible
316 * before we set use_global_lock to 0.
317 */
318 smp_store_release(&sma->use_global_lock, 0);
319 } else {
320 sma->use_global_lock--;
321 }
5e9d5275
MS
322}
323
5864a2fd 324#define SEM_GLOBAL_LOCK (-1)
6062a8dc
RR
325/*
326 * If the request contains only one semaphore operation, and there are
327 * no complex transactions pending, lock only the semaphore involved.
328 * Otherwise, lock the entire semaphore array, since we either have
329 * multiple semaphores in our own semops, or we need to look at
330 * semaphores from other pending complex operations.
6062a8dc
RR
331 */
332static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
333 int nsops)
334{
5e9d5275 335 struct sem *sem;
6062a8dc 336
5e9d5275
MS
337 if (nsops != 1) {
338 /* Complex operation - acquire a full lock */
339 ipc_lock_object(&sma->sem_perm);
6062a8dc 340
5864a2fd
MS
341 /* Prevent parallel simple ops */
342 complexmode_enter(sma);
343 return SEM_GLOBAL_LOCK;
5e9d5275
MS
344 }
345
346 /*
347 * Only one semaphore affected - try to optimize locking.
5864a2fd
MS
348 * Optimized locking is possible if no complex operation
349 * is either enqueued or processed right now.
350 *
9de5ab8a 351 * Both facts are tracked by use_global_mode.
5e9d5275 352 */
1a233956 353 sem = &sma->sems[sops->sem_num];
6062a8dc 354
5864a2fd 355 /*
9de5ab8a 356 * Initial check for use_global_lock. Just an optimization,
5864a2fd
MS
357 * no locking, no memory barrier.
358 */
9de5ab8a 359 if (!sma->use_global_lock) {
6062a8dc 360 /*
5e9d5275
MS
361 * It appears that no complex operation is around.
362 * Acquire the per-semaphore lock.
6062a8dc 363 */
5e9d5275
MS
364 spin_lock(&sem->lock);
365
9de5ab8a
MS
366 /* pairs with smp_store_release() */
367 if (!smp_load_acquire(&sma->use_global_lock)) {
5864a2fd
MS
368 /* fast path successful! */
369 return sops->sem_num;
6062a8dc 370 }
5e9d5275
MS
371 spin_unlock(&sem->lock);
372 }
373
374 /* slow path: acquire the full lock */
375 ipc_lock_object(&sma->sem_perm);
6062a8dc 376
9de5ab8a
MS
377 if (sma->use_global_lock == 0) {
378 /*
379 * The use_global_lock mode ended while we waited for
380 * sma->sem_perm.lock. Thus we must switch to locking
381 * with sem->lock.
382 * Unlike in the fast path, there is no need to recheck
383 * sma->use_global_lock after we have acquired sem->lock:
384 * We own sma->sem_perm.lock, thus use_global_lock cannot
385 * change.
5e9d5275
MS
386 */
387 spin_lock(&sem->lock);
9de5ab8a 388
5e9d5275
MS
389 ipc_unlock_object(&sma->sem_perm);
390 return sops->sem_num;
6062a8dc 391 } else {
9de5ab8a
MS
392 /*
393 * Not a false alarm, thus continue to use the global lock
394 * mode. No need for complexmode_enter(), this was done by
395 * the caller that has set use_global_mode to non-zero.
6062a8dc 396 */
5864a2fd 397 return SEM_GLOBAL_LOCK;
6062a8dc 398 }
6062a8dc
RR
399}
400
401static inline void sem_unlock(struct sem_array *sma, int locknum)
402{
5864a2fd 403 if (locknum == SEM_GLOBAL_LOCK) {
f269f40a 404 unmerge_queues(sma);
5864a2fd 405 complexmode_tryleave(sma);
cf9d5d78 406 ipc_unlock_object(&sma->sem_perm);
6062a8dc 407 } else {
1a233956 408 struct sem *sem = &sma->sems[locknum];
6062a8dc
RR
409 spin_unlock(&sem->lock);
410 }
6062a8dc
RR
411}
412
3e148c79 413/*
d9a605e4 414 * sem_lock_(check_) routines are called in the paths where the rwsem
3e148c79 415 * is not held.
321310ce
LT
416 *
417 * The caller holds the RCU read lock.
3e148c79 418 */
16df3674
DB
419static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
420{
55b7ae50 421 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
16df3674
DB
422
423 if (IS_ERR(ipcp))
424 return ERR_CAST(ipcp);
425
426 return container_of(ipcp, struct sem_array, sem_perm);
427}
428
16df3674
DB
429static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
430 int id)
431{
432 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
433
434 if (IS_ERR(ipcp))
435 return ERR_CAST(ipcp);
b1ed88b4 436
03f02c76 437 return container_of(ipcp, struct sem_array, sem_perm);
023a5355
ND
438}
439
6ff37972
PP
440static inline void sem_lock_and_putref(struct sem_array *sma)
441{
6062a8dc 442 sem_lock(sma, NULL, -1);
dba4cdd3 443 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
6ff37972
PP
444}
445
7ca7e564
ND
446static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
447{
448 ipc_rmid(&sem_ids(ns), &s->sem_perm);
449}
450
101ede01
KC
451static struct sem_array *sem_alloc(size_t nsems)
452{
453 struct sem_array *sma;
454 size_t size;
455
456 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
457 return NULL;
458
459 size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
460 sma = kvmalloc(size, GFP_KERNEL);
461 if (unlikely(!sma))
462 return NULL;
463
464 memset(sma, 0, size);
101ede01
KC
465
466 return sma;
467}
468
f4566f04
ND
469/**
470 * newary - Create a new semaphore set
471 * @ns: namespace
472 * @params: ptr to the structure that contains key, semflg and nsems
473 *
d9a605e4 474 * Called with sem_ids.rwsem held (as a writer)
f4566f04 475 */
7748dbfa 476static int newary(struct ipc_namespace *ns, struct ipc_params *params)
1da177e4 477{
1da177e4
LT
478 int retval;
479 struct sem_array *sma;
7748dbfa
ND
480 key_t key = params->key;
481 int nsems = params->u.nsems;
482 int semflg = params->flg;
b97e820f 483 int i;
1da177e4
LT
484
485 if (!nsems)
486 return -EINVAL;
e3893534 487 if (ns->used_sems + nsems > ns->sc_semmns)
1da177e4
LT
488 return -ENOSPC;
489
101ede01 490 sma = sem_alloc(nsems);
3ab08fe2 491 if (!sma)
1da177e4 492 return -ENOMEM;
3ab08fe2 493
1da177e4
LT
494 sma->sem_perm.mode = (semflg & S_IRWXUGO);
495 sma->sem_perm.key = key;
496
497 sma->sem_perm.security = NULL;
498 retval = security_sem_alloc(sma);
499 if (retval) {
e2029dfe 500 kvfree(sma);
1da177e4
LT
501 return retval;
502 }
503
6062a8dc 504 for (i = 0; i < nsems; i++) {
1a233956
MS
505 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
506 INIT_LIST_HEAD(&sma->sems[i].pending_const);
507 spin_lock_init(&sma->sems[i].lock);
6062a8dc 508 }
b97e820f
MS
509
510 sma->complex_count = 0;
9de5ab8a 511 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
1a82e9e1
MS
512 INIT_LIST_HEAD(&sma->pending_alter);
513 INIT_LIST_HEAD(&sma->pending_const);
4daa28f6 514 INIT_LIST_HEAD(&sma->list_id);
1da177e4 515 sma->sem_nsems = nsems;
e54d02b2 516 sma->sem_ctime = ktime_get_real_seconds();
e8577d1f 517
2ec55f80
MS
518 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
519 if (retval < 0) {
520 call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
521 return retval;
e8577d1f
MS
522 }
523 ns->used_sems += nsems;
524
6062a8dc 525 sem_unlock(sma, -1);
6d49dab8 526 rcu_read_unlock();
1da177e4 527
7ca7e564 528 return sma->sem_perm.id;
1da177e4
LT
529}
530
7748dbfa 531
f4566f04 532/*
d9a605e4 533 * Called with sem_ids.rwsem and ipcp locked.
f4566f04 534 */
03f02c76 535static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
7748dbfa 536{
03f02c76
ND
537 struct sem_array *sma;
538
539 sma = container_of(ipcp, struct sem_array, sem_perm);
540 return security_sem_associate(sma, semflg);
7748dbfa
ND
541}
542
f4566f04 543/*
d9a605e4 544 * Called with sem_ids.rwsem and ipcp locked.
f4566f04 545 */
03f02c76
ND
546static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
547 struct ipc_params *params)
7748dbfa 548{
03f02c76
ND
549 struct sem_array *sma;
550
551 sma = container_of(ipcp, struct sem_array, sem_perm);
552 if (params->u.nsems > sma->sem_nsems)
7748dbfa
ND
553 return -EINVAL;
554
555 return 0;
556}
557
d5460c99 558SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
1da177e4 559{
e3893534 560 struct ipc_namespace *ns;
eb66ec44
MK
561 static const struct ipc_ops sem_ops = {
562 .getnew = newary,
563 .associate = sem_security,
564 .more_checks = sem_more_checks,
565 };
7748dbfa 566 struct ipc_params sem_params;
e3893534
KK
567
568 ns = current->nsproxy->ipc_ns;
1da177e4 569
e3893534 570 if (nsems < 0 || nsems > ns->sc_semmsl)
1da177e4 571 return -EINVAL;
7ca7e564 572
7748dbfa
ND
573 sem_params.key = key;
574 sem_params.flg = semflg;
575 sem_params.u.nsems = nsems;
1da177e4 576
7748dbfa 577 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
1da177e4
LT
578}
579
78f5009c 580/**
4ce33ec2
DB
581 * perform_atomic_semop[_slow] - Attempt to perform semaphore
582 * operations on a given array.
758a6ba3 583 * @sma: semaphore array
d198cd6d 584 * @q: struct sem_queue that describes the operation
758a6ba3 585 *
4ce33ec2
DB
586 * Caller blocking are as follows, based the value
587 * indicated by the semaphore operation (sem_op):
588 *
589 * (1) >0 never blocks.
590 * (2) 0 (wait-for-zero operation): semval is non-zero.
591 * (3) <0 attempting to decrement semval to a value smaller than zero.
592 *
758a6ba3
MS
593 * Returns 0 if the operation was possible.
594 * Returns 1 if the operation is impossible, the caller must sleep.
4ce33ec2 595 * Returns <0 for error codes.
1da177e4 596 */
4ce33ec2 597static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
1da177e4 598{
d198cd6d 599 int result, sem_op, nsops, pid;
1da177e4 600 struct sembuf *sop;
239521f3 601 struct sem *curr;
d198cd6d
MS
602 struct sembuf *sops;
603 struct sem_undo *un;
604
605 sops = q->sops;
606 nsops = q->nsops;
607 un = q->undo;
1da177e4
LT
608
609 for (sop = sops; sop < sops + nsops; sop++) {
1a233956 610 curr = &sma->sems[sop->sem_num];
1da177e4
LT
611 sem_op = sop->sem_op;
612 result = curr->semval;
78f5009c 613
1da177e4
LT
614 if (!sem_op && result)
615 goto would_block;
616
617 result += sem_op;
618 if (result < 0)
619 goto would_block;
620 if (result > SEMVMX)
621 goto out_of_range;
78f5009c 622
1da177e4
LT
623 if (sop->sem_flg & SEM_UNDO) {
624 int undo = un->semadj[sop->sem_num] - sem_op;
78f5009c 625 /* Exceeding the undo range is an error. */
1da177e4
LT
626 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
627 goto out_of_range;
78f5009c 628 un->semadj[sop->sem_num] = undo;
1da177e4 629 }
78f5009c 630
1da177e4
LT
631 curr->semval = result;
632 }
633
634 sop--;
d198cd6d 635 pid = q->pid;
1da177e4 636 while (sop >= sops) {
1a233956 637 sma->sems[sop->sem_num].sempid = pid;
1da177e4
LT
638 sop--;
639 }
78f5009c 640
1da177e4
LT
641 return 0;
642
643out_of_range:
644 result = -ERANGE;
645 goto undo;
646
647would_block:
ed247b7c
MS
648 q->blocking = sop;
649
1da177e4
LT
650 if (sop->sem_flg & IPC_NOWAIT)
651 result = -EAGAIN;
652 else
653 result = 1;
654
655undo:
656 sop--;
657 while (sop >= sops) {
78f5009c 658 sem_op = sop->sem_op;
1a233956 659 sma->sems[sop->sem_num].semval -= sem_op;
78f5009c
PM
660 if (sop->sem_flg & SEM_UNDO)
661 un->semadj[sop->sem_num] += sem_op;
1da177e4
LT
662 sop--;
663 }
664
665 return result;
666}
667
4ce33ec2
DB
668static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
669{
670 int result, sem_op, nsops;
671 struct sembuf *sop;
672 struct sem *curr;
673 struct sembuf *sops;
674 struct sem_undo *un;
675
676 sops = q->sops;
677 nsops = q->nsops;
678 un = q->undo;
679
680 if (unlikely(q->dupsop))
681 return perform_atomic_semop_slow(sma, q);
682
683 /*
684 * We scan the semaphore set twice, first to ensure that the entire
685 * operation can succeed, therefore avoiding any pointless writes
686 * to shared memory and having to undo such changes in order to block
687 * until the operations can go through.
688 */
689 for (sop = sops; sop < sops + nsops; sop++) {
1a233956 690 curr = &sma->sems[sop->sem_num];
4ce33ec2
DB
691 sem_op = sop->sem_op;
692 result = curr->semval;
693
694 if (!sem_op && result)
695 goto would_block; /* wait-for-zero */
696
697 result += sem_op;
698 if (result < 0)
699 goto would_block;
700
701 if (result > SEMVMX)
702 return -ERANGE;
703
704 if (sop->sem_flg & SEM_UNDO) {
705 int undo = un->semadj[sop->sem_num] - sem_op;
706
707 /* Exceeding the undo range is an error. */
708 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
709 return -ERANGE;
710 }
711 }
712
713 for (sop = sops; sop < sops + nsops; sop++) {
1a233956 714 curr = &sma->sems[sop->sem_num];
4ce33ec2
DB
715 sem_op = sop->sem_op;
716 result = curr->semval;
717
718 if (sop->sem_flg & SEM_UNDO) {
719 int undo = un->semadj[sop->sem_num] - sem_op;
720
721 un->semadj[sop->sem_num] = undo;
722 }
723 curr->semval += sem_op;
724 curr->sempid = q->pid;
725 }
726
727 return 0;
728
729would_block:
730 q->blocking = sop;
731 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
732}
733
9ae949fa
DB
734static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
735 struct wake_q_head *wake_q)
0a2b9d4c 736{
9ae949fa
DB
737 wake_q_add(wake_q, q->sleeper);
738 /*
739 * Rely on the above implicit barrier, such that we can
740 * ensure that we hold reference to the task before setting
741 * q->status. Otherwise we could race with do_exit if the
742 * task is awoken by an external event before calling
743 * wake_up_process().
744 */
745 WRITE_ONCE(q->status, error);
d4212093
NP
746}
747
b97e820f
MS
748static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
749{
750 list_del(&q->list);
9f1bc2c9 751 if (q->nsops > 1)
b97e820f
MS
752 sma->complex_count--;
753}
754
fd5db422
MS
755/** check_restart(sma, q)
756 * @sma: semaphore array
757 * @q: the operation that just completed
758 *
759 * update_queue is O(N^2) when it restarts scanning the whole queue of
760 * waiting operations. Therefore this function checks if the restart is
761 * really necessary. It is called after a previously waiting operation
1a82e9e1
MS
762 * modified the array.
763 * Note that wait-for-zero operations are handled without restart.
fd5db422 764 */
4663d3e8 765static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
fd5db422 766{
1a82e9e1
MS
767 /* pending complex alter operations are too difficult to analyse */
768 if (!list_empty(&sma->pending_alter))
fd5db422
MS
769 return 1;
770
771 /* we were a sleeping complex operation. Too difficult */
772 if (q->nsops > 1)
773 return 1;
774
1a82e9e1
MS
775 /* It is impossible that someone waits for the new value:
776 * - complex operations always restart.
777 * - wait-for-zero are handled seperately.
778 * - q is a previously sleeping simple operation that
779 * altered the array. It must be a decrement, because
780 * simple increments never sleep.
781 * - If there are older (higher priority) decrements
782 * in the queue, then they have observed the original
783 * semval value and couldn't proceed. The operation
784 * decremented to value - thus they won't proceed either.
785 */
786 return 0;
787}
fd5db422 788
1a82e9e1 789/**
8001c858 790 * wake_const_ops - wake up non-alter tasks
1a82e9e1
MS
791 * @sma: semaphore array.
792 * @semnum: semaphore that was modified.
9ae949fa 793 * @wake_q: lockless wake-queue head.
1a82e9e1
MS
794 *
795 * wake_const_ops must be called after a semaphore in a semaphore array
796 * was set to 0. If complex const operations are pending, wake_const_ops must
797 * be called with semnum = -1, as well as with the number of each modified
798 * semaphore.
9ae949fa 799 * The tasks that must be woken up are added to @wake_q. The return code
1a82e9e1
MS
800 * is stored in q->pid.
801 * The function returns 1 if at least one operation was completed successfully.
802 */
803static int wake_const_ops(struct sem_array *sma, int semnum,
9ae949fa 804 struct wake_q_head *wake_q)
1a82e9e1 805{
f150f02c 806 struct sem_queue *q, *tmp;
1a82e9e1
MS
807 struct list_head *pending_list;
808 int semop_completed = 0;
809
810 if (semnum == -1)
811 pending_list = &sma->pending_const;
812 else
1a233956 813 pending_list = &sma->sems[semnum].pending_const;
fd5db422 814
f150f02c
DB
815 list_for_each_entry_safe(q, tmp, pending_list, list) {
816 int error = perform_atomic_semop(sma, q);
1a82e9e1 817
f150f02c
DB
818 if (error > 0)
819 continue;
820 /* operation completed, remove from queue & wakeup */
821 unlink_queue(sma, q);
1a82e9e1 822
f150f02c
DB
823 wake_up_sem_queue_prepare(q, error, wake_q);
824 if (error == 0)
825 semop_completed = 1;
1a82e9e1 826 }
f150f02c 827
1a82e9e1
MS
828 return semop_completed;
829}
830
831/**
8001c858 832 * do_smart_wakeup_zero - wakeup all wait for zero tasks
1a82e9e1
MS
833 * @sma: semaphore array
834 * @sops: operations that were performed
835 * @nsops: number of operations
9ae949fa 836 * @wake_q: lockless wake-queue head
1a82e9e1 837 *
8001c858
DB
838 * Checks all required queue for wait-for-zero operations, based
839 * on the actual changes that were performed on the semaphore array.
1a82e9e1
MS
840 * The function returns 1 if at least one operation was completed successfully.
841 */
842static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
9ae949fa 843 int nsops, struct wake_q_head *wake_q)
1a82e9e1
MS
844{
845 int i;
846 int semop_completed = 0;
847 int got_zero = 0;
848
849 /* first: the per-semaphore queues, if known */
850 if (sops) {
851 for (i = 0; i < nsops; i++) {
852 int num = sops[i].sem_num;
853
1a233956 854 if (sma->sems[num].semval == 0) {
1a82e9e1 855 got_zero = 1;
9ae949fa 856 semop_completed |= wake_const_ops(sma, num, wake_q);
1a82e9e1
MS
857 }
858 }
859 } else {
860 /*
861 * No sops means modified semaphores not known.
862 * Assume all were changed.
fd5db422 863 */
1a82e9e1 864 for (i = 0; i < sma->sem_nsems; i++) {
1a233956 865 if (sma->sems[i].semval == 0) {
1a82e9e1 866 got_zero = 1;
9ae949fa 867 semop_completed |= wake_const_ops(sma, i, wake_q);
1a82e9e1
MS
868 }
869 }
fd5db422
MS
870 }
871 /*
1a82e9e1
MS
872 * If one of the modified semaphores got 0,
873 * then check the global queue, too.
fd5db422 874 */
1a82e9e1 875 if (got_zero)
9ae949fa 876 semop_completed |= wake_const_ops(sma, -1, wake_q);
fd5db422 877
1a82e9e1 878 return semop_completed;
fd5db422
MS
879}
880
636c6be8
MS
881
882/**
8001c858 883 * update_queue - look for tasks that can be completed.
636c6be8
MS
884 * @sma: semaphore array.
885 * @semnum: semaphore that was modified.
9ae949fa 886 * @wake_q: lockless wake-queue head.
636c6be8
MS
887 *
888 * update_queue must be called after a semaphore in a semaphore array
9f1bc2c9
RR
889 * was modified. If multiple semaphores were modified, update_queue must
890 * be called with semnum = -1, as well as with the number of each modified
891 * semaphore.
9ae949fa 892 * The tasks that must be woken up are added to @wake_q. The return code
0a2b9d4c 893 * is stored in q->pid.
1a82e9e1
MS
894 * The function internally checks if const operations can now succeed.
895 *
0a2b9d4c 896 * The function return 1 if at least one semop was completed successfully.
1da177e4 897 */
9ae949fa 898static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
1da177e4 899{
f150f02c 900 struct sem_queue *q, *tmp;
636c6be8 901 struct list_head *pending_list;
0a2b9d4c 902 int semop_completed = 0;
636c6be8 903
9f1bc2c9 904 if (semnum == -1)
1a82e9e1 905 pending_list = &sma->pending_alter;
9f1bc2c9 906 else
1a233956 907 pending_list = &sma->sems[semnum].pending_alter;
9cad200c
NP
908
909again:
f150f02c 910 list_for_each_entry_safe(q, tmp, pending_list, list) {
fd5db422 911 int error, restart;
636c6be8 912
d987f8b2
MS
913 /* If we are scanning the single sop, per-semaphore list of
914 * one semaphore and that semaphore is 0, then it is not
1a82e9e1 915 * necessary to scan further: simple increments
d987f8b2
MS
916 * that affect only one entry succeed immediately and cannot
917 * be in the per semaphore pending queue, and decrements
918 * cannot be successful if the value is already 0.
919 */
1a233956 920 if (semnum != -1 && sma->sems[semnum].semval == 0)
d987f8b2
MS
921 break;
922
d198cd6d 923 error = perform_atomic_semop(sma, q);
1da177e4
LT
924
925 /* Does q->sleeper still need to sleep? */
9cad200c
NP
926 if (error > 0)
927 continue;
928
b97e820f 929 unlink_queue(sma, q);
9cad200c 930
0a2b9d4c 931 if (error) {
fd5db422 932 restart = 0;
0a2b9d4c
MS
933 } else {
934 semop_completed = 1;
9ae949fa 935 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
fd5db422 936 restart = check_restart(sma, q);
0a2b9d4c 937 }
fd5db422 938
9ae949fa 939 wake_up_sem_queue_prepare(q, error, wake_q);
fd5db422 940 if (restart)
9cad200c 941 goto again;
1da177e4 942 }
0a2b9d4c 943 return semop_completed;
1da177e4
LT
944}
945
0e8c6656 946/**
8001c858 947 * set_semotime - set sem_otime
0e8c6656
MS
948 * @sma: semaphore array
949 * @sops: operations that modified the array, may be NULL
950 *
951 * sem_otime is replicated to avoid cache line trashing.
952 * This function sets one instance to the current time.
953 */
954static void set_semotime(struct sem_array *sma, struct sembuf *sops)
955{
956 if (sops == NULL) {
1a233956 957 sma->sems[0].sem_otime = get_seconds();
0e8c6656 958 } else {
1a233956 959 sma->sems[sops[0].sem_num].sem_otime =
0e8c6656
MS
960 get_seconds();
961 }
962}
963
0a2b9d4c 964/**
8001c858 965 * do_smart_update - optimized update_queue
fd5db422
MS
966 * @sma: semaphore array
967 * @sops: operations that were performed
968 * @nsops: number of operations
0a2b9d4c 969 * @otime: force setting otime
9ae949fa 970 * @wake_q: lockless wake-queue head
fd5db422 971 *
1a82e9e1
MS
972 * do_smart_update() does the required calls to update_queue and wakeup_zero,
973 * based on the actual changes that were performed on the semaphore array.
0a2b9d4c 974 * Note that the function does not do the actual wake-up: the caller is
9ae949fa 975 * responsible for calling wake_up_q().
0a2b9d4c 976 * It is safe to perform this call after dropping all locks.
fd5db422 977 */
0a2b9d4c 978static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
9ae949fa 979 int otime, struct wake_q_head *wake_q)
fd5db422
MS
980{
981 int i;
982
9ae949fa 983 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
1a82e9e1 984
f269f40a
MS
985 if (!list_empty(&sma->pending_alter)) {
986 /* semaphore array uses the global queue - just process it. */
9ae949fa 987 otime |= update_queue(sma, -1, wake_q);
f269f40a
MS
988 } else {
989 if (!sops) {
990 /*
991 * No sops, thus the modified semaphores are not
992 * known. Check all.
993 */
994 for (i = 0; i < sma->sem_nsems; i++)
9ae949fa 995 otime |= update_queue(sma, i, wake_q);
f269f40a
MS
996 } else {
997 /*
998 * Check the semaphores that were increased:
999 * - No complex ops, thus all sleeping ops are
1000 * decrease.
1001 * - if we decreased the value, then any sleeping
1002 * semaphore ops wont be able to run: If the
1003 * previous value was too small, then the new
1004 * value will be too small, too.
1005 */
1006 for (i = 0; i < nsops; i++) {
1007 if (sops[i].sem_op > 0) {
1008 otime |= update_queue(sma,
9ae949fa 1009 sops[i].sem_num, wake_q);
f269f40a 1010 }
ab465df9 1011 }
9f1bc2c9 1012 }
fd5db422 1013 }
0e8c6656
MS
1014 if (otime)
1015 set_semotime(sma, sops);
fd5db422
MS
1016}
1017
2f2ed41d 1018/*
b220c57a 1019 * check_qop: Test if a queued operation sleeps on the semaphore semnum
2f2ed41d
MS
1020 */
1021static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1022 bool count_zero)
1023{
b220c57a 1024 struct sembuf *sop = q->blocking;
2f2ed41d 1025
9b44ee2e
MS
1026 /*
1027 * Linux always (since 0.99.10) reported a task as sleeping on all
1028 * semaphores. This violates SUS, therefore it was changed to the
1029 * standard compliant behavior.
1030 * Give the administrators a chance to notice that an application
1031 * might misbehave because it relies on the Linux behavior.
1032 */
1033 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1034 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1035 current->comm, task_pid_nr(current));
1036
b220c57a
MS
1037 if (sop->sem_num != semnum)
1038 return 0;
2f2ed41d 1039
b220c57a
MS
1040 if (count_zero && sop->sem_op == 0)
1041 return 1;
1042 if (!count_zero && sop->sem_op < 0)
1043 return 1;
1044
1045 return 0;
2f2ed41d
MS
1046}
1047
1da177e4
LT
1048/* The following counts are associated to each semaphore:
1049 * semncnt number of tasks waiting on semval being nonzero
1050 * semzcnt number of tasks waiting on semval being zero
b220c57a
MS
1051 *
1052 * Per definition, a task waits only on the semaphore of the first semop
1053 * that cannot proceed, even if additional operation would block, too.
1da177e4 1054 */
2f2ed41d
MS
1055static int count_semcnt(struct sem_array *sma, ushort semnum,
1056 bool count_zero)
1da177e4 1057{
2f2ed41d 1058 struct list_head *l;
239521f3 1059 struct sem_queue *q;
2f2ed41d 1060 int semcnt;
1da177e4 1061
2f2ed41d
MS
1062 semcnt = 0;
1063 /* First: check the simple operations. They are easy to evaluate */
1064 if (count_zero)
1a233956 1065 l = &sma->sems[semnum].pending_const;
2f2ed41d 1066 else
1a233956 1067 l = &sma->sems[semnum].pending_alter;
1da177e4 1068
2f2ed41d
MS
1069 list_for_each_entry(q, l, list) {
1070 /* all task on a per-semaphore list sleep on exactly
1071 * that semaphore
1072 */
1073 semcnt++;
ebc2e5e6
RR
1074 }
1075
2f2ed41d 1076 /* Then: check the complex operations. */
1994862d 1077 list_for_each_entry(q, &sma->pending_alter, list) {
2f2ed41d
MS
1078 semcnt += check_qop(sma, semnum, q, count_zero);
1079 }
1080 if (count_zero) {
1081 list_for_each_entry(q, &sma->pending_const, list) {
1082 semcnt += check_qop(sma, semnum, q, count_zero);
1083 }
1994862d 1084 }
2f2ed41d 1085 return semcnt;
1da177e4
LT
1086}
1087
d9a605e4
DB
1088/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1089 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
3e148c79 1090 * remains locked on exit.
1da177e4 1091 */
01b8b07a 1092static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
1da177e4 1093{
380af1b3
MS
1094 struct sem_undo *un, *tu;
1095 struct sem_queue *q, *tq;
01b8b07a 1096 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
9f1bc2c9 1097 int i;
9ae949fa 1098 DEFINE_WAKE_Q(wake_q);
1da177e4 1099
380af1b3 1100 /* Free the existing undo structures for this semaphore set. */
cf9d5d78 1101 ipc_assert_locked_object(&sma->sem_perm);
380af1b3
MS
1102 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1103 list_del(&un->list_id);
1104 spin_lock(&un->ulp->lock);
1da177e4 1105 un->semid = -1;
380af1b3
MS
1106 list_del_rcu(&un->list_proc);
1107 spin_unlock(&un->ulp->lock);
693a8b6e 1108 kfree_rcu(un, rcu);
380af1b3 1109 }
1da177e4
LT
1110
1111 /* Wake up all pending processes and let them fail with EIDRM. */
1a82e9e1
MS
1112 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1113 unlink_queue(sma, q);
9ae949fa 1114 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1a82e9e1
MS
1115 }
1116
1117 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
b97e820f 1118 unlink_queue(sma, q);
9ae949fa 1119 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1da177e4 1120 }
9f1bc2c9 1121 for (i = 0; i < sma->sem_nsems; i++) {
1a233956 1122 struct sem *sem = &sma->sems[i];
1a82e9e1
MS
1123 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1124 unlink_queue(sma, q);
9ae949fa 1125 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
1a82e9e1
MS
1126 }
1127 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
9f1bc2c9 1128 unlink_queue(sma, q);
9ae949fa 1129 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
9f1bc2c9
RR
1130 }
1131 }
1da177e4 1132
7ca7e564
ND
1133 /* Remove the semaphore set from the IDR */
1134 sem_rmid(ns, sma);
6062a8dc 1135 sem_unlock(sma, -1);
6d49dab8 1136 rcu_read_unlock();
1da177e4 1137
9ae949fa 1138 wake_up_q(&wake_q);
e3893534 1139 ns->used_sems -= sma->sem_nsems;
dba4cdd3 1140 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1141}
1142
1143static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1144{
239521f3 1145 switch (version) {
1da177e4
LT
1146 case IPC_64:
1147 return copy_to_user(buf, in, sizeof(*in));
1148 case IPC_OLD:
1149 {
1150 struct semid_ds out;
1151
982f7c2b
DR
1152 memset(&out, 0, sizeof(out));
1153
1da177e4
LT
1154 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1155
1156 out.sem_otime = in->sem_otime;
1157 out.sem_ctime = in->sem_ctime;
1158 out.sem_nsems = in->sem_nsems;
1159
1160 return copy_to_user(buf, &out, sizeof(out));
1161 }
1162 default:
1163 return -EINVAL;
1164 }
1165}
1166
e54d02b2 1167static time64_t get_semotime(struct sem_array *sma)
d12e1e50
MS
1168{
1169 int i;
e54d02b2 1170 time64_t res;
d12e1e50 1171
1a233956 1172 res = sma->sems[0].sem_otime;
d12e1e50 1173 for (i = 1; i < sma->sem_nsems; i++) {
e54d02b2 1174 time64_t to = sma->sems[i].sem_otime;
d12e1e50
MS
1175
1176 if (to > res)
1177 res = to;
1178 }
1179 return res;
1180}
1181
45a4a64a
AV
1182static int semctl_stat(struct ipc_namespace *ns, int semid,
1183 int cmd, struct semid64_ds *semid64)
1da177e4 1184{
1da177e4 1185 struct sem_array *sma;
45a4a64a
AV
1186 int id = 0;
1187 int err;
1da177e4 1188
45a4a64a 1189 memset(semid64, 0, sizeof(*semid64));
46c0a8ca 1190
45a4a64a
AV
1191 rcu_read_lock();
1192 if (cmd == SEM_STAT) {
1193 sma = sem_obtain_object(ns, semid);
1194 if (IS_ERR(sma)) {
1195 err = PTR_ERR(sma);
1196 goto out_unlock;
1197 }
1198 id = sma->sem_perm.id;
1199 } else {
1200 sma = sem_obtain_object_check(ns, semid);
1201 if (IS_ERR(sma)) {
1202 err = PTR_ERR(sma);
1203 goto out_unlock;
1da177e4 1204 }
1da177e4 1205 }
1da177e4 1206
45a4a64a
AV
1207 err = -EACCES;
1208 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1209 goto out_unlock;
1da177e4 1210
45a4a64a
AV
1211 err = security_sem_semctl(sma, cmd);
1212 if (err)
1213 goto out_unlock;
1da177e4 1214
45a4a64a
AV
1215 kernel_to_ipc64_perm(&sma->sem_perm, &semid64->sem_perm);
1216 semid64->sem_otime = get_semotime(sma);
1217 semid64->sem_ctime = sma->sem_ctime;
1218 semid64->sem_nsems = sma->sem_nsems;
1219 rcu_read_unlock();
1220 return id;
1da177e4 1221
1da177e4 1222out_unlock:
16df3674 1223 rcu_read_unlock();
1da177e4
LT
1224 return err;
1225}
1226
45a4a64a
AV
1227static int semctl_info(struct ipc_namespace *ns, int semid,
1228 int cmd, void __user *p)
1229{
1230 struct seminfo seminfo;
1231 int max_id;
1232 int err;
1233
1234 err = security_sem_semctl(NULL, cmd);
1235 if (err)
1236 return err;
1237
1238 memset(&seminfo, 0, sizeof(seminfo));
1239 seminfo.semmni = ns->sc_semmni;
1240 seminfo.semmns = ns->sc_semmns;
1241 seminfo.semmsl = ns->sc_semmsl;
1242 seminfo.semopm = ns->sc_semopm;
1243 seminfo.semvmx = SEMVMX;
1244 seminfo.semmnu = SEMMNU;
1245 seminfo.semmap = SEMMAP;
1246 seminfo.semume = SEMUME;
1247 down_read(&sem_ids(ns).rwsem);
1248 if (cmd == SEM_INFO) {
1249 seminfo.semusz = sem_ids(ns).in_use;
1250 seminfo.semaem = ns->used_sems;
1251 } else {
1252 seminfo.semusz = SEMUSZ;
1253 seminfo.semaem = SEMAEM;
1254 }
1255 max_id = ipc_get_maxid(&sem_ids(ns));
1256 up_read(&sem_ids(ns).rwsem);
1257 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1258 return -EFAULT;
1259 return (max_id < 0) ? 0 : max_id;
1260}
1261
e1fd1f49 1262static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
45a4a64a 1263 int val)
e1fd1f49
AV
1264{
1265 struct sem_undo *un;
1266 struct sem_array *sma;
239521f3 1267 struct sem *curr;
45a4a64a 1268 int err;
9ae949fa
DB
1269 DEFINE_WAKE_Q(wake_q);
1270
6062a8dc
RR
1271 if (val > SEMVMX || val < 0)
1272 return -ERANGE;
e1fd1f49 1273
6062a8dc
RR
1274 rcu_read_lock();
1275 sma = sem_obtain_object_check(ns, semid);
1276 if (IS_ERR(sma)) {
1277 rcu_read_unlock();
1278 return PTR_ERR(sma);
1279 }
1280
1281 if (semnum < 0 || semnum >= sma->sem_nsems) {
1282 rcu_read_unlock();
1283 return -EINVAL;
1284 }
1285
1286
1287 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1288 rcu_read_unlock();
1289 return -EACCES;
1290 }
e1fd1f49
AV
1291
1292 err = security_sem_semctl(sma, SETVAL);
6062a8dc
RR
1293 if (err) {
1294 rcu_read_unlock();
1295 return -EACCES;
1296 }
e1fd1f49 1297
6062a8dc 1298 sem_lock(sma, NULL, -1);
e1fd1f49 1299
0f3d2b01 1300 if (!ipc_valid_object(&sma->sem_perm)) {
6e224f94
MS
1301 sem_unlock(sma, -1);
1302 rcu_read_unlock();
1303 return -EIDRM;
1304 }
1305
1a233956 1306 curr = &sma->sems[semnum];
e1fd1f49 1307
cf9d5d78 1308 ipc_assert_locked_object(&sma->sem_perm);
e1fd1f49
AV
1309 list_for_each_entry(un, &sma->list_id, list_id)
1310 un->semadj[semnum] = 0;
1311
1312 curr->semval = val;
1313 curr->sempid = task_tgid_vnr(current);
e54d02b2 1314 sma->sem_ctime = ktime_get_real_seconds();
e1fd1f49 1315 /* maybe some queued-up processes were waiting for this */
9ae949fa 1316 do_smart_update(sma, NULL, 0, 0, &wake_q);
6062a8dc 1317 sem_unlock(sma, -1);
6d49dab8 1318 rcu_read_unlock();
9ae949fa 1319 wake_up_q(&wake_q);
6062a8dc 1320 return 0;
e1fd1f49
AV
1321}
1322
e3893534 1323static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
e1fd1f49 1324 int cmd, void __user *p)
1da177e4
LT
1325{
1326 struct sem_array *sma;
239521f3 1327 struct sem *curr;
16df3674 1328 int err, nsems;
1da177e4 1329 ushort fast_sem_io[SEMMSL_FAST];
239521f3 1330 ushort *sem_io = fast_sem_io;
9ae949fa 1331 DEFINE_WAKE_Q(wake_q);
16df3674
DB
1332
1333 rcu_read_lock();
1334 sma = sem_obtain_object_check(ns, semid);
1335 if (IS_ERR(sma)) {
1336 rcu_read_unlock();
023a5355 1337 return PTR_ERR(sma);
16df3674 1338 }
1da177e4
LT
1339
1340 nsems = sma->sem_nsems;
1341
1da177e4 1342 err = -EACCES;
c728b9c8
LT
1343 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1344 goto out_rcu_wakeup;
1da177e4
LT
1345
1346 err = security_sem_semctl(sma, cmd);
c728b9c8
LT
1347 if (err)
1348 goto out_rcu_wakeup;
1da177e4
LT
1349
1350 err = -EACCES;
1351 switch (cmd) {
1352 case GETALL:
1353 {
e1fd1f49 1354 ushort __user *array = p;
1da177e4
LT
1355 int i;
1356
ce857229 1357 sem_lock(sma, NULL, -1);
0f3d2b01 1358 if (!ipc_valid_object(&sma->sem_perm)) {
6e224f94
MS
1359 err = -EIDRM;
1360 goto out_unlock;
1361 }
239521f3 1362 if (nsems > SEMMSL_FAST) {
dba4cdd3 1363 if (!ipc_rcu_getref(&sma->sem_perm)) {
ce857229 1364 err = -EIDRM;
6e224f94 1365 goto out_unlock;
ce857229
AV
1366 }
1367 sem_unlock(sma, -1);
6d49dab8 1368 rcu_read_unlock();
f8dbe8d2
KC
1369 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1370 GFP_KERNEL);
239521f3 1371 if (sem_io == NULL) {
dba4cdd3 1372 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1373 return -ENOMEM;
1374 }
1375
4091fd94 1376 rcu_read_lock();
6ff37972 1377 sem_lock_and_putref(sma);
0f3d2b01 1378 if (!ipc_valid_object(&sma->sem_perm)) {
1da177e4 1379 err = -EIDRM;
6e224f94 1380 goto out_unlock;
1da177e4 1381 }
ce857229 1382 }
1da177e4 1383 for (i = 0; i < sma->sem_nsems; i++)
1a233956 1384 sem_io[i] = sma->sems[i].semval;
6062a8dc 1385 sem_unlock(sma, -1);
6d49dab8 1386 rcu_read_unlock();
1da177e4 1387 err = 0;
239521f3 1388 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
1da177e4
LT
1389 err = -EFAULT;
1390 goto out_free;
1391 }
1392 case SETALL:
1393 {
1394 int i;
1395 struct sem_undo *un;
1396
dba4cdd3 1397 if (!ipc_rcu_getref(&sma->sem_perm)) {
6e224f94
MS
1398 err = -EIDRM;
1399 goto out_rcu_wakeup;
6062a8dc 1400 }
16df3674 1401 rcu_read_unlock();
1da177e4 1402
239521f3 1403 if (nsems > SEMMSL_FAST) {
f8dbe8d2
KC
1404 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1405 GFP_KERNEL);
239521f3 1406 if (sem_io == NULL) {
dba4cdd3 1407 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1408 return -ENOMEM;
1409 }
1410 }
1411
239521f3 1412 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
dba4cdd3 1413 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1414 err = -EFAULT;
1415 goto out_free;
1416 }
1417
1418 for (i = 0; i < nsems; i++) {
1419 if (sem_io[i] > SEMVMX) {
dba4cdd3 1420 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1421 err = -ERANGE;
1422 goto out_free;
1423 }
1424 }
4091fd94 1425 rcu_read_lock();
6ff37972 1426 sem_lock_and_putref(sma);
0f3d2b01 1427 if (!ipc_valid_object(&sma->sem_perm)) {
1da177e4 1428 err = -EIDRM;
6e224f94 1429 goto out_unlock;
1da177e4
LT
1430 }
1431
a5f4db87 1432 for (i = 0; i < nsems; i++) {
1a233956
MS
1433 sma->sems[i].semval = sem_io[i];
1434 sma->sems[i].sempid = task_tgid_vnr(current);
a5f4db87 1435 }
4daa28f6 1436
cf9d5d78 1437 ipc_assert_locked_object(&sma->sem_perm);
4daa28f6 1438 list_for_each_entry(un, &sma->list_id, list_id) {
1da177e4
LT
1439 for (i = 0; i < nsems; i++)
1440 un->semadj[i] = 0;
4daa28f6 1441 }
e54d02b2 1442 sma->sem_ctime = ktime_get_real_seconds();
1da177e4 1443 /* maybe some queued-up processes were waiting for this */
9ae949fa 1444 do_smart_update(sma, NULL, 0, 0, &wake_q);
1da177e4
LT
1445 err = 0;
1446 goto out_unlock;
1447 }
e1fd1f49 1448 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1da177e4
LT
1449 }
1450 err = -EINVAL;
c728b9c8
LT
1451 if (semnum < 0 || semnum >= nsems)
1452 goto out_rcu_wakeup;
1da177e4 1453
6062a8dc 1454 sem_lock(sma, NULL, -1);
0f3d2b01 1455 if (!ipc_valid_object(&sma->sem_perm)) {
6e224f94
MS
1456 err = -EIDRM;
1457 goto out_unlock;
1458 }
1a233956 1459 curr = &sma->sems[semnum];
1da177e4
LT
1460
1461 switch (cmd) {
1462 case GETVAL:
1463 err = curr->semval;
1464 goto out_unlock;
1465 case GETPID:
1466 err = curr->sempid;
1467 goto out_unlock;
1468 case GETNCNT:
2f2ed41d 1469 err = count_semcnt(sma, semnum, 0);
1da177e4
LT
1470 goto out_unlock;
1471 case GETZCNT:
2f2ed41d 1472 err = count_semcnt(sma, semnum, 1);
1da177e4 1473 goto out_unlock;
1da177e4 1474 }
16df3674 1475
1da177e4 1476out_unlock:
6062a8dc 1477 sem_unlock(sma, -1);
c728b9c8 1478out_rcu_wakeup:
6d49dab8 1479 rcu_read_unlock();
9ae949fa 1480 wake_up_q(&wake_q);
1da177e4 1481out_free:
239521f3 1482 if (sem_io != fast_sem_io)
f8dbe8d2 1483 kvfree(sem_io);
1da177e4
LT
1484 return err;
1485}
1486
016d7132
PP
1487static inline unsigned long
1488copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
1da177e4 1489{
239521f3 1490 switch (version) {
1da177e4 1491 case IPC_64:
016d7132 1492 if (copy_from_user(out, buf, sizeof(*out)))
1da177e4 1493 return -EFAULT;
1da177e4 1494 return 0;
1da177e4
LT
1495 case IPC_OLD:
1496 {
1497 struct semid_ds tbuf_old;
1498
239521f3 1499 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
1da177e4
LT
1500 return -EFAULT;
1501
016d7132
PP
1502 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1503 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1504 out->sem_perm.mode = tbuf_old.sem_perm.mode;
1da177e4
LT
1505
1506 return 0;
1507 }
1508 default:
1509 return -EINVAL;
1510 }
1511}
1512
522bb2a2 1513/*
d9a605e4 1514 * This function handles some semctl commands which require the rwsem
522bb2a2 1515 * to be held in write mode.
d9a605e4 1516 * NOTE: no locks must be held, the rwsem is taken inside this function.
522bb2a2 1517 */
21a4826a 1518static int semctl_down(struct ipc_namespace *ns, int semid,
45a4a64a 1519 int cmd, struct semid64_ds *semid64)
1da177e4
LT
1520{
1521 struct sem_array *sma;
1522 int err;
1da177e4
LT
1523 struct kern_ipc_perm *ipcp;
1524
d9a605e4 1525 down_write(&sem_ids(ns).rwsem);
7b4cc5d8
DB
1526 rcu_read_lock();
1527
16df3674 1528 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
45a4a64a 1529 &semid64->sem_perm, 0);
7b4cc5d8
DB
1530 if (IS_ERR(ipcp)) {
1531 err = PTR_ERR(ipcp);
7b4cc5d8
DB
1532 goto out_unlock1;
1533 }
073115d6 1534
a5f75e7f 1535 sma = container_of(ipcp, struct sem_array, sem_perm);
1da177e4
LT
1536
1537 err = security_sem_semctl(sma, cmd);
7b4cc5d8
DB
1538 if (err)
1539 goto out_unlock1;
1da177e4 1540
7b4cc5d8 1541 switch (cmd) {
1da177e4 1542 case IPC_RMID:
6062a8dc 1543 sem_lock(sma, NULL, -1);
7b4cc5d8 1544 /* freeary unlocks the ipc object and rcu */
01b8b07a 1545 freeary(ns, ipcp);
522bb2a2 1546 goto out_up;
1da177e4 1547 case IPC_SET:
6062a8dc 1548 sem_lock(sma, NULL, -1);
45a4a64a 1549 err = ipc_update_perm(&semid64->sem_perm, ipcp);
1efdb69b 1550 if (err)
7b4cc5d8 1551 goto out_unlock0;
e54d02b2 1552 sma->sem_ctime = ktime_get_real_seconds();
1da177e4
LT
1553 break;
1554 default:
1da177e4 1555 err = -EINVAL;
7b4cc5d8 1556 goto out_unlock1;
1da177e4 1557 }
1da177e4 1558
7b4cc5d8 1559out_unlock0:
6062a8dc 1560 sem_unlock(sma, -1);
7b4cc5d8 1561out_unlock1:
6d49dab8 1562 rcu_read_unlock();
522bb2a2 1563out_up:
d9a605e4 1564 up_write(&sem_ids(ns).rwsem);
1da177e4
LT
1565 return err;
1566}
1567
e1fd1f49 1568SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
1da177e4 1569{
1da177e4 1570 int version;
e3893534 1571 struct ipc_namespace *ns;
e1fd1f49 1572 void __user *p = (void __user *)arg;
45a4a64a
AV
1573 struct semid64_ds semid64;
1574 int err;
1da177e4
LT
1575
1576 if (semid < 0)
1577 return -EINVAL;
1578
1579 version = ipc_parse_version(&cmd);
e3893534 1580 ns = current->nsproxy->ipc_ns;
1da177e4 1581
239521f3 1582 switch (cmd) {
1da177e4
LT
1583 case IPC_INFO:
1584 case SEM_INFO:
45a4a64a 1585 return semctl_info(ns, semid, cmd, p);
4b9fcb0e 1586 case IPC_STAT:
1da177e4 1587 case SEM_STAT:
45a4a64a
AV
1588 err = semctl_stat(ns, semid, cmd, &semid64);
1589 if (err < 0)
1590 return err;
1591 if (copy_semid_to_user(p, &semid64, version))
1592 err = -EFAULT;
1593 return err;
1da177e4
LT
1594 case GETALL:
1595 case GETVAL:
1596 case GETPID:
1597 case GETNCNT:
1598 case GETZCNT:
1da177e4 1599 case SETALL:
e1fd1f49 1600 return semctl_main(ns, semid, semnum, cmd, p);
45a4a64a
AV
1601 case SETVAL: {
1602 int val;
1603#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1604 /* big-endian 64bit */
1605 val = arg >> 32;
1606#else
1607 /* 32bit or little-endian 64bit */
1608 val = arg;
1609#endif
1610 return semctl_setval(ns, semid, semnum, val);
1611 }
1da177e4 1612 case IPC_SET:
45a4a64a
AV
1613 if (copy_semid_from_user(&semid64, p, version))
1614 return -EFAULT;
1615 case IPC_RMID:
1616 return semctl_down(ns, semid, cmd, &semid64);
1da177e4
LT
1617 default:
1618 return -EINVAL;
1619 }
1620}
1621
c0ebccb6
AV
1622#ifdef CONFIG_COMPAT
1623
1624struct compat_semid_ds {
1625 struct compat_ipc_perm sem_perm;
1626 compat_time_t sem_otime;
1627 compat_time_t sem_ctime;
1628 compat_uptr_t sem_base;
1629 compat_uptr_t sem_pending;
1630 compat_uptr_t sem_pending_last;
1631 compat_uptr_t undo;
1632 unsigned short sem_nsems;
1633};
1634
1635static int copy_compat_semid_from_user(struct semid64_ds *out, void __user *buf,
1636 int version)
1637{
1638 memset(out, 0, sizeof(*out));
1639 if (version == IPC_64) {
1640 struct compat_semid64_ds *p = buf;
1641 return get_compat_ipc64_perm(&out->sem_perm, &p->sem_perm);
1642 } else {
1643 struct compat_semid_ds *p = buf;
1644 return get_compat_ipc_perm(&out->sem_perm, &p->sem_perm);
1645 }
1646}
1647
1648static int copy_compat_semid_to_user(void __user *buf, struct semid64_ds *in,
1649 int version)
1650{
1651 if (version == IPC_64) {
1652 struct compat_semid64_ds v;
1653 memset(&v, 0, sizeof(v));
1654 to_compat_ipc64_perm(&v.sem_perm, &in->sem_perm);
1655 v.sem_otime = in->sem_otime;
1656 v.sem_ctime = in->sem_ctime;
1657 v.sem_nsems = in->sem_nsems;
1658 return copy_to_user(buf, &v, sizeof(v));
1659 } else {
1660 struct compat_semid_ds v;
1661 memset(&v, 0, sizeof(v));
1662 to_compat_ipc_perm(&v.sem_perm, &in->sem_perm);
1663 v.sem_otime = in->sem_otime;
1664 v.sem_ctime = in->sem_ctime;
1665 v.sem_nsems = in->sem_nsems;
1666 return copy_to_user(buf, &v, sizeof(v));
1667 }
1668}
1669
1670COMPAT_SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, int, arg)
1671{
1672 void __user *p = compat_ptr(arg);
1673 struct ipc_namespace *ns;
1674 struct semid64_ds semid64;
1675 int version = compat_ipc_parse_version(&cmd);
1676 int err;
1677
1678 ns = current->nsproxy->ipc_ns;
1679
1680 if (semid < 0)
1681 return -EINVAL;
1682
1683 switch (cmd & (~IPC_64)) {
1684 case IPC_INFO:
1685 case SEM_INFO:
1686 return semctl_info(ns, semid, cmd, p);
1687 case IPC_STAT:
1688 case SEM_STAT:
1689 err = semctl_stat(ns, semid, cmd, &semid64);
1690 if (err < 0)
1691 return err;
1692 if (copy_compat_semid_to_user(p, &semid64, version))
1693 err = -EFAULT;
1694 return err;
1695 case GETVAL:
1696 case GETPID:
1697 case GETNCNT:
1698 case GETZCNT:
1699 case GETALL:
1da177e4 1700 case SETALL:
e1fd1f49
AV
1701 return semctl_main(ns, semid, semnum, cmd, p);
1702 case SETVAL:
1703 return semctl_setval(ns, semid, semnum, arg);
1da177e4 1704 case IPC_SET:
c0ebccb6
AV
1705 if (copy_compat_semid_from_user(&semid64, p, version))
1706 return -EFAULT;
1707 /* fallthru */
1708 case IPC_RMID:
1709 return semctl_down(ns, semid, cmd, &semid64);
1da177e4
LT
1710 default:
1711 return -EINVAL;
1712 }
1713}
c0ebccb6 1714#endif
1da177e4 1715
1da177e4
LT
1716/* If the task doesn't already have a undo_list, then allocate one
1717 * here. We guarantee there is only one thread using this undo list,
1718 * and current is THE ONE
1719 *
1720 * If this allocation and assignment succeeds, but later
1721 * portions of this code fail, there is no need to free the sem_undo_list.
1722 * Just let it stay associated with the task, and it'll be freed later
1723 * at exit time.
1724 *
1725 * This can block, so callers must hold no locks.
1726 */
1727static inline int get_undo_list(struct sem_undo_list **undo_listp)
1728{
1729 struct sem_undo_list *undo_list;
1da177e4
LT
1730
1731 undo_list = current->sysvsem.undo_list;
1732 if (!undo_list) {
2453a306 1733 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
1da177e4
LT
1734 if (undo_list == NULL)
1735 return -ENOMEM;
00a5dfdb 1736 spin_lock_init(&undo_list->lock);
f74370b8 1737 refcount_set(&undo_list->refcnt, 1);
4daa28f6
MS
1738 INIT_LIST_HEAD(&undo_list->list_proc);
1739
1da177e4
LT
1740 current->sysvsem.undo_list = undo_list;
1741 }
1742 *undo_listp = undo_list;
1743 return 0;
1744}
1745
bf17bb71 1746static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
1da177e4 1747{
bf17bb71 1748 struct sem_undo *un;
4daa28f6 1749
bf17bb71
NP
1750 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1751 if (un->semid == semid)
1752 return un;
1da177e4 1753 }
4daa28f6 1754 return NULL;
1da177e4
LT
1755}
1756
bf17bb71
NP
1757static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1758{
1759 struct sem_undo *un;
1760
239521f3 1761 assert_spin_locked(&ulp->lock);
bf17bb71
NP
1762
1763 un = __lookup_undo(ulp, semid);
1764 if (un) {
1765 list_del_rcu(&un->list_proc);
1766 list_add_rcu(&un->list_proc, &ulp->list_proc);
1767 }
1768 return un;
1769}
1770
4daa28f6 1771/**
8001c858 1772 * find_alloc_undo - lookup (and if not present create) undo array
4daa28f6
MS
1773 * @ns: namespace
1774 * @semid: semaphore array id
1775 *
1776 * The function looks up (and if not present creates) the undo structure.
1777 * The size of the undo structure depends on the size of the semaphore
1778 * array, thus the alloc path is not that straightforward.
380af1b3
MS
1779 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1780 * performs a rcu_read_lock().
4daa28f6
MS
1781 */
1782static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
1da177e4
LT
1783{
1784 struct sem_array *sma;
1785 struct sem_undo_list *ulp;
1786 struct sem_undo *un, *new;
6062a8dc 1787 int nsems, error;
1da177e4
LT
1788
1789 error = get_undo_list(&ulp);
1790 if (error)
1791 return ERR_PTR(error);
1792
380af1b3 1793 rcu_read_lock();
c530c6ac 1794 spin_lock(&ulp->lock);
1da177e4 1795 un = lookup_undo(ulp, semid);
c530c6ac 1796 spin_unlock(&ulp->lock);
239521f3 1797 if (likely(un != NULL))
1da177e4
LT
1798 goto out;
1799
1800 /* no undo structure around - allocate one. */
4daa28f6 1801 /* step 1: figure out the size of the semaphore array */
16df3674
DB
1802 sma = sem_obtain_object_check(ns, semid);
1803 if (IS_ERR(sma)) {
1804 rcu_read_unlock();
4de85cd6 1805 return ERR_CAST(sma);
16df3674 1806 }
023a5355 1807
1da177e4 1808 nsems = sma->sem_nsems;
dba4cdd3 1809 if (!ipc_rcu_getref(&sma->sem_perm)) {
6062a8dc
RR
1810 rcu_read_unlock();
1811 un = ERR_PTR(-EIDRM);
1812 goto out;
1813 }
16df3674 1814 rcu_read_unlock();
1da177e4 1815
4daa28f6 1816 /* step 2: allocate new undo structure */
4668edc3 1817 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
1da177e4 1818 if (!new) {
dba4cdd3 1819 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
1da177e4
LT
1820 return ERR_PTR(-ENOMEM);
1821 }
1da177e4 1822
380af1b3 1823 /* step 3: Acquire the lock on semaphore array */
4091fd94 1824 rcu_read_lock();
6ff37972 1825 sem_lock_and_putref(sma);
0f3d2b01 1826 if (!ipc_valid_object(&sma->sem_perm)) {
6062a8dc 1827 sem_unlock(sma, -1);
6d49dab8 1828 rcu_read_unlock();
1da177e4
LT
1829 kfree(new);
1830 un = ERR_PTR(-EIDRM);
1831 goto out;
1832 }
380af1b3
MS
1833 spin_lock(&ulp->lock);
1834
1835 /*
1836 * step 4: check for races: did someone else allocate the undo struct?
1837 */
1838 un = lookup_undo(ulp, semid);
1839 if (un) {
1840 kfree(new);
1841 goto success;
1842 }
4daa28f6
MS
1843 /* step 5: initialize & link new undo structure */
1844 new->semadj = (short *) &new[1];
380af1b3 1845 new->ulp = ulp;
4daa28f6
MS
1846 new->semid = semid;
1847 assert_spin_locked(&ulp->lock);
380af1b3 1848 list_add_rcu(&new->list_proc, &ulp->list_proc);
cf9d5d78 1849 ipc_assert_locked_object(&sma->sem_perm);
4daa28f6 1850 list_add(&new->list_id, &sma->list_id);
380af1b3 1851 un = new;
4daa28f6 1852
380af1b3 1853success:
c530c6ac 1854 spin_unlock(&ulp->lock);
6062a8dc 1855 sem_unlock(sma, -1);
1da177e4
LT
1856out:
1857 return un;
1858}
1859
44ee4546 1860static long do_semtimedop(int semid, struct sembuf __user *tsops,
3ef56dc2 1861 unsigned nsops, const struct timespec64 *timeout)
1da177e4
LT
1862{
1863 int error = -EINVAL;
1864 struct sem_array *sma;
1865 struct sembuf fast_sops[SEMOPM_FAST];
239521f3 1866 struct sembuf *sops = fast_sops, *sop;
1da177e4 1867 struct sem_undo *un;
4ce33ec2
DB
1868 int max, locknum;
1869 bool undos = false, alter = false, dupsop = false;
1da177e4 1870 struct sem_queue queue;
4ce33ec2 1871 unsigned long dup = 0, jiffies_left = 0;
e3893534
KK
1872 struct ipc_namespace *ns;
1873
1874 ns = current->nsproxy->ipc_ns;
1da177e4
LT
1875
1876 if (nsops < 1 || semid < 0)
1877 return -EINVAL;
e3893534 1878 if (nsops > ns->sc_semopm)
1da177e4 1879 return -E2BIG;
239521f3 1880 if (nsops > SEMOPM_FAST) {
e4243b80 1881 sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
239521f3 1882 if (sops == NULL)
1da177e4
LT
1883 return -ENOMEM;
1884 }
4ce33ec2 1885
239521f3
MS
1886 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1887 error = -EFAULT;
1da177e4
LT
1888 goto out_free;
1889 }
4ce33ec2 1890
1da177e4 1891 if (timeout) {
44ee4546
AV
1892 if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 ||
1893 timeout->tv_nsec >= 1000000000L) {
1da177e4
LT
1894 error = -EINVAL;
1895 goto out_free;
1896 }
3ef56dc2 1897 jiffies_left = timespec64_to_jiffies(timeout);
1da177e4 1898 }
4ce33ec2 1899
1da177e4
LT
1900 max = 0;
1901 for (sop = sops; sop < sops + nsops; sop++) {
4ce33ec2
DB
1902 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1903
1da177e4
LT
1904 if (sop->sem_num >= max)
1905 max = sop->sem_num;
1906 if (sop->sem_flg & SEM_UNDO)
4ce33ec2
DB
1907 undos = true;
1908 if (dup & mask) {
1909 /*
1910 * There was a previous alter access that appears
1911 * to have accessed the same semaphore, thus use
1912 * the dupsop logic. "appears", because the detection
1913 * can only check % BITS_PER_LONG.
1914 */
1915 dupsop = true;
1916 }
1917 if (sop->sem_op != 0) {
1918 alter = true;
1919 dup |= mask;
1920 }
1da177e4 1921 }
1da177e4 1922
1da177e4 1923 if (undos) {
6062a8dc 1924 /* On success, find_alloc_undo takes the rcu_read_lock */
4daa28f6 1925 un = find_alloc_undo(ns, semid);
1da177e4
LT
1926 if (IS_ERR(un)) {
1927 error = PTR_ERR(un);
1928 goto out_free;
1929 }
6062a8dc 1930 } else {
1da177e4 1931 un = NULL;
6062a8dc
RR
1932 rcu_read_lock();
1933 }
1da177e4 1934
16df3674 1935 sma = sem_obtain_object_check(ns, semid);
023a5355 1936 if (IS_ERR(sma)) {
6062a8dc 1937 rcu_read_unlock();
023a5355 1938 error = PTR_ERR(sma);
1da177e4 1939 goto out_free;
023a5355
ND
1940 }
1941
16df3674 1942 error = -EFBIG;
248e7357
DB
1943 if (max >= sma->sem_nsems) {
1944 rcu_read_unlock();
1945 goto out_free;
1946 }
16df3674
DB
1947
1948 error = -EACCES;
248e7357
DB
1949 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1950 rcu_read_unlock();
1951 goto out_free;
1952 }
16df3674
DB
1953
1954 error = security_sem_semop(sma, sops, nsops, alter);
248e7357
DB
1955 if (error) {
1956 rcu_read_unlock();
1957 goto out_free;
1958 }
16df3674 1959
6e224f94
MS
1960 error = -EIDRM;
1961 locknum = sem_lock(sma, sops, nsops);
0f3d2b01
RA
1962 /*
1963 * We eventually might perform the following check in a lockless
1964 * fashion, considering ipc_valid_object() locking constraints.
1965 * If nsops == 1 and there is no contention for sem_perm.lock, then
1966 * only a per-semaphore lock is held and it's OK to proceed with the
1967 * check below. More details on the fine grained locking scheme
1968 * entangled here and why it's RMID race safe on comments at sem_lock()
1969 */
1970 if (!ipc_valid_object(&sma->sem_perm))
6e224f94 1971 goto out_unlock_free;
1da177e4 1972 /*
4daa28f6 1973 * semid identifiers are not unique - find_alloc_undo may have
1da177e4 1974 * allocated an undo structure, it was invalidated by an RMID
4daa28f6 1975 * and now a new array with received the same id. Check and fail.
25985edc 1976 * This case can be detected checking un->semid. The existence of
380af1b3 1977 * "un" itself is guaranteed by rcu.
1da177e4 1978 */
6062a8dc
RR
1979 if (un && un->semid == -1)
1980 goto out_unlock_free;
4daa28f6 1981
d198cd6d
MS
1982 queue.sops = sops;
1983 queue.nsops = nsops;
1984 queue.undo = un;
1985 queue.pid = task_tgid_vnr(current);
1986 queue.alter = alter;
4ce33ec2 1987 queue.dupsop = dupsop;
d198cd6d
MS
1988
1989 error = perform_atomic_semop(sma, &queue);
9ae949fa
DB
1990 if (error == 0) { /* non-blocking succesfull path */
1991 DEFINE_WAKE_Q(wake_q);
1992
1993 /*
1994 * If the operation was successful, then do
0e8c6656
MS
1995 * the required updates.
1996 */
1997 if (alter)
9ae949fa 1998 do_smart_update(sma, sops, nsops, 1, &wake_q);
0e8c6656
MS
1999 else
2000 set_semotime(sma, sops);
9ae949fa
DB
2001
2002 sem_unlock(sma, locknum);
2003 rcu_read_unlock();
2004 wake_up_q(&wake_q);
2005
2006 goto out_free;
1da177e4 2007 }
9ae949fa 2008 if (error < 0) /* non-blocking error path */
0e8c6656 2009 goto out_unlock_free;
1da177e4 2010
9ae949fa
DB
2011 /*
2012 * We need to sleep on this operation, so we put the current
1da177e4
LT
2013 * task into the pending queue and go to sleep.
2014 */
b97e820f
MS
2015 if (nsops == 1) {
2016 struct sem *curr;
1a233956 2017 curr = &sma->sems[sops->sem_num];
b97e820f 2018
f269f40a
MS
2019 if (alter) {
2020 if (sma->complex_count) {
2021 list_add_tail(&queue.list,
2022 &sma->pending_alter);
2023 } else {
2024
2025 list_add_tail(&queue.list,
2026 &curr->pending_alter);
2027 }
2028 } else {
1a82e9e1 2029 list_add_tail(&queue.list, &curr->pending_const);
f269f40a 2030 }
b97e820f 2031 } else {
f269f40a
MS
2032 if (!sma->complex_count)
2033 merge_queues(sma);
2034
9f1bc2c9 2035 if (alter)
1a82e9e1 2036 list_add_tail(&queue.list, &sma->pending_alter);
9f1bc2c9 2037 else
1a82e9e1
MS
2038 list_add_tail(&queue.list, &sma->pending_const);
2039
b97e820f
MS
2040 sma->complex_count++;
2041 }
2042
b5fa01a2
DB
2043 do {
2044 queue.status = -EINTR;
2045 queue.sleeper = current;
0b0577f6 2046
b5fa01a2
DB
2047 __set_current_state(TASK_INTERRUPTIBLE);
2048 sem_unlock(sma, locknum);
2049 rcu_read_unlock();
1da177e4 2050
b5fa01a2
DB
2051 if (timeout)
2052 jiffies_left = schedule_timeout(jiffies_left);
2053 else
2054 schedule();
1da177e4 2055
9ae949fa 2056 /*
b5fa01a2
DB
2057 * fastpath: the semop has completed, either successfully or
2058 * not, from the syscall pov, is quite irrelevant to us at this
2059 * point; we're done.
2060 *
2061 * We _do_ care, nonetheless, about being awoken by a signal or
2062 * spuriously. The queue.status is checked again in the
2063 * slowpath (aka after taking sem_lock), such that we can detect
2064 * scenarios where we were awakened externally, during the
2065 * window between wake_q_add() and wake_up_q().
c61284e9 2066 */
b5fa01a2
DB
2067 error = READ_ONCE(queue.status);
2068 if (error != -EINTR) {
2069 /*
2070 * User space could assume that semop() is a memory
2071 * barrier: Without the mb(), the cpu could
2072 * speculatively read in userspace stale data that was
2073 * overwritten by the previous owner of the semaphore.
2074 */
2075 smp_mb();
2076 goto out_free;
2077 }
d694ad62 2078
b5fa01a2 2079 rcu_read_lock();
c626bc46 2080 locknum = sem_lock(sma, sops, nsops);
1da177e4 2081
370b262c
DB
2082 if (!ipc_valid_object(&sma->sem_perm))
2083 goto out_unlock_free;
2084
2085 error = READ_ONCE(queue.status);
1da177e4 2086
b5fa01a2
DB
2087 /*
2088 * If queue.status != -EINTR we are woken up by another process.
2089 * Leave without unlink_queue(), but with sem_unlock().
2090 */
2091 if (error != -EINTR)
2092 goto out_unlock_free;
0b0577f6 2093
b5fa01a2
DB
2094 /*
2095 * If an interrupt occurred we have to clean up the queue.
2096 */
2097 if (timeout && jiffies_left == 0)
2098 error = -EAGAIN;
2099 } while (error == -EINTR && !signal_pending(current)); /* spurious */
0b0577f6 2100
b97e820f 2101 unlink_queue(sma, &queue);
1da177e4
LT
2102
2103out_unlock_free:
6062a8dc 2104 sem_unlock(sma, locknum);
6d49dab8 2105 rcu_read_unlock();
1da177e4 2106out_free:
239521f3 2107 if (sops != fast_sops)
e4243b80 2108 kvfree(sops);
1da177e4
LT
2109 return error;
2110}
2111
44ee4546
AV
2112SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
2113 unsigned, nsops, const struct timespec __user *, timeout)
2114{
2115 if (timeout) {
3ef56dc2
DD
2116 struct timespec64 ts;
2117 if (get_timespec64(&ts, timeout))
44ee4546
AV
2118 return -EFAULT;
2119 return do_semtimedop(semid, tsops, nsops, &ts);
2120 }
2121 return do_semtimedop(semid, tsops, nsops, NULL);
2122}
2123
2124#ifdef CONFIG_COMPAT
2125COMPAT_SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsems,
2126 unsigned, nsops,
2127 const struct compat_timespec __user *, timeout)
2128{
2129 if (timeout) {
3ef56dc2
DD
2130 struct timespec64 ts;
2131 if (compat_get_timespec64(&ts, timeout))
44ee4546
AV
2132 return -EFAULT;
2133 return do_semtimedop(semid, tsems, nsops, &ts);
2134 }
2135 return do_semtimedop(semid, tsems, nsops, NULL);
2136}
2137#endif
2138
d5460c99
HC
2139SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2140 unsigned, nsops)
1da177e4 2141{
44ee4546 2142 return do_semtimedop(semid, tsops, nsops, NULL);
1da177e4
LT
2143}
2144
2145/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2146 * parent and child tasks.
1da177e4
LT
2147 */
2148
2149int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2150{
2151 struct sem_undo_list *undo_list;
2152 int error;
2153
2154 if (clone_flags & CLONE_SYSVSEM) {
2155 error = get_undo_list(&undo_list);
2156 if (error)
2157 return error;
f74370b8 2158 refcount_inc(&undo_list->refcnt);
1da177e4 2159 tsk->sysvsem.undo_list = undo_list;
46c0a8ca 2160 } else
1da177e4
LT
2161 tsk->sysvsem.undo_list = NULL;
2162
2163 return 0;
2164}
2165
2166/*
2167 * add semadj values to semaphores, free undo structures.
2168 * undo structures are not freed when semaphore arrays are destroyed
2169 * so some of them may be out of date.
2170 * IMPLEMENTATION NOTE: There is some confusion over whether the
2171 * set of adjustments that needs to be done should be done in an atomic
2172 * manner or not. That is, if we are attempting to decrement the semval
2173 * should we queue up and wait until we can do so legally?
2174 * The original implementation attempted to do this (queue and wait).
2175 * The current implementation does not do so. The POSIX standard
2176 * and SVID should be consulted to determine what behavior is mandated.
2177 */
2178void exit_sem(struct task_struct *tsk)
2179{
4daa28f6 2180 struct sem_undo_list *ulp;
1da177e4 2181
4daa28f6
MS
2182 ulp = tsk->sysvsem.undo_list;
2183 if (!ulp)
1da177e4 2184 return;
9edff4ab 2185 tsk->sysvsem.undo_list = NULL;
1da177e4 2186
f74370b8 2187 if (!refcount_dec_and_test(&ulp->refcnt))
1da177e4
LT
2188 return;
2189
380af1b3 2190 for (;;) {
1da177e4 2191 struct sem_array *sma;
380af1b3 2192 struct sem_undo *un;
6062a8dc 2193 int semid, i;
9ae949fa 2194 DEFINE_WAKE_Q(wake_q);
4daa28f6 2195
2a1613a5
NB
2196 cond_resched();
2197
380af1b3 2198 rcu_read_lock();
05725f7e
JP
2199 un = list_entry_rcu(ulp->list_proc.next,
2200 struct sem_undo, list_proc);
602b8593
HK
2201 if (&un->list_proc == &ulp->list_proc) {
2202 /*
2203 * We must wait for freeary() before freeing this ulp,
2204 * in case we raced with last sem_undo. There is a small
2205 * possibility where we exit while freeary() didn't
2206 * finish unlocking sem_undo_list.
2207 */
e0892e08
PM
2208 spin_lock(&ulp->lock);
2209 spin_unlock(&ulp->lock);
602b8593
HK
2210 rcu_read_unlock();
2211 break;
2212 }
2213 spin_lock(&ulp->lock);
2214 semid = un->semid;
2215 spin_unlock(&ulp->lock);
4daa28f6 2216
602b8593 2217 /* exit_sem raced with IPC_RMID, nothing to do */
6062a8dc
RR
2218 if (semid == -1) {
2219 rcu_read_unlock();
602b8593 2220 continue;
6062a8dc 2221 }
1da177e4 2222
602b8593 2223 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
380af1b3 2224 /* exit_sem raced with IPC_RMID, nothing to do */
6062a8dc
RR
2225 if (IS_ERR(sma)) {
2226 rcu_read_unlock();
380af1b3 2227 continue;
6062a8dc 2228 }
1da177e4 2229
6062a8dc 2230 sem_lock(sma, NULL, -1);
6e224f94 2231 /* exit_sem raced with IPC_RMID, nothing to do */
0f3d2b01 2232 if (!ipc_valid_object(&sma->sem_perm)) {
6e224f94
MS
2233 sem_unlock(sma, -1);
2234 rcu_read_unlock();
2235 continue;
2236 }
bf17bb71 2237 un = __lookup_undo(ulp, semid);
380af1b3
MS
2238 if (un == NULL) {
2239 /* exit_sem raced with IPC_RMID+semget() that created
2240 * exactly the same semid. Nothing to do.
2241 */
6062a8dc 2242 sem_unlock(sma, -1);
6d49dab8 2243 rcu_read_unlock();
380af1b3
MS
2244 continue;
2245 }
2246
2247 /* remove un from the linked lists */
cf9d5d78 2248 ipc_assert_locked_object(&sma->sem_perm);
4daa28f6
MS
2249 list_del(&un->list_id);
2250
a9795584
HK
2251 /* we are the last process using this ulp, acquiring ulp->lock
2252 * isn't required. Besides that, we are also protected against
2253 * IPC_RMID as we hold sma->sem_perm lock now
2254 */
380af1b3 2255 list_del_rcu(&un->list_proc);
380af1b3 2256
4daa28f6
MS
2257 /* perform adjustments registered in un */
2258 for (i = 0; i < sma->sem_nsems; i++) {
1a233956 2259 struct sem *semaphore = &sma->sems[i];
4daa28f6
MS
2260 if (un->semadj[i]) {
2261 semaphore->semval += un->semadj[i];
1da177e4
LT
2262 /*
2263 * Range checks of the new semaphore value,
2264 * not defined by sus:
2265 * - Some unices ignore the undo entirely
2266 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2267 * - some cap the value (e.g. FreeBSD caps
2268 * at 0, but doesn't enforce SEMVMX)
2269 *
2270 * Linux caps the semaphore value, both at 0
2271 * and at SEMVMX.
2272 *
239521f3 2273 * Manfred <manfred@colorfullife.com>
1da177e4 2274 */
5f921ae9
IM
2275 if (semaphore->semval < 0)
2276 semaphore->semval = 0;
2277 if (semaphore->semval > SEMVMX)
2278 semaphore->semval = SEMVMX;
b488893a 2279 semaphore->sempid = task_tgid_vnr(current);
1da177e4
LT
2280 }
2281 }
1da177e4 2282 /* maybe some queued-up processes were waiting for this */
9ae949fa 2283 do_smart_update(sma, NULL, 0, 1, &wake_q);
6062a8dc 2284 sem_unlock(sma, -1);
6d49dab8 2285 rcu_read_unlock();
9ae949fa 2286 wake_up_q(&wake_q);
380af1b3 2287
693a8b6e 2288 kfree_rcu(un, rcu);
1da177e4 2289 }
4daa28f6 2290 kfree(ulp);
1da177e4
LT
2291}
2292
2293#ifdef CONFIG_PROC_FS
19b4946c 2294static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
1da177e4 2295{
1efdb69b 2296 struct user_namespace *user_ns = seq_user_ns(s);
ade9f91b
KC
2297 struct kern_ipc_perm *ipcp = it;
2298 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
e54d02b2 2299 time64_t sem_otime;
d12e1e50 2300
d8c63376
MS
2301 /*
2302 * The proc interface isn't aware of sem_lock(), it calls
2303 * ipc_lock_object() directly (in sysvipc_find_ipc).
5864a2fd
MS
2304 * In order to stay compatible with sem_lock(), we must
2305 * enter / leave complex_mode.
d8c63376 2306 */
5864a2fd 2307 complexmode_enter(sma);
d8c63376 2308
d12e1e50 2309 sem_otime = get_semotime(sma);
19b4946c 2310
7f032d6e 2311 seq_printf(s,
e54d02b2 2312 "%10d %10d %4o %10u %5u %5u %5u %5u %10llu %10llu\n",
7f032d6e
JP
2313 sma->sem_perm.key,
2314 sma->sem_perm.id,
2315 sma->sem_perm.mode,
2316 sma->sem_nsems,
2317 from_kuid_munged(user_ns, sma->sem_perm.uid),
2318 from_kgid_munged(user_ns, sma->sem_perm.gid),
2319 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2320 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2321 sem_otime,
2322 sma->sem_ctime);
2323
5864a2fd
MS
2324 complexmode_tryleave(sma);
2325
7f032d6e 2326 return 0;
1da177e4
LT
2327}
2328#endif