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