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