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