blob: 87bd38f38dc328529ef2f6854a7a3d1e945bd9a0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/ipc/sem.c
4 * Copyright (C) 1992 Krishna Balasubramanian
5 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
8 *
9 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +010010 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Enforced range limit on SEM_UNDO
Alan Cox046c6882009-01-05 14:06:29 +000012 * (c) 2001 Red Hat Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Lockless wakeup
14 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080015 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070016 * Further wakeup optimizations, documentation
17 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040018 *
19 * support for audit of ipc object properties and permission changes
20 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070021 *
22 * namespaces support
23 * OpenVZ, SWsoft Inc.
24 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070025 *
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.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070052 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070053 * - 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
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080058 * dropping all locks. (see wake_up_sem_queue_prepare())
Manfred Spraulc5cf6352010-05-26 14:43:43 -070059 * - 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).
Manfred Spraulc5cf6352010-05-26 14:43:43 -070063 * - 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/slab.h>
74#include <linux/spinlock.h>
75#include <linux/init.h>
76#include <linux/proc_fs.h>
77#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/security.h>
79#include <linux/syscalls.h>
80#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080081#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070082#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070083#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070084#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080085#include <linux/ipc_namespace.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010086#include <linux/sched/wake_q.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080087
Paul McQuade7153e402014-06-06 14:37:37 -070088#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include "util.h"
90
Manfred Spraule57940d2011-11-02 13:38:54 -070091
92/* One queue for each sleeping process in the system. */
93struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -070094 struct list_head list; /* queue of pending operations */
95 struct task_struct *sleeper; /* this process */
96 struct sem_undo *undo; /* undo structure */
97 int pid; /* process id of requesting process */
98 int status; /* completion status of operation */
99 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -0700100 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700101 int nsops; /* number of operations */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800102 bool alter; /* does *sops alter the array? */
103 bool dupsop; /* sops on more than one sem_num */
Manfred Spraule57940d2011-11-02 13:38:54 -0700104};
105
106/* Each task has a list of undo requests. They are executed automatically
107 * when the process exits.
108 */
109struct sem_undo {
110 struct list_head list_proc; /* per-process list: *
111 * all undos from one process
112 * rcu protected */
113 struct rcu_head rcu; /* rcu struct for sem_undo */
114 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
115 struct list_head list_id; /* per semaphore array list:
116 * all undos for one array */
117 int semid; /* semaphore set identifier */
118 short *semadj; /* array of adjustments */
119 /* one per semaphore */
120};
121
122/* sem_undo_list controls shared access to the list of sem_undo structures
123 * that may be shared among all a CLONE_SYSVSEM task group.
124 */
125struct sem_undo_list {
Elena Reshetovaf74370b2017-09-08 16:17:42 -0700126 refcount_t refcnt;
Manfred Spraule57940d2011-11-02 13:38:54 -0700127 spinlock_t lock;
128 struct list_head list_proc;
129};
130
131
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800132#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700134static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800135static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700137static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#endif
139
140#define SEMMSL_FAST 256 /* 512 bytes on stack */
141#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
142
143/*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800144 * Switching from the mode suitable for simple ops
145 * to the mode for complex ops is costly. Therefore:
146 * use some hysteresis
147 */
148#define USE_GLOBAL_LOCK_HYSTERESIS 10
149
150/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700151 * Locking:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700152 * a) global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700154 * sem_array.complex_count,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700155 * sem_array.pending{_alter,_const},
156 * sem_array.sem_undo
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700157 *
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700158 * b) global or semaphore sem_lock() for read/write:
Manfred Spraul1a233952017-07-12 14:34:38 -0700159 * sem_array.sems[i].pending_{const,alter}:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700160 *
161 * c) special:
162 * sem_undo_list.list_proc:
163 * * undo_list->lock for write
164 * * rcu for read
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800165 * use_global_lock:
166 * * global sem_lock() for write
167 * * either local or global sem_lock() for read.
168 *
169 * Memory ordering:
170 * Most ordering is enforced by using spin_lock() and spin_unlock().
171 * The special case is use_global_lock:
172 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
173 * using smp_store_release().
174 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
175 * smp_load_acquire().
176 * Setting it from 0 to non-zero must be ordered with regards to
177 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
178 * is inside a spin_lock() and after a write from 0 to non-zero a
179 * spin_lock()+spin_unlock() is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181
Kirill Korotaeve3893532006-10-02 02:18:22 -0700182#define sc_semmsl sem_ctls[0]
183#define sc_semmns sem_ctls[1]
184#define sc_semopm sem_ctls[2]
185#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700187int sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700188{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700189 ns->sc_semmsl = SEMMSL;
190 ns->sc_semmns = SEMMNS;
191 ns->sc_semopm = SEMOPM;
192 ns->sc_semmni = SEMMNI;
193 ns->used_sems = 0;
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700194 return ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700195}
196
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800197#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700198void sem_exit_ns(struct ipc_namespace *ns)
199{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800200 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800201 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700202 rhashtable_destroy(&ns->ids[IPC_SEM_IDS].key_ht);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700203}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700206int __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700208 const int err = sem_init_ns(&init_ipc_ns);
209
Mike Waychison19b49462005-09-06 15:17:10 -0700210 ipc_init_proc_interface("sysvipc/sem",
211 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700212 IPC_SEM_IDS, sysvipc_sem_proc_show);
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Manfred Spraulf269f402013-07-08 16:01:24 -0700216/**
217 * unmerge_queues - unmerge queues, if possible.
218 * @sma: semaphore array
219 *
220 * The function unmerges the wait queues if complex_count is 0.
221 * It must be called prior to dropping the global semaphore array lock.
222 */
223static void unmerge_queues(struct sem_array *sma)
224{
225 struct sem_queue *q, *tq;
226
227 /* complex operations still around? */
228 if (sma->complex_count)
229 return;
230 /*
231 * We will switch back to simple mode.
232 * Move all pending operation back into the per-semaphore
233 * queues.
234 */
235 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
236 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -0700237 curr = &sma->sems[q->sops[0].sem_num];
Manfred Spraulf269f402013-07-08 16:01:24 -0700238
239 list_add_tail(&q->list, &curr->pending_alter);
240 }
241 INIT_LIST_HEAD(&sma->pending_alter);
242}
243
244/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800245 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700246 * @sma: semaphore array
247 *
248 * This function merges all per-semaphore queues into the global queue.
249 * It is necessary to achieve FIFO ordering for the pending single-sop
250 * operations when a multi-semop operation must sleep.
251 * Only the alter operations must be moved, the const operations can stay.
252 */
253static void merge_queues(struct sem_array *sma)
254{
255 int i;
256 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700257 struct sem *sem = &sma->sems[i];
Manfred Spraulf269f402013-07-08 16:01:24 -0700258
259 list_splice_init(&sem->pending_alter, &sma->pending_alter);
260 }
261}
262
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700263static void sem_rcu_free(struct rcu_head *head)
264{
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700265 struct kern_ipc_perm *p = container_of(head, struct kern_ipc_perm, rcu);
266 struct sem_array *sma = container_of(p, struct sem_array, sem_perm);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700267
268 security_sem_free(sma);
Kees Cooke2029df2017-07-12 14:35:31 -0700269 kvfree(sma);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700270}
271
Nadia Derbey3e148c72007-10-18 23:40:54 -0700272/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700273 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700274 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700275 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700276static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700277{
278 int i;
279 struct sem *sem;
280
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800281 if (sma->use_global_lock > 0) {
282 /*
283 * We are already in global lock mode.
284 * Nothing to do, just reset the
285 * counter until we return to simple mode.
286 */
287 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul6d07b682013-09-30 13:45:06 -0700288 return;
289 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800290 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700291
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700292 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700293 sem = &sma->sems[i];
Manfred Spraul27d7be12017-02-27 14:28:15 -0800294 spin_lock(&sem->lock);
295 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700296 }
297}
298
299/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700300 * Try to leave the mode that disallows simple operations:
301 * Caller must own sem_perm.lock.
302 */
303static void complexmode_tryleave(struct sem_array *sma)
304{
305 if (sma->complex_count) {
306 /* Complex ops are sleeping.
307 * We must stay in complex mode
308 */
309 return;
310 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800311 if (sma->use_global_lock == 1) {
312 /*
313 * Immediately after setting use_global_lock to 0,
314 * a simple op can start. Thus: all memory writes
315 * performed by the current operation must be visible
316 * before we set use_global_lock to 0.
317 */
318 smp_store_release(&sma->use_global_lock, 0);
319 } else {
320 sma->use_global_lock--;
321 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700322}
323
324#define SEM_GLOBAL_LOCK (-1)
325/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700326 * If the request contains only one semaphore operation, and there are
327 * no complex transactions pending, lock only the semaphore involved.
328 * Otherwise, lock the entire semaphore array, since we either have
329 * multiple semaphores in our own semops, or we need to look at
330 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700331 */
332static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
333 int nsops)
334{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700335 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700336
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700337 if (nsops != 1) {
338 /* Complex operation - acquire a full lock */
339 ipc_lock_object(&sma->sem_perm);
340
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700341 /* Prevent parallel simple ops */
342 complexmode_enter(sma);
343 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700344 }
345
346 /*
347 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700348 * Optimized locking is possible if no complex operation
349 * is either enqueued or processed right now.
350 *
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800351 * Both facts are tracked by use_global_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700352 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700353 sem = &sma->sems[sops->sem_num];
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700354
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700355 /*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800356 * Initial check for use_global_lock. Just an optimization,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700357 * no locking, no memory barrier.
358 */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800359 if (!sma->use_global_lock) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700360 /*
361 * It appears that no complex operation is around.
362 * Acquire the per-semaphore lock.
363 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700364 spin_lock(&sem->lock);
365
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800366 /* pairs with smp_store_release() */
367 if (!smp_load_acquire(&sma->use_global_lock)) {
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700368 /* fast path successful! */
369 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700370 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700371 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700372 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700373
374 /* slow path: acquire the full lock */
375 ipc_lock_object(&sma->sem_perm);
376
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800377 if (sma->use_global_lock == 0) {
378 /*
379 * The use_global_lock mode ended while we waited for
380 * sma->sem_perm.lock. Thus we must switch to locking
381 * with sem->lock.
382 * Unlike in the fast path, there is no need to recheck
383 * sma->use_global_lock after we have acquired sem->lock:
384 * We own sma->sem_perm.lock, thus use_global_lock cannot
385 * change.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700386 */
387 spin_lock(&sem->lock);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800388
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700389 ipc_unlock_object(&sma->sem_perm);
390 return sops->sem_num;
391 } else {
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800392 /*
393 * Not a false alarm, thus continue to use the global lock
394 * mode. No need for complexmode_enter(), this was done by
395 * the caller that has set use_global_mode to non-zero.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700396 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700397 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700398 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700399}
400
401static inline void sem_unlock(struct sem_array *sma, int locknum)
402{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700403 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700404 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700405 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700406 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700407 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700408 struct sem *sem = &sma->sems[locknum];
Rik van Riel6062a8d2013-04-30 19:15:44 -0700409 spin_unlock(&sem->lock);
410 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700411}
412
413/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700414 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700415 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700416 *
417 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700418 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700419static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
420{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700421 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700422
423 if (IS_ERR(ipcp))
424 return ERR_CAST(ipcp);
425
426 return container_of(ipcp, struct sem_array, sem_perm);
427}
428
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700429static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
430 int id)
431{
432 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
433
434 if (IS_ERR(ipcp))
435 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800436
Nadia Derbey03f02c72007-10-18 23:40:51 -0700437 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700438}
439
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700440static inline void sem_lock_and_putref(struct sem_array *sma)
441{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700442 sem_lock(sma, NULL, -1);
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700443 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700444}
445
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700446static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
447{
448 ipc_rmid(&sem_ids(ns), &s->sem_perm);
449}
450
Kees Cook101ede02017-07-12 14:35:02 -0700451static struct sem_array *sem_alloc(size_t nsems)
452{
453 struct sem_array *sma;
454 size_t size;
455
456 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
457 return NULL;
458
459 size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
460 sma = kvmalloc(size, GFP_KERNEL);
461 if (unlikely(!sma))
462 return NULL;
463
464 memset(sma, 0, size);
Kees Cook101ede02017-07-12 14:35:02 -0700465
466 return sma;
467}
468
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700469/**
470 * newary - Create a new semaphore set
471 * @ns: namespace
472 * @params: ptr to the structure that contains key, semflg and nsems
473 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700474 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700475 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700476static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int retval;
479 struct sem_array *sma;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700480 key_t key = params->key;
481 int nsems = params->u.nsems;
482 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800483 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (!nsems)
486 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700487 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return -ENOSPC;
489
Kees Cook101ede02017-07-12 14:35:02 -0700490 sma = sem_alloc(nsems);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800491 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 sma->sem_perm.mode = (semflg & S_IRWXUGO);
495 sma->sem_perm.key = key;
496
497 sma->sem_perm.security = NULL;
498 retval = security_sem_alloc(sma);
499 if (retval) {
Kees Cooke2029df2017-07-12 14:35:31 -0700500 kvfree(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return retval;
502 }
503
Rik van Riel6062a8d2013-04-30 19:15:44 -0700504 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700505 INIT_LIST_HEAD(&sma->sems[i].pending_alter);
506 INIT_LIST_HEAD(&sma->sems[i].pending_const);
507 spin_lock_init(&sma->sems[i].lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700508 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800509
510 sma->complex_count = 0;
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800511 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700512 INIT_LIST_HEAD(&sma->pending_alter);
513 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700514 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 sma->sem_nsems = nsems;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -0700516 sma->sem_ctime = ktime_get_real_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800517
Davidlohr Bueso39c96a12017-11-17 15:31:11 -0800518 /* ipc_addid() locks sma upon success. */
Manfred Spraul2ec55f82017-07-12 14:35:13 -0700519 retval = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
520 if (retval < 0) {
521 call_rcu(&sma->sem_perm.rcu, sem_rcu_free);
522 return retval;
Manfred Spraule8577d12014-12-02 15:59:34 -0800523 }
524 ns->used_sems += nsems;
525
Rik van Riel6062a8d2013-04-30 19:15:44 -0700526 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700527 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700529 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700532
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700533/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700534 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700535 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700536static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700538 struct sem_array *sma;
539
540 sma = container_of(ipcp, struct sem_array, sem_perm);
541 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700542}
543
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700544/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700545 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700546 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700547static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
548 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700549{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700550 struct sem_array *sma;
551
552 sma = container_of(ipcp, struct sem_array, sem_perm);
553 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700554 return -EINVAL;
555
556 return 0;
557}
558
Heiko Carstensd5460c92009-01-14 14:14:27 +0100559SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700560{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700561 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700562 static const struct ipc_ops sem_ops = {
563 .getnew = newary,
564 .associate = sem_security,
565 .more_checks = sem_more_checks,
566 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700567 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Kirill Korotaeve3893532006-10-02 02:18:22 -0700569 ns = current->nsproxy->ipc_ns;
570
571 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700573
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700574 sem_params.key = key;
575 sem_params.flg = semflg;
576 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700577
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700578 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Petr Mladek78f50092014-01-27 17:07:00 -0800581/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800582 * perform_atomic_semop[_slow] - Attempt to perform semaphore
583 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700584 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700585 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700586 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800587 * Caller blocking are as follows, based the value
588 * indicated by the semaphore operation (sem_op):
589 *
590 * (1) >0 never blocks.
591 * (2) 0 (wait-for-zero operation): semval is non-zero.
592 * (3) <0 attempting to decrement semval to a value smaller than zero.
593 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700594 * Returns 0 if the operation was possible.
595 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800596 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800598static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700600 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800602 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700603 struct sembuf *sops;
604 struct sem_undo *un;
605
606 sops = q->sops;
607 nsops = q->nsops;
608 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700611 curr = &sma->sems[sop->sem_num];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sem_op = sop->sem_op;
613 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!sem_op && result)
616 goto would_block;
617
618 result += sem_op;
619 if (result < 0)
620 goto would_block;
621 if (result > SEMVMX)
622 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (sop->sem_flg & SEM_UNDO) {
625 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800626 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
628 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800629 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
Petr Mladek78f50092014-01-27 17:07:00 -0800631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 curr->semval = result;
633 }
634
635 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700636 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 while (sop >= sops) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700638 sma->sems[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 sop--;
640 }
Petr Mladek78f50092014-01-27 17:07:00 -0800641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643
644out_of_range:
645 result = -ERANGE;
646 goto undo;
647
648would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700649 q->blocking = sop;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (sop->sem_flg & IPC_NOWAIT)
652 result = -EAGAIN;
653 else
654 result = 1;
655
656undo:
657 sop--;
658 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800659 sem_op = sop->sem_op;
Manfred Spraul1a233952017-07-12 14:34:38 -0700660 sma->sems[sop->sem_num].semval -= sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800661 if (sop->sem_flg & SEM_UNDO)
662 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 sop--;
664 }
665
666 return result;
667}
668
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800669static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
670{
671 int result, sem_op, nsops;
672 struct sembuf *sop;
673 struct sem *curr;
674 struct sembuf *sops;
675 struct sem_undo *un;
676
677 sops = q->sops;
678 nsops = q->nsops;
679 un = q->undo;
680
681 if (unlikely(q->dupsop))
682 return perform_atomic_semop_slow(sma, q);
683
684 /*
685 * We scan the semaphore set twice, first to ensure that the entire
686 * operation can succeed, therefore avoiding any pointless writes
687 * to shared memory and having to undo such changes in order to block
688 * until the operations can go through.
689 */
690 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700691 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800692 sem_op = sop->sem_op;
693 result = curr->semval;
694
695 if (!sem_op && result)
696 goto would_block; /* wait-for-zero */
697
698 result += sem_op;
699 if (result < 0)
700 goto would_block;
701
702 if (result > SEMVMX)
703 return -ERANGE;
704
705 if (sop->sem_flg & SEM_UNDO) {
706 int undo = un->semadj[sop->sem_num] - sem_op;
707
708 /* Exceeding the undo range is an error. */
709 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
710 return -ERANGE;
711 }
712 }
713
714 for (sop = sops; sop < sops + nsops; sop++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700715 curr = &sma->sems[sop->sem_num];
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800716 sem_op = sop->sem_op;
717 result = curr->semval;
718
719 if (sop->sem_flg & SEM_UNDO) {
720 int undo = un->semadj[sop->sem_num] - sem_op;
721
722 un->semadj[sop->sem_num] = undo;
723 }
724 curr->semval += sem_op;
725 curr->sempid = q->pid;
726 }
727
728 return 0;
729
730would_block:
731 q->blocking = sop;
732 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
733}
734
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800735static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
736 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800737{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800738 wake_q_add(wake_q, q->sleeper);
739 /*
740 * Rely on the above implicit barrier, such that we can
741 * ensure that we hold reference to the task before setting
742 * q->status. Otherwise we could race with do_exit if the
743 * task is awoken by an external event before calling
744 * wake_up_process().
745 */
746 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800747}
748
Manfred Spraulb97e8202009-12-15 16:47:32 -0800749static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
750{
751 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700752 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800753 sma->complex_count--;
754}
755
Manfred Spraulfd5db422010-05-26 14:43:40 -0700756/** check_restart(sma, q)
757 * @sma: semaphore array
758 * @q: the operation that just completed
759 *
760 * update_queue is O(N^2) when it restarts scanning the whole queue of
761 * waiting operations. Therefore this function checks if the restart is
762 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700763 * modified the array.
764 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700765 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800766static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700767{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700768 /* pending complex alter operations are too difficult to analyse */
769 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700770 return 1;
771
772 /* we were a sleeping complex operation. Too difficult */
773 if (q->nsops > 1)
774 return 1;
775
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700776 /* It is impossible that someone waits for the new value:
777 * - complex operations always restart.
778 * - wait-for-zero are handled seperately.
779 * - q is a previously sleeping simple operation that
780 * altered the array. It must be a decrement, because
781 * simple increments never sleep.
782 * - If there are older (higher priority) decrements
783 * in the queue, then they have observed the original
784 * semval value and couldn't proceed. The operation
785 * decremented to value - thus they won't proceed either.
786 */
787 return 0;
788}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700789
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700790/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800791 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700792 * @sma: semaphore array.
793 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800794 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700795 *
796 * wake_const_ops must be called after a semaphore in a semaphore array
797 * was set to 0. If complex const operations are pending, wake_const_ops must
798 * be called with semnum = -1, as well as with the number of each modified
799 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800800 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700801 * is stored in q->pid.
802 * The function returns 1 if at least one operation was completed successfully.
803 */
804static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800805 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700806{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800807 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700808 struct list_head *pending_list;
809 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700810
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700811 if (semnum == -1)
812 pending_list = &sma->pending_const;
813 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700814 pending_list = &sma->sems[semnum].pending_const;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700815
Davidlohr Buesof150f022016-12-14 15:06:43 -0800816 list_for_each_entry_safe(q, tmp, pending_list, list) {
817 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700818
Davidlohr Buesof150f022016-12-14 15:06:43 -0800819 if (error > 0)
820 continue;
821 /* operation completed, remove from queue & wakeup */
822 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700823
Davidlohr Buesof150f022016-12-14 15:06:43 -0800824 wake_up_sem_queue_prepare(q, error, wake_q);
825 if (error == 0)
826 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700827 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800828
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700829 return semop_completed;
830}
831
832/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800833 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700834 * @sma: semaphore array
835 * @sops: operations that were performed
836 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800837 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700838 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800839 * Checks all required queue for wait-for-zero operations, based
840 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700841 * The function returns 1 if at least one operation was completed successfully.
842 */
843static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800844 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700845{
846 int i;
847 int semop_completed = 0;
848 int got_zero = 0;
849
850 /* first: the per-semaphore queues, if known */
851 if (sops) {
852 for (i = 0; i < nsops; i++) {
853 int num = sops[i].sem_num;
854
Manfred Spraul1a233952017-07-12 14:34:38 -0700855 if (sma->sems[num].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700856 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800857 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700858 }
859 }
860 } else {
861 /*
862 * No sops means modified semaphores not known.
863 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700864 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700865 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700866 if (sma->sems[i].semval == 0) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700867 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800868 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700869 }
870 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700871 }
872 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700873 * If one of the modified semaphores got 0,
874 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700875 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700876 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800877 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700878
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700879 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700880}
881
Manfred Spraul636c6be2009-12-15 16:47:33 -0800882
883/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800884 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800885 * @sma: semaphore array.
886 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800887 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800888 *
889 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700890 * was modified. If multiple semaphores were modified, update_queue must
891 * be called with semnum = -1, as well as with the number of each modified
892 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800893 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700894 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700895 * The function internally checks if const operations can now succeed.
896 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700897 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800899static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800901 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800902 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700903 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800904
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700905 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700906 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700907 else
Manfred Spraul1a233952017-07-12 14:34:38 -0700908 pending_list = &sma->sems[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Nick Piggin9cad2002009-12-15 16:47:29 -0800910again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800911 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700912 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800913
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800914 /* If we are scanning the single sop, per-semaphore list of
915 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700916 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800917 * that affect only one entry succeed immediately and cannot
918 * be in the per semaphore pending queue, and decrements
919 * cannot be successful if the value is already 0.
920 */
Manfred Spraul1a233952017-07-12 14:34:38 -0700921 if (semnum != -1 && sma->sems[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800922 break;
923
Manfred Sprauld198cd62014-06-06 14:37:49 -0700924 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800927 if (error > 0)
928 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700929
Manfred Spraulb97e8202009-12-15 16:47:32 -0800930 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700931
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700932 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700933 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700934 } else {
935 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800936 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700937 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700938 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700939
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800940 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700941 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800942 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700944 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700947/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800948 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700949 * @sma: semaphore array
950 * @sops: operations that modified the array, may be NULL
951 *
952 * sem_otime is replicated to avoid cache line trashing.
953 * This function sets one instance to the current time.
954 */
955static void set_semotime(struct sem_array *sma, struct sembuf *sops)
956{
957 if (sops == NULL) {
Manfred Spraul1a233952017-07-12 14:34:38 -0700958 sma->sems[0].sem_otime = get_seconds();
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700959 } else {
Manfred Spraul1a233952017-07-12 14:34:38 -0700960 sma->sems[sops[0].sem_num].sem_otime =
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700961 get_seconds();
962 }
963}
964
965/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800966 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700967 * @sma: semaphore array
968 * @sops: operations that were performed
969 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700970 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800971 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700972 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700973 * do_smart_update() does the required calls to update_queue and wakeup_zero,
974 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700975 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800976 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700977 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700978 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700979static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800980 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700981{
982 int i;
983
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800984 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700985
Manfred Spraulf269f402013-07-08 16:01:24 -0700986 if (!list_empty(&sma->pending_alter)) {
987 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800988 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700989 } else {
990 if (!sops) {
991 /*
992 * No sops, thus the modified semaphores are not
993 * known. Check all.
994 */
995 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800996 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700997 } else {
998 /*
999 * Check the semaphores that were increased:
1000 * - No complex ops, thus all sleeping ops are
1001 * decrease.
1002 * - if we decreased the value, then any sleeping
1003 * semaphore ops wont be able to run: If the
1004 * previous value was too small, then the new
1005 * value will be too small, too.
1006 */
1007 for (i = 0; i < nsops; i++) {
1008 if (sops[i].sem_op > 0) {
1009 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001010 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001011 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001012 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001013 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001014 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001015 if (otime)
1016 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001017}
1018
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001019/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001020 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001021 */
1022static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1023 bool count_zero)
1024{
Manfred Spraulb220c572014-06-06 14:37:51 -07001025 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001026
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001027 /*
1028 * Linux always (since 0.99.10) reported a task as sleeping on all
1029 * semaphores. This violates SUS, therefore it was changed to the
1030 * standard compliant behavior.
1031 * Give the administrators a chance to notice that an application
1032 * might misbehave because it relies on the Linux behavior.
1033 */
1034 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1035 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1036 current->comm, task_pid_nr(current));
1037
Manfred Spraulb220c572014-06-06 14:37:51 -07001038 if (sop->sem_num != semnum)
1039 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001040
Manfred Spraulb220c572014-06-06 14:37:51 -07001041 if (count_zero && sop->sem_op == 0)
1042 return 1;
1043 if (!count_zero && sop->sem_op < 0)
1044 return 1;
1045
1046 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001047}
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049/* The following counts are associated to each semaphore:
1050 * semncnt number of tasks waiting on semval being nonzero
1051 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001052 *
1053 * Per definition, a task waits only on the semaphore of the first semop
1054 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001056static int count_semcnt(struct sem_array *sma, ushort semnum,
1057 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001059 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001060 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001061 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001063 semcnt = 0;
1064 /* First: check the simple operations. They are easy to evaluate */
1065 if (count_zero)
Manfred Spraul1a233952017-07-12 14:34:38 -07001066 l = &sma->sems[semnum].pending_const;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001067 else
Manfred Spraul1a233952017-07-12 14:34:38 -07001068 l = &sma->sems[semnum].pending_alter;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001069
1070 list_for_each_entry(q, l, list) {
1071 /* all task on a per-semaphore list sleep on exactly
1072 * that semaphore
1073 */
1074 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001075 }
1076
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001077 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001078 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001079 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001081 if (count_zero) {
1082 list_for_each_entry(q, &sma->pending_const, list) {
1083 semcnt += check_qop(sma, semnum, q, count_zero);
1084 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001085 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001086 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001089/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1090 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001091 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001093static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001095 struct sem_undo *un, *tu;
1096 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001097 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001098 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001099 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Manfred Spraul380af1b2008-07-25 01:48:06 -07001101 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001102 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001103 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1104 list_del(&un->list_id);
1105 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001107 list_del_rcu(&un->list_proc);
1108 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001109 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001113 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1114 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001115 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001116 }
1117
1118 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001119 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001120 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001122 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001123 struct sem *sem = &sma->sems[i];
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001124 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1125 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001126 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001127 }
1128 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001129 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001130 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001131 }
1132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001134 /* Remove the semaphore set from the IDR */
1135 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001136 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001137 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001139 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001140 ns->used_sems -= sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001141 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
1144static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1145{
Manfred Spraul239521f2014-01-27 17:07:04 -08001146 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 case IPC_64:
1148 return copy_to_user(buf, in, sizeof(*in));
1149 case IPC_OLD:
1150 {
1151 struct semid_ds out;
1152
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001153 memset(&out, 0, sizeof(out));
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1156
1157 out.sem_otime = in->sem_otime;
1158 out.sem_ctime = in->sem_ctime;
1159 out.sem_nsems = in->sem_nsems;
1160
1161 return copy_to_user(buf, &out, sizeof(out));
1162 }
1163 default:
1164 return -EINVAL;
1165 }
1166}
1167
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001168static time64_t get_semotime(struct sem_array *sma)
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001169{
1170 int i;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001171 time64_t res;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001172
Manfred Spraul1a233952017-07-12 14:34:38 -07001173 res = sma->sems[0].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001174 for (i = 1; i < sma->sem_nsems; i++) {
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001175 time64_t to = sma->sems[i].sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001176
1177 if (to > res)
1178 res = to;
1179 }
1180 return res;
1181}
1182
Al Viro45a4a642017-07-09 09:11:00 -04001183static int semctl_stat(struct ipc_namespace *ns, int semid,
1184 int cmd, struct semid64_ds *semid64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct sem_array *sma;
Al Viro45a4a642017-07-09 09:11:00 -04001187 int id = 0;
1188 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Al Viro45a4a642017-07-09 09:11:00 -04001190 memset(semid64, 0, sizeof(*semid64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Al Viro45a4a642017-07-09 09:11:00 -04001192 rcu_read_lock();
1193 if (cmd == SEM_STAT) {
1194 sma = sem_obtain_object(ns, semid);
1195 if (IS_ERR(sma)) {
1196 err = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 goto out_unlock;
Al Viro45a4a642017-07-09 09:11:00 -04001198 }
1199 id = sma->sem_perm.id;
1200 } else {
1201 sma = sem_obtain_object_check(ns, semid);
1202 if (IS_ERR(sma)) {
1203 err = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 goto out_unlock;
Al Viro45a4a642017-07-09 09:11:00 -04001205 }
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Al Viro45a4a642017-07-09 09:11:00 -04001208 err = -EACCES;
1209 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
1210 goto out_unlock;
1211
1212 err = security_sem_semctl(sma, cmd);
1213 if (err)
1214 goto out_unlock;
1215
1216 kernel_to_ipc64_perm(&sma->sem_perm, &semid64->sem_perm);
1217 semid64->sem_otime = get_semotime(sma);
1218 semid64->sem_ctime = sma->sem_ctime;
1219 semid64->sem_nsems = sma->sem_nsems;
1220 rcu_read_unlock();
1221 return id;
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001224 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return err;
1226}
1227
Al Viro45a4a642017-07-09 09:11:00 -04001228static int semctl_info(struct ipc_namespace *ns, int semid,
1229 int cmd, void __user *p)
1230{
1231 struct seminfo seminfo;
1232 int max_id;
1233 int err;
1234
1235 err = security_sem_semctl(NULL, cmd);
1236 if (err)
1237 return err;
1238
1239 memset(&seminfo, 0, sizeof(seminfo));
1240 seminfo.semmni = ns->sc_semmni;
1241 seminfo.semmns = ns->sc_semmns;
1242 seminfo.semmsl = ns->sc_semmsl;
1243 seminfo.semopm = ns->sc_semopm;
1244 seminfo.semvmx = SEMVMX;
1245 seminfo.semmnu = SEMMNU;
1246 seminfo.semmap = SEMMAP;
1247 seminfo.semume = SEMUME;
1248 down_read(&sem_ids(ns).rwsem);
1249 if (cmd == SEM_INFO) {
1250 seminfo.semusz = sem_ids(ns).in_use;
1251 seminfo.semaem = ns->used_sems;
1252 } else {
1253 seminfo.semusz = SEMUSZ;
1254 seminfo.semaem = SEMAEM;
1255 }
1256 max_id = ipc_get_maxid(&sem_ids(ns));
1257 up_read(&sem_ids(ns).rwsem);
1258 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
1259 return -EFAULT;
1260 return (max_id < 0) ? 0 : max_id;
1261}
1262
Al Viroe1fd1f42013-03-05 15:04:55 -05001263static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
Al Viro45a4a642017-07-09 09:11:00 -04001264 int val)
Al Viroe1fd1f42013-03-05 15:04:55 -05001265{
1266 struct sem_undo *un;
1267 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001268 struct sem *curr;
Al Viro45a4a642017-07-09 09:11:00 -04001269 int err;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001270 DEFINE_WAKE_Q(wake_q);
1271
Rik van Riel6062a8d2013-04-30 19:15:44 -07001272 if (val > SEMVMX || val < 0)
1273 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001274
Rik van Riel6062a8d2013-04-30 19:15:44 -07001275 rcu_read_lock();
1276 sma = sem_obtain_object_check(ns, semid);
1277 if (IS_ERR(sma)) {
1278 rcu_read_unlock();
1279 return PTR_ERR(sma);
1280 }
1281
1282 if (semnum < 0 || semnum >= sma->sem_nsems) {
1283 rcu_read_unlock();
1284 return -EINVAL;
1285 }
1286
1287
1288 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1289 rcu_read_unlock();
1290 return -EACCES;
1291 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001292
1293 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001294 if (err) {
1295 rcu_read_unlock();
1296 return -EACCES;
1297 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001298
Rik van Riel6062a8d2013-04-30 19:15:44 -07001299 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001300
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001301 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001302 sem_unlock(sma, -1);
1303 rcu_read_unlock();
1304 return -EIDRM;
1305 }
1306
Manfred Spraul1a233952017-07-12 14:34:38 -07001307 curr = &sma->sems[semnum];
Al Viroe1fd1f42013-03-05 15:04:55 -05001308
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001309 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001310 list_for_each_entry(un, &sma->list_id, list_id)
1311 un->semadj[semnum] = 0;
1312
1313 curr->semval = val;
1314 curr->sempid = task_tgid_vnr(current);
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001315 sma->sem_ctime = ktime_get_real_seconds();
Al Viroe1fd1f42013-03-05 15:04:55 -05001316 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001317 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001318 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001319 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001320 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001321 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001322}
1323
Kirill Korotaeve3893532006-10-02 02:18:22 -07001324static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001325 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001328 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001329 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001331 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001332 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001333
1334 rcu_read_lock();
1335 sma = sem_obtain_object_check(ns, semid);
1336 if (IS_ERR(sma)) {
1337 rcu_read_unlock();
1338 return PTR_ERR(sma);
1339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 nsems = sma->sem_nsems;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001344 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1345 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001348 if (err)
1349 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 err = -EACCES;
1352 switch (cmd) {
1353 case GETALL:
1354 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001355 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 int i;
1357
Al Viroce857222013-05-03 00:30:49 +01001358 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001359 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001360 err = -EIDRM;
1361 goto out_unlock;
1362 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001363 if (nsems > SEMMSL_FAST) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001364 if (!ipc_rcu_getref(&sma->sem_perm)) {
Al Viroce857222013-05-03 00:30:49 +01001365 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001366 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001367 }
1368 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001369 rcu_read_unlock();
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001370 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1371 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001372 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001373 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return -ENOMEM;
1375 }
1376
Linus Torvalds4091fd92013-05-04 10:13:40 -07001377 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001378 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001379 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001381 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Al Viroce857222013-05-03 00:30:49 +01001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 for (i = 0; i < sma->sem_nsems; i++)
Manfred Spraul1a233952017-07-12 14:34:38 -07001385 sem_io[i] = sma->sems[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001386 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001387 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001389 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 err = -EFAULT;
1391 goto out_free;
1392 }
1393 case SETALL:
1394 {
1395 int i;
1396 struct sem_undo *un;
1397
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001398 if (!ipc_rcu_getref(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001399 err = -EIDRM;
1400 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001401 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001402 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Manfred Spraul239521f2014-01-27 17:07:04 -08001404 if (nsems > SEMMSL_FAST) {
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001405 sem_io = kvmalloc_array(nsems, sizeof(ushort),
1406 GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001407 if (sem_io == NULL) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001408 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -ENOMEM;
1410 }
1411 }
1412
Manfred Spraul239521f2014-01-27 17:07:04 -08001413 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001414 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 err = -EFAULT;
1416 goto out_free;
1417 }
1418
1419 for (i = 0; i < nsems; i++) {
1420 if (sem_io[i] > SEMVMX) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001421 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 err = -ERANGE;
1423 goto out_free;
1424 }
1425 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001426 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001427 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001428 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001430 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001433 for (i = 0; i < nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07001434 sma->sems[i].semval = sem_io[i];
1435 sma->sems[i].sempid = task_tgid_vnr(current);
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001436 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001437
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001438 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001439 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 for (i = 0; i < nsems; i++)
1441 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001442 }
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001443 sma->sem_ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001445 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 err = 0;
1447 goto out_unlock;
1448 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001449 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001452 if (semnum < 0 || semnum >= nsems)
1453 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Rik van Riel6062a8d2013-04-30 19:15:44 -07001455 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001456 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001457 err = -EIDRM;
1458 goto out_unlock;
1459 }
Manfred Spraul1a233952017-07-12 14:34:38 -07001460 curr = &sma->sems[semnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 switch (cmd) {
1463 case GETVAL:
1464 err = curr->semval;
1465 goto out_unlock;
1466 case GETPID:
1467 err = curr->sempid;
1468 goto out_unlock;
1469 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001470 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out_unlock;
1472 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001473 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001478 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001479out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001480 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001481 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001483 if (sem_io != fast_sem_io)
Kees Cookf8dbe8d2017-07-12 14:34:47 -07001484 kvfree(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return err;
1486}
1487
Pierre Peiffer016d7132008-04-29 01:00:50 -07001488static inline unsigned long
1489copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Manfred Spraul239521f2014-01-27 17:07:04 -08001491 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001493 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 case IPC_OLD:
1497 {
1498 struct semid_ds tbuf_old;
1499
Manfred Spraul239521f2014-01-27 17:07:04 -08001500 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EFAULT;
1502
Pierre Peiffer016d7132008-04-29 01:00:50 -07001503 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1504 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1505 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 return 0;
1508 }
1509 default:
1510 return -EINVAL;
1511 }
1512}
1513
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001514/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001515 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001516 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001517 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001518 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001519static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viro45a4a642017-07-09 09:11:00 -04001520 int cmd, struct semid64_ds *semid64)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 struct sem_array *sma;
1523 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 struct kern_ipc_perm *ipcp;
1525
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001526 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001527 rcu_read_lock();
1528
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001529 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
Al Viro45a4a642017-07-09 09:11:00 -04001530 &semid64->sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001531 if (IS_ERR(ipcp)) {
1532 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001533 goto out_unlock1;
1534 }
Steve Grubb073115d2006-04-02 17:07:33 -04001535
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001536 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001539 if (err)
1540 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001542 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001544 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001545 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001546 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001547 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001549 sem_lock(sma, NULL, -1);
Al Viro45a4a642017-07-09 09:11:00 -04001550 err = ipc_update_perm(&semid64->sem_perm, ipcp);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001551 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001552 goto out_unlock0;
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07001553 sma->sem_ctime = ktime_get_real_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001557 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001560out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001561 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001562out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001563 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001564out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001565 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return err;
1567}
1568
Al Viroe1fd1f42013-03-05 15:04:55 -05001569SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001572 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001573 void __user *p = (void __user *)arg;
Al Viro45a4a642017-07-09 09:11:00 -04001574 struct semid64_ds semid64;
1575 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if (semid < 0)
1578 return -EINVAL;
1579
1580 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001581 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Manfred Spraul239521f2014-01-27 17:07:04 -08001583 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 case IPC_INFO:
1585 case SEM_INFO:
Al Viro45a4a642017-07-09 09:11:00 -04001586 return semctl_info(ns, semid, cmd, p);
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001587 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case SEM_STAT:
Al Viro45a4a642017-07-09 09:11:00 -04001589 err = semctl_stat(ns, semid, cmd, &semid64);
1590 if (err < 0)
1591 return err;
1592 if (copy_semid_to_user(p, &semid64, version))
1593 err = -EFAULT;
1594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case GETALL:
1596 case GETVAL:
1597 case GETPID:
1598 case GETNCNT:
1599 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001601 return semctl_main(ns, semid, semnum, cmd, p);
Al Viro45a4a642017-07-09 09:11:00 -04001602 case SETVAL: {
1603 int val;
1604#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1605 /* big-endian 64bit */
1606 val = arg >> 32;
1607#else
1608 /* 32bit or little-endian 64bit */
1609 val = arg;
1610#endif
1611 return semctl_setval(ns, semid, semnum, val);
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case IPC_SET:
Al Viro45a4a642017-07-09 09:11:00 -04001614 if (copy_semid_from_user(&semid64, p, version))
1615 return -EFAULT;
1616 case IPC_RMID:
1617 return semctl_down(ns, semid, cmd, &semid64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 default:
1619 return -EINVAL;
1620 }
1621}
1622
Al Viroc0ebccb2017-07-09 10:03:23 -04001623#ifdef CONFIG_COMPAT
1624
1625struct compat_semid_ds {
1626 struct compat_ipc_perm sem_perm;
1627 compat_time_t sem_otime;
1628 compat_time_t sem_ctime;
1629 compat_uptr_t sem_base;
1630 compat_uptr_t sem_pending;
1631 compat_uptr_t sem_pending_last;
1632 compat_uptr_t undo;
1633 unsigned short sem_nsems;
1634};
1635
1636static int copy_compat_semid_from_user(struct semid64_ds *out, void __user *buf,
1637 int version)
1638{
1639 memset(out, 0, sizeof(*out));
1640 if (version == IPC_64) {
Linus Torvalds6aa211e2017-09-25 18:37:28 -07001641 struct compat_semid64_ds __user *p = buf;
Al Viroc0ebccb2017-07-09 10:03:23 -04001642 return get_compat_ipc64_perm(&out->sem_perm, &p->sem_perm);
1643 } else {
Linus Torvalds6aa211e2017-09-25 18:37:28 -07001644 struct compat_semid_ds __user *p = buf;
Al Viroc0ebccb2017-07-09 10:03:23 -04001645 return get_compat_ipc_perm(&out->sem_perm, &p->sem_perm);
1646 }
1647}
1648
1649static int copy_compat_semid_to_user(void __user *buf, struct semid64_ds *in,
1650 int version)
1651{
1652 if (version == IPC_64) {
1653 struct compat_semid64_ds v;
1654 memset(&v, 0, sizeof(v));
1655 to_compat_ipc64_perm(&v.sem_perm, &in->sem_perm);
1656 v.sem_otime = in->sem_otime;
1657 v.sem_ctime = in->sem_ctime;
1658 v.sem_nsems = in->sem_nsems;
1659 return copy_to_user(buf, &v, sizeof(v));
1660 } else {
1661 struct compat_semid_ds v;
1662 memset(&v, 0, sizeof(v));
1663 to_compat_ipc_perm(&v.sem_perm, &in->sem_perm);
1664 v.sem_otime = in->sem_otime;
1665 v.sem_ctime = in->sem_ctime;
1666 v.sem_nsems = in->sem_nsems;
1667 return copy_to_user(buf, &v, sizeof(v));
1668 }
1669}
1670
1671COMPAT_SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, int, arg)
1672{
1673 void __user *p = compat_ptr(arg);
1674 struct ipc_namespace *ns;
1675 struct semid64_ds semid64;
1676 int version = compat_ipc_parse_version(&cmd);
1677 int err;
1678
1679 ns = current->nsproxy->ipc_ns;
1680
1681 if (semid < 0)
1682 return -EINVAL;
1683
1684 switch (cmd & (~IPC_64)) {
1685 case IPC_INFO:
1686 case SEM_INFO:
1687 return semctl_info(ns, semid, cmd, p);
1688 case IPC_STAT:
1689 case SEM_STAT:
1690 err = semctl_stat(ns, semid, cmd, &semid64);
1691 if (err < 0)
1692 return err;
1693 if (copy_compat_semid_to_user(p, &semid64, version))
1694 err = -EFAULT;
1695 return err;
1696 case GETVAL:
1697 case GETPID:
1698 case GETNCNT:
1699 case GETZCNT:
1700 case GETALL:
1701 case SETALL:
1702 return semctl_main(ns, semid, semnum, cmd, p);
1703 case SETVAL:
1704 return semctl_setval(ns, semid, semnum, arg);
1705 case IPC_SET:
1706 if (copy_compat_semid_from_user(&semid64, p, version))
1707 return -EFAULT;
1708 /* fallthru */
1709 case IPC_RMID:
1710 return semctl_down(ns, semid, cmd, &semid64);
1711 default:
1712 return -EINVAL;
1713 }
1714}
1715#endif
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/* If the task doesn't already have a undo_list, then allocate one
1718 * here. We guarantee there is only one thread using this undo list,
1719 * and current is THE ONE
1720 *
1721 * If this allocation and assignment succeeds, but later
1722 * portions of this code fail, there is no need to free the sem_undo_list.
1723 * Just let it stay associated with the task, and it'll be freed later
1724 * at exit time.
1725 *
1726 * This can block, so callers must hold no locks.
1727 */
1728static inline int get_undo_list(struct sem_undo_list **undo_listp)
1729{
1730 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 undo_list = current->sysvsem.undo_list;
1733 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001734 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (undo_list == NULL)
1736 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001737 spin_lock_init(&undo_list->lock);
Elena Reshetovaf74370b2017-09-08 16:17:42 -07001738 refcount_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001739 INIT_LIST_HEAD(&undo_list->list_proc);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 current->sysvsem.undo_list = undo_list;
1742 }
1743 *undo_listp = undo_list;
1744 return 0;
1745}
1746
Nick Pigginbf17bb72009-12-15 16:47:28 -08001747static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001749 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Nick Pigginbf17bb72009-12-15 16:47:28 -08001751 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1752 if (un->semid == semid)
1753 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001755 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Nick Pigginbf17bb72009-12-15 16:47:28 -08001758static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1759{
1760 struct sem_undo *un;
1761
Manfred Spraul239521f2014-01-27 17:07:04 -08001762 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001763
1764 un = __lookup_undo(ulp, semid);
1765 if (un) {
1766 list_del_rcu(&un->list_proc);
1767 list_add_rcu(&un->list_proc, &ulp->list_proc);
1768 }
1769 return un;
1770}
1771
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001772/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001773 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001774 * @ns: namespace
1775 * @semid: semaphore array id
1776 *
1777 * The function looks up (and if not present creates) the undo structure.
1778 * The size of the undo structure depends on the size of the semaphore
1779 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001780 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1781 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001782 */
1783static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct sem_array *sma;
1786 struct sem_undo_list *ulp;
1787 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001788 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 error = get_undo_list(&ulp);
1791 if (error)
1792 return ERR_PTR(error);
1793
Manfred Spraul380af1b2008-07-25 01:48:06 -07001794 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001795 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001797 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001798 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto out;
1800
1801 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001802 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001803 sma = sem_obtain_object_check(ns, semid);
1804 if (IS_ERR(sma)) {
1805 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001806 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001807 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 nsems = sma->sem_nsems;
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001810 if (!ipc_rcu_getref(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001811 rcu_read_unlock();
1812 un = ERR_PTR(-EIDRM);
1813 goto out;
1814 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001815 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001817 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001818 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (!new) {
Manfred Sprauldba4cdd2017-07-12 14:34:41 -07001820 ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return ERR_PTR(-ENOMEM);
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Manfred Spraul380af1b2008-07-25 01:48:06 -07001824 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001825 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001826 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001827 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001828 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001829 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 kfree(new);
1831 un = ERR_PTR(-EIDRM);
1832 goto out;
1833 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001834 spin_lock(&ulp->lock);
1835
1836 /*
1837 * step 4: check for races: did someone else allocate the undo struct?
1838 */
1839 un = lookup_undo(ulp, semid);
1840 if (un) {
1841 kfree(new);
1842 goto success;
1843 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001844 /* step 5: initialize & link new undo structure */
1845 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001846 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001847 new->semid = semid;
1848 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001849 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001850 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001851 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001852 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001853
1854success:
1855 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001856 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857out:
1858 return un;
1859}
1860
Al Viro44ee4542017-07-09 10:50:14 -04001861static long do_semtimedop(int semid, struct sembuf __user *tsops,
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07001862 unsigned nsops, const struct timespec64 *timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int error = -EINVAL;
1865 struct sem_array *sma;
1866 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001867 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001869 int max, locknum;
1870 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001872 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001873 struct ipc_namespace *ns;
1874
1875 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 if (nsops < 1 || semid < 0)
1878 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001879 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001881 if (nsops > SEMOPM_FAST) {
Davidlohr Buesoe4243b82017-09-08 16:17:52 -07001882 sops = kvmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
Manfred Spraul239521f2014-01-27 17:07:04 -08001883 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 return -ENOMEM;
1885 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001886
Manfred Spraul239521f2014-01-27 17:07:04 -08001887 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1888 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto out_free;
1890 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (timeout) {
Al Viro44ee4542017-07-09 10:50:14 -04001893 if (timeout->tv_sec < 0 || timeout->tv_nsec < 0 ||
1894 timeout->tv_nsec >= 1000000000L) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 error = -EINVAL;
1896 goto out_free;
1897 }
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07001898 jiffies_left = timespec64_to_jiffies(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 max = 0;
1902 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001903 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (sop->sem_num >= max)
1906 max = sop->sem_num;
1907 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001908 undos = true;
1909 if (dup & mask) {
1910 /*
1911 * There was a previous alter access that appears
1912 * to have accessed the same semaphore, thus use
1913 * the dupsop logic. "appears", because the detection
1914 * can only check % BITS_PER_LONG.
1915 */
1916 dupsop = true;
1917 }
1918 if (sop->sem_op != 0) {
1919 alter = true;
1920 dup |= mask;
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001925 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001926 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (IS_ERR(un)) {
1928 error = PTR_ERR(un);
1929 goto out_free;
1930 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001933 rcu_read_lock();
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001936 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001937 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001938 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001939 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001941 }
1942
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001943 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001944 if (max >= sma->sem_nsems) {
1945 rcu_read_unlock();
1946 goto out_free;
1947 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001948
1949 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001950 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1951 rcu_read_unlock();
1952 goto out_free;
1953 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001954
1955 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001956 if (error) {
1957 rcu_read_unlock();
1958 goto out_free;
1959 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001960
Manfred Spraul6e224f92013-10-16 13:46:45 -07001961 error = -EIDRM;
1962 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001963 /*
1964 * We eventually might perform the following check in a lockless
1965 * fashion, considering ipc_valid_object() locking constraints.
1966 * If nsops == 1 and there is no contention for sem_perm.lock, then
1967 * only a per-semaphore lock is held and it's OK to proceed with the
1968 * check below. More details on the fine grained locking scheme
1969 * entangled here and why it's RMID race safe on comments at sem_lock()
1970 */
1971 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001972 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001974 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001976 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001977 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001978 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001980 if (un && un->semid == -1)
1981 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001982
Manfred Sprauld198cd62014-06-06 14:37:49 -07001983 queue.sops = sops;
1984 queue.nsops = nsops;
1985 queue.undo = un;
1986 queue.pid = task_tgid_vnr(current);
1987 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001988 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001989
1990 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001991 if (error == 0) { /* non-blocking succesfull path */
1992 DEFINE_WAKE_Q(wake_q);
1993
1994 /*
1995 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001996 * the required updates.
1997 */
1998 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001999 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07002000 else
2001 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002002
2003 sem_unlock(sma, locknum);
2004 rcu_read_unlock();
2005 wake_up_q(&wake_q);
2006
2007 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002009 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07002010 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002012 /*
2013 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 * task into the pending queue and go to sleep.
2015 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08002016 if (nsops == 1) {
2017 struct sem *curr;
Manfred Spraul1a233952017-07-12 14:34:38 -07002018 curr = &sma->sems[sops->sem_num];
Manfred Spraulb97e8202009-12-15 16:47:32 -08002019
Manfred Spraulf269f402013-07-08 16:01:24 -07002020 if (alter) {
2021 if (sma->complex_count) {
2022 list_add_tail(&queue.list,
2023 &sma->pending_alter);
2024 } else {
2025
2026 list_add_tail(&queue.list,
2027 &curr->pending_alter);
2028 }
2029 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002030 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07002031 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08002032 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07002033 if (!sma->complex_count)
2034 merge_queues(sma);
2035
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07002036 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002037 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07002038 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07002039 list_add_tail(&queue.list, &sma->pending_const);
2040
Manfred Spraulb97e8202009-12-15 16:47:32 -08002041 sma->complex_count++;
2042 }
2043
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002044 do {
2045 queue.status = -EINTR;
2046 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002047
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002048 __set_current_state(TASK_INTERRUPTIBLE);
2049 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07002050 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002052 if (timeout)
2053 jiffies_left = schedule_timeout(jiffies_left);
2054 else
2055 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002057 /*
2058 * fastpath: the semop has completed, either successfully or
2059 * not, from the syscall pov, is quite irrelevant to us at this
2060 * point; we're done.
2061 *
2062 * We _do_ care, nonetheless, about being awoken by a signal or
2063 * spuriously. The queue.status is checked again in the
2064 * slowpath (aka after taking sem_lock), such that we can detect
2065 * scenarios where we were awakened externally, during the
2066 * window between wake_q_add() and wake_up_q().
2067 */
2068 error = READ_ONCE(queue.status);
2069 if (error != -EINTR) {
2070 /*
2071 * User space could assume that semop() is a memory
2072 * barrier: Without the mb(), the cpu could
2073 * speculatively read in userspace stale data that was
2074 * overwritten by the previous owner of the semaphore.
2075 */
2076 smp_mb();
2077 goto out_free;
2078 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002079
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002080 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08002081 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002082
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002083 if (!ipc_valid_object(&sma->sem_perm))
2084 goto out_unlock_free;
2085
2086 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002087
2088 /*
2089 * If queue.status != -EINTR we are woken up by another process.
2090 * Leave without unlink_queue(), but with sem_unlock().
2091 */
2092 if (error != -EINTR)
2093 goto out_unlock_free;
2094
2095 /*
2096 * If an interrupt occurred we have to clean up the queue.
2097 */
2098 if (timeout && jiffies_left == 0)
2099 error = -EAGAIN;
2100 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002101
Manfred Spraulb97e8202009-12-15 16:47:32 -08002102 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002105 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002106 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002108 if (sops != fast_sops)
Davidlohr Buesoe4243b82017-09-08 16:17:52 -07002109 kvfree(sops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return error;
2111}
2112
Al Viro44ee4542017-07-09 10:50:14 -04002113SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
2114 unsigned, nsops, const struct timespec __user *, timeout)
2115{
2116 if (timeout) {
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07002117 struct timespec64 ts;
2118 if (get_timespec64(&ts, timeout))
Al Viro44ee4542017-07-09 10:50:14 -04002119 return -EFAULT;
2120 return do_semtimedop(semid, tsops, nsops, &ts);
2121 }
2122 return do_semtimedop(semid, tsops, nsops, NULL);
2123}
2124
2125#ifdef CONFIG_COMPAT
2126COMPAT_SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsems,
2127 unsigned, nsops,
2128 const struct compat_timespec __user *, timeout)
2129{
2130 if (timeout) {
Deepa Dinamani3ef56dc2017-08-02 19:51:10 -07002131 struct timespec64 ts;
2132 if (compat_get_timespec64(&ts, timeout))
Al Viro44ee4542017-07-09 10:50:14 -04002133 return -EFAULT;
2134 return do_semtimedop(semid, tsems, nsops, &ts);
2135 }
2136 return do_semtimedop(semid, tsems, nsops, NULL);
2137}
2138#endif
2139
Heiko Carstensd5460c92009-01-14 14:14:27 +01002140SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2141 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Al Viro44ee4542017-07-09 10:50:14 -04002143 return do_semtimedop(semid, tsops, nsops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2147 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
2149
2150int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2151{
2152 struct sem_undo_list *undo_list;
2153 int error;
2154
2155 if (clone_flags & CLONE_SYSVSEM) {
2156 error = get_undo_list(&undo_list);
2157 if (error)
2158 return error;
Elena Reshetovaf74370b2017-09-08 16:17:42 -07002159 refcount_inc(&undo_list->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002161 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 tsk->sysvsem.undo_list = NULL;
2163
2164 return 0;
2165}
2166
2167/*
2168 * add semadj values to semaphores, free undo structures.
2169 * undo structures are not freed when semaphore arrays are destroyed
2170 * so some of them may be out of date.
2171 * IMPLEMENTATION NOTE: There is some confusion over whether the
2172 * set of adjustments that needs to be done should be done in an atomic
2173 * manner or not. That is, if we are attempting to decrement the semval
2174 * should we queue up and wait until we can do so legally?
2175 * The original implementation attempted to do this (queue and wait).
2176 * The current implementation does not do so. The POSIX standard
2177 * and SVID should be consulted to determine what behavior is mandated.
2178 */
2179void exit_sem(struct task_struct *tsk)
2180{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002181 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002183 ulp = tsk->sysvsem.undo_list;
2184 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002186 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Elena Reshetovaf74370b2017-09-08 16:17:42 -07002188 if (!refcount_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return;
2190
Manfred Spraul380af1b2008-07-25 01:48:06 -07002191 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002193 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002194 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002195 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002197 cond_resched();
2198
Manfred Spraul380af1b2008-07-25 01:48:06 -07002199 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002200 un = list_entry_rcu(ulp->list_proc.next,
2201 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002202 if (&un->list_proc == &ulp->list_proc) {
2203 /*
2204 * We must wait for freeary() before freeing this ulp,
2205 * in case we raced with last sem_undo. There is a small
2206 * possibility where we exit while freeary() didn't
2207 * finish unlocking sem_undo_list.
2208 */
Paul E. McKenneye0892e02017-06-29 12:59:32 -07002209 spin_lock(&ulp->lock);
2210 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002211 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002212 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002213 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002214 spin_lock(&ulp->lock);
2215 semid = un->semid;
2216 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002217
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002218 /* exit_sem raced with IPC_RMID, nothing to do */
2219 if (semid == -1) {
2220 rcu_read_unlock();
2221 continue;
2222 }
2223
2224 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002225 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002226 if (IS_ERR(sma)) {
2227 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002228 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Rik van Riel6062a8d2013-04-30 19:15:44 -07002231 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002232 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002233 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002234 sem_unlock(sma, -1);
2235 rcu_read_unlock();
2236 continue;
2237 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002238 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002239 if (un == NULL) {
2240 /* exit_sem raced with IPC_RMID+semget() that created
2241 * exactly the same semid. Nothing to do.
2242 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002243 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002244 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002245 continue;
2246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Manfred Spraul380af1b2008-07-25 01:48:06 -07002248 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002249 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002250 list_del(&un->list_id);
2251
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002252 /* we are the last process using this ulp, acquiring ulp->lock
2253 * isn't required. Besides that, we are also protected against
2254 * IPC_RMID as we hold sma->sem_perm lock now
2255 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002256 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002257
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002258 /* perform adjustments registered in un */
2259 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul1a233952017-07-12 14:34:38 -07002260 struct sem *semaphore = &sma->sems[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002261 if (un->semadj[i]) {
2262 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /*
2264 * Range checks of the new semaphore value,
2265 * not defined by sus:
2266 * - Some unices ignore the undo entirely
2267 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2268 * - some cap the value (e.g. FreeBSD caps
2269 * at 0, but doesn't enforce SEMVMX)
2270 *
2271 * Linux caps the semaphore value, both at 0
2272 * and at SEMVMX.
2273 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002274 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002276 if (semaphore->semval < 0)
2277 semaphore->semval = 0;
2278 if (semaphore->semval > SEMVMX)
2279 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002280 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002284 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002285 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002286 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002287 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002288
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002289 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002291 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002295static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002297 struct user_namespace *user_ns = seq_user_ns(s);
Kees Cookade9f912017-08-02 13:32:21 -07002298 struct kern_ipc_perm *ipcp = it;
2299 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07002300 time64_t sem_otime;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002301
Manfred Sprauld8c63372013-09-30 13:45:07 -07002302 /*
2303 * The proc interface isn't aware of sem_lock(), it calls
2304 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002305 * In order to stay compatible with sem_lock(), we must
2306 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002307 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002308 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002309
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002310 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Joe Perches7f032d62015-04-15 16:17:54 -07002312 seq_printf(s,
Deepa Dinamanie54d02b2017-08-02 19:51:13 -07002313 "%10d %10d %4o %10u %5u %5u %5u %5u %10llu %10llu\n",
Joe Perches7f032d62015-04-15 16:17:54 -07002314 sma->sem_perm.key,
2315 sma->sem_perm.id,
2316 sma->sem_perm.mode,
2317 sma->sem_nsems,
2318 from_kuid_munged(user_ns, sma->sem_perm.uid),
2319 from_kgid_munged(user_ns, sma->sem_perm.gid),
2320 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2321 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2322 sem_otime,
2323 sma->sem_ctime);
2324
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002325 complexmode_tryleave(sma);
2326
Joe Perches7f032d62015-04-15 16:17:54 -07002327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329#endif