blob: 947dc2348271f9b8b373e098932c1e4e351806de [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
5 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
7 *
8 * SMP-threaded, sysctl's added
Christian Kujau624dffc2006-01-15 02:43:54 +01009 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Enforced range limit on SEM_UNDO
Alan Cox046c6882009-01-05 14:06:29 +000011 * (c) 2001 Red Hat Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Lockless wakeup
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080014 * (c) 2016 Davidlohr Bueso <dave@stgolabs.net>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070015 * Further wakeup optimizations, documentation
16 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040017 *
18 * support for audit of ipc object properties and permission changes
19 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070020 *
21 * namespaces support
22 * OpenVZ, SWsoft Inc.
23 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070024 *
25 * Implementation notes: (May 2010)
26 * This file implements System V semaphores.
27 *
28 * User space visible behavior:
29 * - FIFO ordering for semop() operations (just FIFO, not starvation
30 * protection)
31 * - multiple semaphore operations that alter the same semaphore in
32 * one semop() are handled.
33 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
34 * SETALL calls.
35 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
36 * - undo adjustments at process exit are limited to 0..SEMVMX.
37 * - namespace are supported.
38 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
39 * to /proc/sys/kernel/sem.
40 * - statistics about the usage are reported in /proc/sysvipc/sem.
41 *
42 * Internals:
43 * - scalability:
44 * - all global variables are read-mostly.
45 * - semop() calls and semctl(RMID) are synchronized by RCU.
46 * - most operations do write operations (actually: spin_lock calls) to
47 * the per-semaphore array structure.
48 * Thus: Perfect SMP scaling between independent semaphore arrays.
49 * If multiple semaphores in one array are used, then cache line
50 * trashing on the semaphore array spinlock will limit the scaling.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070051 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070052 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -080057 * dropping all locks. (see wake_up_sem_queue_prepare())
Manfred Spraulc5cf6352010-05-26 14:43:43 -070058 * - All work is done by the waker, the woken up task does not have to do
59 * anything - not even acquiring a lock or dropping a refcount.
60 * - A woken up task may not even touch the semaphore array anymore, it may
61 * have been destroyed already by a semctl(RMID).
Manfred Spraulc5cf6352010-05-26 14:43:43 -070062 * - UNDO values are stored in an array (one per process and per
63 * semaphore array, lazily allocated). For backwards compatibility, multiple
64 * modes for the UNDO variables are supported (per process, per thread)
65 * (see copy_semundo, CLONE_SYSVSEM)
66 * - There are two lists of the pending operations: a per-array list
67 * and per-semaphore list (stored in the array). This allows to achieve FIFO
68 * ordering without always scanning all pending operations.
69 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 */
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/slab.h>
73#include <linux/spinlock.h>
74#include <linux/init.h>
75#include <linux/proc_fs.h>
76#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/security.h>
78#include <linux/syscalls.h>
79#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080080#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070081#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070082#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070083#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080084#include <linux/ipc_namespace.h>
Ingo Molnar84f001e2017-02-01 16:36:40 +010085#include <linux/sched/wake_q.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080086
Paul McQuade7153e402014-06-06 14:37:37 -070087#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include "util.h"
89
Manfred Spraule57940d2011-11-02 13:38:54 -070090/* One semaphore structure for each semaphore in the system. */
91struct sem {
92 int semval; /* current value */
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -070093 /*
94 * PID of the process that last modified the semaphore. For
95 * Linux, specifically these are:
96 * - semop
97 * - semctl, via SETVAL and SETALL.
98 * - at task exit when performing undo adjustments (see exit_sem).
99 */
100 int sempid;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700101 spinlock_t lock; /* spinlock for fine-grained semtimedop */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700102 struct list_head pending_alter; /* pending single-sop operations */
103 /* that alter the semaphore */
104 struct list_head pending_const; /* pending single-sop operations */
105 /* that do not alter the semaphore*/
Manfred Sprauld12e1e52013-07-08 16:01:25 -0700106 time_t sem_otime; /* candidate for sem_otime */
Manfred Spraulf5c936c2013-07-08 16:01:22 -0700107} ____cacheline_aligned_in_smp;
Manfred Spraule57940d2011-11-02 13:38:54 -0700108
109/* One queue for each sleeping process in the system. */
110struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700111 struct list_head list; /* queue of pending operations */
112 struct task_struct *sleeper; /* this process */
113 struct sem_undo *undo; /* undo structure */
114 int pid; /* process id of requesting process */
115 int status; /* completion status of operation */
116 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -0700117 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700118 int nsops; /* number of operations */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800119 bool alter; /* does *sops alter the array? */
120 bool dupsop; /* sops on more than one sem_num */
Manfred Spraule57940d2011-11-02 13:38:54 -0700121};
122
123/* Each task has a list of undo requests. They are executed automatically
124 * when the process exits.
125 */
126struct sem_undo {
127 struct list_head list_proc; /* per-process list: *
128 * all undos from one process
129 * rcu protected */
130 struct rcu_head rcu; /* rcu struct for sem_undo */
131 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
132 struct list_head list_id; /* per semaphore array list:
133 * all undos for one array */
134 int semid; /* semaphore set identifier */
135 short *semadj; /* array of adjustments */
136 /* one per semaphore */
137};
138
139/* sem_undo_list controls shared access to the list of sem_undo structures
140 * that may be shared among all a CLONE_SYSVSEM task group.
141 */
142struct sem_undo_list {
143 atomic_t refcnt;
144 spinlock_t lock;
145 struct list_head list_proc;
146};
147
148
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800149#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Nadia Derbey1b531f22007-10-18 23:40:55 -0700151#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700153static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800154static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700156static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif
158
159#define SEMMSL_FAST 256 /* 512 bytes on stack */
160#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
161
162/*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800163 * Switching from the mode suitable for simple ops
164 * to the mode for complex ops is costly. Therefore:
165 * use some hysteresis
166 */
167#define USE_GLOBAL_LOCK_HYSTERESIS 10
168
169/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700170 * Locking:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700171 * a) global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700173 * sem_array.complex_count,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700174 * sem_array.pending{_alter,_const},
175 * sem_array.sem_undo
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700176 *
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700177 * b) global or semaphore sem_lock() for read/write:
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700178 * sem_array.sem_base[i].pending_{const,alter}:
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700179 *
180 * c) special:
181 * sem_undo_list.list_proc:
182 * * undo_list->lock for write
183 * * rcu for read
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800184 * use_global_lock:
185 * * global sem_lock() for write
186 * * either local or global sem_lock() for read.
187 *
188 * Memory ordering:
189 * Most ordering is enforced by using spin_lock() and spin_unlock().
190 * The special case is use_global_lock:
191 * Setting it from non-zero to 0 is a RELEASE, this is ensured by
192 * using smp_store_release().
193 * Testing if it is non-zero is an ACQUIRE, this is ensured by using
194 * smp_load_acquire().
195 * Setting it from 0 to non-zero must be ordered with regards to
196 * this smp_load_acquire(), this is guaranteed because the smp_load_acquire()
197 * is inside a spin_lock() and after a write from 0 to non-zero a
198 * spin_lock()+spin_unlock() is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
200
Kirill Korotaeve3893532006-10-02 02:18:22 -0700201#define sc_semmsl sem_ctls[0]
202#define sc_semmns sem_ctls[1]
203#define sc_semopm sem_ctls[2]
204#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800206void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700207{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700208 ns->sc_semmsl = SEMMSL;
209 ns->sc_semmns = SEMMNS;
210 ns->sc_semopm = SEMOPM;
211 ns->sc_semmni = SEMMNI;
212 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800213 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700214}
215
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800216#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700217void sem_exit_ns(struct ipc_namespace *ns)
218{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800219 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800220 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700221}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Manfred Spraul239521f2014-01-27 17:07:04 -0800224void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800226 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700227 ipc_init_proc_interface("sysvipc/sem",
228 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700229 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Manfred Spraulf269f402013-07-08 16:01:24 -0700232/**
233 * unmerge_queues - unmerge queues, if possible.
234 * @sma: semaphore array
235 *
236 * The function unmerges the wait queues if complex_count is 0.
237 * It must be called prior to dropping the global semaphore array lock.
238 */
239static void unmerge_queues(struct sem_array *sma)
240{
241 struct sem_queue *q, *tq;
242
243 /* complex operations still around? */
244 if (sma->complex_count)
245 return;
246 /*
247 * We will switch back to simple mode.
248 * Move all pending operation back into the per-semaphore
249 * queues.
250 */
251 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
252 struct sem *curr;
253 curr = &sma->sem_base[q->sops[0].sem_num];
254
255 list_add_tail(&q->list, &curr->pending_alter);
256 }
257 INIT_LIST_HEAD(&sma->pending_alter);
258}
259
260/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800261 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700262 * @sma: semaphore array
263 *
264 * This function merges all per-semaphore queues into the global queue.
265 * It is necessary to achieve FIFO ordering for the pending single-sop
266 * operations when a multi-semop operation must sleep.
267 * Only the alter operations must be moved, the const operations can stay.
268 */
269static void merge_queues(struct sem_array *sma)
270{
271 int i;
272 for (i = 0; i < sma->sem_nsems; i++) {
273 struct sem *sem = sma->sem_base + i;
274
275 list_splice_init(&sem->pending_alter, &sma->pending_alter);
276 }
277}
278
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700279static void sem_rcu_free(struct rcu_head *head)
280{
281 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
282 struct sem_array *sma = ipc_rcu_to_struct(p);
283
284 security_sem_free(sma);
285 ipc_rcu_free(head);
286}
287
Nadia Derbey3e148c72007-10-18 23:40:54 -0700288/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700289 * Enter the mode suitable for non-simple operations:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700290 * Caller must own sem_perm.lock.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700291 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700292static void complexmode_enter(struct sem_array *sma)
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700293{
294 int i;
295 struct sem *sem;
296
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800297 if (sma->use_global_lock > 0) {
298 /*
299 * We are already in global lock mode.
300 * Nothing to do, just reset the
301 * counter until we return to simple mode.
302 */
303 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul6d07b682013-09-30 13:45:06 -0700304 return;
305 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800306 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700307
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700308 for (i = 0; i < sma->sem_nsems; i++) {
309 sem = sma->sem_base + i;
Manfred Spraul27d7be12017-02-27 14:28:15 -0800310 spin_lock(&sem->lock);
311 spin_unlock(&sem->lock);
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700312 }
313}
314
315/*
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700316 * Try to leave the mode that disallows simple operations:
317 * Caller must own sem_perm.lock.
318 */
319static void complexmode_tryleave(struct sem_array *sma)
320{
321 if (sma->complex_count) {
322 /* Complex ops are sleeping.
323 * We must stay in complex mode
324 */
325 return;
326 }
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800327 if (sma->use_global_lock == 1) {
328 /*
329 * Immediately after setting use_global_lock to 0,
330 * a simple op can start. Thus: all memory writes
331 * performed by the current operation must be visible
332 * before we set use_global_lock to 0.
333 */
334 smp_store_release(&sma->use_global_lock, 0);
335 } else {
336 sma->use_global_lock--;
337 }
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700338}
339
340#define SEM_GLOBAL_LOCK (-1)
341/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700342 * If the request contains only one semaphore operation, and there are
343 * no complex transactions pending, lock only the semaphore involved.
344 * Otherwise, lock the entire semaphore array, since we either have
345 * multiple semaphores in our own semops, or we need to look at
346 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700347 */
348static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
349 int nsops)
350{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700351 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700352
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700353 if (nsops != 1) {
354 /* Complex operation - acquire a full lock */
355 ipc_lock_object(&sma->sem_perm);
356
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700357 /* Prevent parallel simple ops */
358 complexmode_enter(sma);
359 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700360 }
361
362 /*
363 * Only one semaphore affected - try to optimize locking.
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700364 * Optimized locking is possible if no complex operation
365 * is either enqueued or processed right now.
366 *
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800367 * Both facts are tracked by use_global_mode.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700368 */
369 sem = sma->sem_base + sops->sem_num;
370
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700371 /*
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800372 * Initial check for use_global_lock. Just an optimization,
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700373 * no locking, no memory barrier.
374 */
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800375 if (!sma->use_global_lock) {
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700376 /*
377 * It appears that no complex operation is around.
378 * Acquire the per-semaphore lock.
379 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700380 spin_lock(&sem->lock);
381
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800382 /* pairs with smp_store_release() */
383 if (!smp_load_acquire(&sma->use_global_lock)) {
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700384 /* fast path successful! */
385 return sops->sem_num;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700386 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700387 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700388 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700389
390 /* slow path: acquire the full lock */
391 ipc_lock_object(&sma->sem_perm);
392
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800393 if (sma->use_global_lock == 0) {
394 /*
395 * The use_global_lock mode ended while we waited for
396 * sma->sem_perm.lock. Thus we must switch to locking
397 * with sem->lock.
398 * Unlike in the fast path, there is no need to recheck
399 * sma->use_global_lock after we have acquired sem->lock:
400 * We own sma->sem_perm.lock, thus use_global_lock cannot
401 * change.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700402 */
403 spin_lock(&sem->lock);
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800404
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700405 ipc_unlock_object(&sma->sem_perm);
406 return sops->sem_num;
407 } else {
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800408 /*
409 * Not a false alarm, thus continue to use the global lock
410 * mode. No need for complexmode_enter(), this was done by
411 * the caller that has set use_global_mode to non-zero.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700412 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700413 return SEM_GLOBAL_LOCK;
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700414 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700415}
416
417static inline void sem_unlock(struct sem_array *sma, int locknum)
418{
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700419 if (locknum == SEM_GLOBAL_LOCK) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700420 unmerge_queues(sma);
Manfred Spraul5864a2f2016-10-11 13:54:50 -0700421 complexmode_tryleave(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700422 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700423 } else {
424 struct sem *sem = sma->sem_base + locknum;
425 spin_unlock(&sem->lock);
426 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700427}
428
429/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700430 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700431 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700432 *
433 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700434 */
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700435static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
436{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700437 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700438
439 if (IS_ERR(ipcp))
440 return ERR_CAST(ipcp);
441
442 return container_of(ipcp, struct sem_array, sem_perm);
443}
444
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700445static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
446 int id)
447{
448 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
449
450 if (IS_ERR(ipcp))
451 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800452
Nadia Derbey03f02c72007-10-18 23:40:51 -0700453 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700454}
455
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700456static inline void sem_lock_and_putref(struct sem_array *sma)
457{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700458 sem_lock(sma, NULL, -1);
Fabian Frederick9b24fef2016-08-02 14:03:07 -0700459 ipc_rcu_putref(sma, sem_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700460}
461
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700462static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
463{
464 ipc_rmid(&sem_ids(ns), &s->sem_perm);
465}
466
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700467/**
468 * newary - Create a new semaphore set
469 * @ns: namespace
470 * @params: ptr to the structure that contains key, semflg and nsems
471 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700472 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700473 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700474static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 int id;
477 int retval;
478 struct sem_array *sma;
479 int size;
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
Manfred Spraul239521f2014-01-27 17:07:04 -0800490 size = sizeof(*sma) + nsems * sizeof(struct sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 sma = ipc_rcu_alloc(size);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800492 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800494
Manfred Spraul239521f2014-01-27 17:07:04 -0800495 memset(sma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 sma->sem_perm.mode = (semflg & S_IRWXUGO);
498 sma->sem_perm.key = key;
499
500 sma->sem_perm.security = NULL;
501 retval = security_sem_alloc(sma);
502 if (retval) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700503 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return retval;
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800508
Rik van Riel6062a8d2013-04-30 19:15:44 -0700509 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700510 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
511 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700512 spin_lock_init(&sma->sem_base[i].lock);
513 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800514
515 sma->complex_count = 0;
Manfred Spraul9de5ab82017-02-27 14:28:18 -0800516 sma->use_global_lock = USE_GLOBAL_LOCK_HYSTERESIS;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700517 INIT_LIST_HEAD(&sma->pending_alter);
518 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700519 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 sma->sem_nsems = nsems;
521 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800522
523 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
524 if (id < 0) {
525 ipc_rcu_putref(sma, sem_rcu_free);
526 return id;
527 }
528 ns->used_sems += nsems;
529
Rik van Riel6062a8d2013-04-30 19:15:44 -0700530 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700533 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700536
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700537/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700538 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700539 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700540static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700542 struct sem_array *sma;
543
544 sma = container_of(ipcp, struct sem_array, sem_perm);
545 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700546}
547
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700548/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700549 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700550 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700551static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
552 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700553{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700554 struct sem_array *sma;
555
556 sma = container_of(ipcp, struct sem_array, sem_perm);
557 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700558 return -EINVAL;
559
560 return 0;
561}
562
Heiko Carstensd5460c92009-01-14 14:14:27 +0100563SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700564{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700565 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700566 static const struct ipc_ops sem_ops = {
567 .getnew = newary,
568 .associate = sem_security,
569 .more_checks = sem_more_checks,
570 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700571 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Kirill Korotaeve3893532006-10-02 02:18:22 -0700573 ns = current->nsproxy->ipc_ns;
574
575 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700577
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700578 sem_params.key = key;
579 sem_params.flg = semflg;
580 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700581
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700582 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Petr Mladek78f50092014-01-27 17:07:00 -0800585/**
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800586 * perform_atomic_semop[_slow] - Attempt to perform semaphore
587 * operations on a given array.
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700588 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700589 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700590 *
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800591 * Caller blocking are as follows, based the value
592 * indicated by the semaphore operation (sem_op):
593 *
594 * (1) >0 never blocks.
595 * (2) 0 (wait-for-zero operation): semval is non-zero.
596 * (3) <0 attempting to decrement semval to a value smaller than zero.
597 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700598 * Returns 0 if the operation was possible.
599 * Returns 1 if the operation is impossible, the caller must sleep.
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800600 * Returns <0 for error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800602static int perform_atomic_semop_slow(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700604 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800606 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700607 struct sembuf *sops;
608 struct sem_undo *un;
609
610 sops = q->sops;
611 nsops = q->nsops;
612 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 for (sop = sops; sop < sops + nsops; sop++) {
615 curr = sma->sem_base + sop->sem_num;
616 sem_op = sop->sem_op;
617 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!sem_op && result)
620 goto would_block;
621
622 result += sem_op;
623 if (result < 0)
624 goto would_block;
625 if (result > SEMVMX)
626 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (sop->sem_flg & SEM_UNDO) {
629 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800630 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
632 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800633 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Petr Mladek78f50092014-01-27 17:07:00 -0800635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 curr->semval = result;
637 }
638
639 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700640 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 while (sop >= sops) {
642 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 sop--;
644 }
Petr Mladek78f50092014-01-27 17:07:00 -0800645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return 0;
647
648out_of_range:
649 result = -ERANGE;
650 goto undo;
651
652would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700653 q->blocking = sop;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (sop->sem_flg & IPC_NOWAIT)
656 result = -EAGAIN;
657 else
658 result = 1;
659
660undo:
661 sop--;
662 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800663 sem_op = sop->sem_op;
664 sma->sem_base[sop->sem_num].semval -= sem_op;
665 if (sop->sem_flg & SEM_UNDO)
666 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 sop--;
668 }
669
670 return result;
671}
672
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -0800673static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
674{
675 int result, sem_op, nsops;
676 struct sembuf *sop;
677 struct sem *curr;
678 struct sembuf *sops;
679 struct sem_undo *un;
680
681 sops = q->sops;
682 nsops = q->nsops;
683 un = q->undo;
684
685 if (unlikely(q->dupsop))
686 return perform_atomic_semop_slow(sma, q);
687
688 /*
689 * We scan the semaphore set twice, first to ensure that the entire
690 * operation can succeed, therefore avoiding any pointless writes
691 * to shared memory and having to undo such changes in order to block
692 * until the operations can go through.
693 */
694 for (sop = sops; sop < sops + nsops; sop++) {
695 curr = sma->sem_base + sop->sem_num;
696 sem_op = sop->sem_op;
697 result = curr->semval;
698
699 if (!sem_op && result)
700 goto would_block; /* wait-for-zero */
701
702 result += sem_op;
703 if (result < 0)
704 goto would_block;
705
706 if (result > SEMVMX)
707 return -ERANGE;
708
709 if (sop->sem_flg & SEM_UNDO) {
710 int undo = un->semadj[sop->sem_num] - sem_op;
711
712 /* Exceeding the undo range is an error. */
713 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
714 return -ERANGE;
715 }
716 }
717
718 for (sop = sops; sop < sops + nsops; sop++) {
719 curr = sma->sem_base + sop->sem_num;
720 sem_op = sop->sem_op;
721 result = curr->semval;
722
723 if (sop->sem_flg & SEM_UNDO) {
724 int undo = un->semadj[sop->sem_num] - sem_op;
725
726 un->semadj[sop->sem_num] = undo;
727 }
728 curr->semval += sem_op;
729 curr->sempid = q->pid;
730 }
731
732 return 0;
733
734would_block:
735 q->blocking = sop;
736 return sop->sem_flg & IPC_NOWAIT ? -EAGAIN : 1;
737}
738
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800739static inline void wake_up_sem_queue_prepare(struct sem_queue *q, int error,
740 struct wake_q_head *wake_q)
Nick Piggind4212092009-12-15 16:47:30 -0800741{
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800742 wake_q_add(wake_q, q->sleeper);
743 /*
744 * Rely on the above implicit barrier, such that we can
745 * ensure that we hold reference to the task before setting
746 * q->status. Otherwise we could race with do_exit if the
747 * task is awoken by an external event before calling
748 * wake_up_process().
749 */
750 WRITE_ONCE(q->status, error);
Nick Piggind4212092009-12-15 16:47:30 -0800751}
752
Manfred Spraulb97e8202009-12-15 16:47:32 -0800753static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
754{
755 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700756 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800757 sma->complex_count--;
758}
759
Manfred Spraulfd5db422010-05-26 14:43:40 -0700760/** check_restart(sma, q)
761 * @sma: semaphore array
762 * @q: the operation that just completed
763 *
764 * update_queue is O(N^2) when it restarts scanning the whole queue of
765 * waiting operations. Therefore this function checks if the restart is
766 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700767 * modified the array.
768 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700769 */
Davidlohr Bueso4663d3e2016-12-14 15:06:40 -0800770static inline int check_restart(struct sem_array *sma, struct sem_queue *q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700771{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700772 /* pending complex alter operations are too difficult to analyse */
773 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700774 return 1;
775
776 /* we were a sleeping complex operation. Too difficult */
777 if (q->nsops > 1)
778 return 1;
779
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700780 /* It is impossible that someone waits for the new value:
781 * - complex operations always restart.
782 * - wait-for-zero are handled seperately.
783 * - q is a previously sleeping simple operation that
784 * altered the array. It must be a decrement, because
785 * simple increments never sleep.
786 * - If there are older (higher priority) decrements
787 * in the queue, then they have observed the original
788 * semval value and couldn't proceed. The operation
789 * decremented to value - thus they won't proceed either.
790 */
791 return 0;
792}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700793
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700794/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800795 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700796 * @sma: semaphore array.
797 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800798 * @wake_q: lockless wake-queue head.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700799 *
800 * wake_const_ops must be called after a semaphore in a semaphore array
801 * was set to 0. If complex const operations are pending, wake_const_ops must
802 * be called with semnum = -1, as well as with the number of each modified
803 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800804 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700805 * is stored in q->pid.
806 * The function returns 1 if at least one operation was completed successfully.
807 */
808static int wake_const_ops(struct sem_array *sma, int semnum,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800809 struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700810{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800811 struct sem_queue *q, *tmp;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700812 struct list_head *pending_list;
813 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700814
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700815 if (semnum == -1)
816 pending_list = &sma->pending_const;
817 else
818 pending_list = &sma->sem_base[semnum].pending_const;
819
Davidlohr Buesof150f022016-12-14 15:06:43 -0800820 list_for_each_entry_safe(q, tmp, pending_list, list) {
821 int error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700822
Davidlohr Buesof150f022016-12-14 15:06:43 -0800823 if (error > 0)
824 continue;
825 /* operation completed, remove from queue & wakeup */
826 unlink_queue(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700827
Davidlohr Buesof150f022016-12-14 15:06:43 -0800828 wake_up_sem_queue_prepare(q, error, wake_q);
829 if (error == 0)
830 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700831 }
Davidlohr Buesof150f022016-12-14 15:06:43 -0800832
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700833 return semop_completed;
834}
835
836/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800837 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700838 * @sma: semaphore array
839 * @sops: operations that were performed
840 * @nsops: number of operations
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800841 * @wake_q: lockless wake-queue head
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700842 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800843 * Checks all required queue for wait-for-zero operations, based
844 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700845 * The function returns 1 if at least one operation was completed successfully.
846 */
847static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800848 int nsops, struct wake_q_head *wake_q)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700849{
850 int i;
851 int semop_completed = 0;
852 int got_zero = 0;
853
854 /* first: the per-semaphore queues, if known */
855 if (sops) {
856 for (i = 0; i < nsops; i++) {
857 int num = sops[i].sem_num;
858
859 if (sma->sem_base[num].semval == 0) {
860 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800861 semop_completed |= wake_const_ops(sma, num, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700862 }
863 }
864 } else {
865 /*
866 * No sops means modified semaphores not known.
867 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700868 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700869 for (i = 0; i < sma->sem_nsems; i++) {
870 if (sma->sem_base[i].semval == 0) {
871 got_zero = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800872 semop_completed |= wake_const_ops(sma, i, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700873 }
874 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700875 }
876 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700877 * If one of the modified semaphores got 0,
878 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700879 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700880 if (got_zero)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800881 semop_completed |= wake_const_ops(sma, -1, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700882
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700883 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700884}
885
Manfred Spraul636c6be2009-12-15 16:47:33 -0800886
887/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800888 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800889 * @sma: semaphore array.
890 * @semnum: semaphore that was modified.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800891 * @wake_q: lockless wake-queue head.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800892 *
893 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700894 * was modified. If multiple semaphores were modified, update_queue must
895 * be called with semnum = -1, as well as with the number of each modified
896 * semaphore.
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800897 * The tasks that must be woken up are added to @wake_q. The return code
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700898 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700899 * The function internally checks if const operations can now succeed.
900 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700901 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800903static int update_queue(struct sem_array *sma, int semnum, struct wake_q_head *wake_q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Davidlohr Buesof150f022016-12-14 15:06:43 -0800905 struct sem_queue *q, *tmp;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800906 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700907 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800908
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700909 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700910 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700911 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700912 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Nick Piggin9cad2002009-12-15 16:47:29 -0800914again:
Davidlohr Buesof150f022016-12-14 15:06:43 -0800915 list_for_each_entry_safe(q, tmp, pending_list, list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700916 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800917
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800918 /* If we are scanning the single sop, per-semaphore list of
919 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700920 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800921 * that affect only one entry succeed immediately and cannot
922 * be in the per semaphore pending queue, and decrements
923 * cannot be successful if the value is already 0.
924 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700925 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800926 break;
927
Manfred Sprauld198cd62014-06-06 14:37:49 -0700928 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800931 if (error > 0)
932 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700933
Manfred Spraulb97e8202009-12-15 16:47:32 -0800934 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700935
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700936 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700937 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700938 } else {
939 semop_completed = 1;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800940 do_smart_wakeup_zero(sma, q->sops, q->nsops, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700941 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700942 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700943
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800944 wake_up_sem_queue_prepare(q, error, wake_q);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700945 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800946 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700948 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700951/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800952 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700953 * @sma: semaphore array
954 * @sops: operations that modified the array, may be NULL
955 *
956 * sem_otime is replicated to avoid cache line trashing.
957 * This function sets one instance to the current time.
958 */
959static void set_semotime(struct sem_array *sma, struct sembuf *sops)
960{
961 if (sops == NULL) {
962 sma->sem_base[0].sem_otime = get_seconds();
963 } else {
964 sma->sem_base[sops[0].sem_num].sem_otime =
965 get_seconds();
966 }
967}
968
969/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800970 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700971 * @sma: semaphore array
972 * @sops: operations that were performed
973 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700974 * @otime: force setting otime
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800975 * @wake_q: lockless wake-queue head
Manfred Spraulfd5db422010-05-26 14:43:40 -0700976 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700977 * do_smart_update() does the required calls to update_queue and wakeup_zero,
978 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700979 * Note that the function does not do the actual wake-up: the caller is
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800980 * responsible for calling wake_up_q().
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700981 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700982 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700983static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800984 int otime, struct wake_q_head *wake_q)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700985{
986 int i;
987
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800988 otime |= do_smart_wakeup_zero(sma, sops, nsops, wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700989
Manfred Spraulf269f402013-07-08 16:01:24 -0700990 if (!list_empty(&sma->pending_alter)) {
991 /* semaphore array uses the global queue - just process it. */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -0800992 otime |= update_queue(sma, -1, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -0700993 } else {
994 if (!sops) {
995 /*
996 * No sops, thus the modified semaphores are not
997 * known. Check all.
998 */
999 for (i = 0; i < sma->sem_nsems; i++)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001000 otime |= update_queue(sma, i, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001001 } else {
1002 /*
1003 * Check the semaphores that were increased:
1004 * - No complex ops, thus all sleeping ops are
1005 * decrease.
1006 * - if we decreased the value, then any sleeping
1007 * semaphore ops wont be able to run: If the
1008 * previous value was too small, then the new
1009 * value will be too small, too.
1010 */
1011 for (i = 0; i < nsops; i++) {
1012 if (sops[i].sem_op > 0) {
1013 otime |= update_queue(sma,
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001014 sops[i].sem_num, wake_q);
Manfred Spraulf269f402013-07-08 16:01:24 -07001015 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001016 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001017 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001018 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001019 if (otime)
1020 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001021}
1022
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001023/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001024 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001025 */
1026static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1027 bool count_zero)
1028{
Manfred Spraulb220c572014-06-06 14:37:51 -07001029 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001030
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001031 /*
1032 * Linux always (since 0.99.10) reported a task as sleeping on all
1033 * semaphores. This violates SUS, therefore it was changed to the
1034 * standard compliant behavior.
1035 * Give the administrators a chance to notice that an application
1036 * might misbehave because it relies on the Linux behavior.
1037 */
1038 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1039 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1040 current->comm, task_pid_nr(current));
1041
Manfred Spraulb220c572014-06-06 14:37:51 -07001042 if (sop->sem_num != semnum)
1043 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001044
Manfred Spraulb220c572014-06-06 14:37:51 -07001045 if (count_zero && sop->sem_op == 0)
1046 return 1;
1047 if (!count_zero && sop->sem_op < 0)
1048 return 1;
1049
1050 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/* The following counts are associated to each semaphore:
1054 * semncnt number of tasks waiting on semval being nonzero
1055 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001056 *
1057 * Per definition, a task waits only on the semaphore of the first semop
1058 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001060static int count_semcnt(struct sem_array *sma, ushort semnum,
1061 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001063 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001064 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001065 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001067 semcnt = 0;
1068 /* First: check the simple operations. They are easy to evaluate */
1069 if (count_zero)
1070 l = &sma->sem_base[semnum].pending_const;
1071 else
1072 l = &sma->sem_base[semnum].pending_alter;
1073
1074 list_for_each_entry(q, l, list) {
1075 /* all task on a per-semaphore list sleep on exactly
1076 * that semaphore
1077 */
1078 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001079 }
1080
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001081 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001082 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001083 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001085 if (count_zero) {
1086 list_for_each_entry(q, &sma->pending_const, list) {
1087 semcnt += check_qop(sma, semnum, q, count_zero);
1088 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001089 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001090 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001093/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1094 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001095 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001097static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001099 struct sem_undo *un, *tu;
1100 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001101 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001102 int i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001103 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Manfred Spraul380af1b2008-07-25 01:48:06 -07001105 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001106 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001107 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1108 list_del(&un->list_id);
1109 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001111 list_del_rcu(&un->list_proc);
1112 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001113 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001117 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1118 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001119 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001120 }
1121
1122 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001123 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001124 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001126 for (i = 0; i < sma->sem_nsems; i++) {
1127 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001128 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1129 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001130 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001131 }
1132 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001133 unlink_queue(sma, q);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001134 wake_up_sem_queue_prepare(q, -EIDRM, &wake_q);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001135 }
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001138 /* Remove the semaphore set from the IDR */
1139 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001140 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001143 wake_up_q(&wake_q);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001144 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001145 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
1148static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1149{
Manfred Spraul239521f2014-01-27 17:07:04 -08001150 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 case IPC_64:
1152 return copy_to_user(buf, in, sizeof(*in));
1153 case IPC_OLD:
1154 {
1155 struct semid_ds out;
1156
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001157 memset(&out, 0, sizeof(out));
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1160
1161 out.sem_otime = in->sem_otime;
1162 out.sem_ctime = in->sem_ctime;
1163 out.sem_nsems = in->sem_nsems;
1164
1165 return copy_to_user(buf, &out, sizeof(out));
1166 }
1167 default:
1168 return -EINVAL;
1169 }
1170}
1171
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001172static time_t get_semotime(struct sem_array *sma)
1173{
1174 int i;
1175 time_t res;
1176
1177 res = sma->sem_base[0].sem_otime;
1178 for (i = 1; i < sma->sem_nsems; i++) {
1179 time_t to = sma->sem_base[i].sem_otime;
1180
1181 if (to > res)
1182 res = to;
1183 }
1184 return res;
1185}
1186
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001187static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001188 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001190 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 struct sem_array *sma;
1192
Manfred Spraul239521f2014-01-27 17:07:04 -08001193 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 case IPC_INFO:
1195 case SEM_INFO:
1196 {
1197 struct seminfo seminfo;
1198 int max_id;
1199
1200 err = security_sem_semctl(NULL, cmd);
1201 if (err)
1202 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001203
Manfred Spraul239521f2014-01-27 17:07:04 -08001204 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001205 seminfo.semmni = ns->sc_semmni;
1206 seminfo.semmns = ns->sc_semmns;
1207 seminfo.semmsl = ns->sc_semmsl;
1208 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 seminfo.semvmx = SEMVMX;
1210 seminfo.semmnu = SEMMNU;
1211 seminfo.semmap = SEMMAP;
1212 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001213 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001215 seminfo.semusz = sem_ids(ns).in_use;
1216 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 } else {
1218 seminfo.semusz = SEMUSZ;
1219 seminfo.semaem = SEMAEM;
1220 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001221 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001222 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001223 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001225 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001227 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case SEM_STAT:
1229 {
1230 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001231 int id = 0;
1232
1233 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Linus Torvalds941b0302013-05-04 11:04:29 -07001235 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001236 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001237 sma = sem_obtain_object(ns, semid);
1238 if (IS_ERR(sma)) {
1239 err = PTR_ERR(sma);
1240 goto out_unlock;
1241 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001242 id = sma->sem_perm.id;
1243 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001244 sma = sem_obtain_object_check(ns, semid);
1245 if (IS_ERR(sma)) {
1246 err = PTR_ERR(sma);
1247 goto out_unlock;
1248 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001252 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto out_unlock;
1254
1255 err = security_sem_semctl(sma, cmd);
1256 if (err)
1257 goto out_unlock;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001260 tbuf.sem_otime = get_semotime(sma);
1261 tbuf.sem_ctime = sma->sem_ctime;
1262 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001263 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001264 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EFAULT;
1266 return id;
1267 }
1268 default:
1269 return -EINVAL;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001272 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return err;
1274}
1275
Al Viroe1fd1f42013-03-05 15:04:55 -05001276static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1277 unsigned long arg)
1278{
1279 struct sem_undo *un;
1280 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001281 struct sem *curr;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001282 int err, val;
1283 DEFINE_WAKE_Q(wake_q);
1284
Al Viroe1fd1f42013-03-05 15:04:55 -05001285#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1286 /* big-endian 64bit */
1287 val = arg >> 32;
1288#else
1289 /* 32bit or little-endian 64bit */
1290 val = arg;
1291#endif
1292
Rik van Riel6062a8d2013-04-30 19:15:44 -07001293 if (val > SEMVMX || val < 0)
1294 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001295
Rik van Riel6062a8d2013-04-30 19:15:44 -07001296 rcu_read_lock();
1297 sma = sem_obtain_object_check(ns, semid);
1298 if (IS_ERR(sma)) {
1299 rcu_read_unlock();
1300 return PTR_ERR(sma);
1301 }
1302
1303 if (semnum < 0 || semnum >= sma->sem_nsems) {
1304 rcu_read_unlock();
1305 return -EINVAL;
1306 }
1307
1308
1309 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1310 rcu_read_unlock();
1311 return -EACCES;
1312 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001313
1314 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001315 if (err) {
1316 rcu_read_unlock();
1317 return -EACCES;
1318 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001319
Rik van Riel6062a8d2013-04-30 19:15:44 -07001320 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001321
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001322 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001323 sem_unlock(sma, -1);
1324 rcu_read_unlock();
1325 return -EIDRM;
1326 }
1327
Al Viroe1fd1f42013-03-05 15:04:55 -05001328 curr = &sma->sem_base[semnum];
1329
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001330 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001331 list_for_each_entry(un, &sma->list_id, list_id)
1332 un->semadj[semnum] = 0;
1333
1334 curr->semval = val;
1335 curr->sempid = task_tgid_vnr(current);
1336 sma->sem_ctime = get_seconds();
1337 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001338 do_smart_update(sma, NULL, 0, 0, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001339 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001340 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001341 wake_up_q(&wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001342 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001343}
1344
Kirill Korotaeve3893532006-10-02 02:18:22 -07001345static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001346 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001349 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001350 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001352 ushort *sem_io = fast_sem_io;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001353 DEFINE_WAKE_Q(wake_q);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001354
1355 rcu_read_lock();
1356 sma = sem_obtain_object_check(ns, semid);
1357 if (IS_ERR(sma)) {
1358 rcu_read_unlock();
1359 return PTR_ERR(sma);
1360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 nsems = sma->sem_nsems;
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001365 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1366 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001369 if (err)
1370 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 err = -EACCES;
1373 switch (cmd) {
1374 case GETALL:
1375 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001376 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int i;
1378
Al Viroce857222013-05-03 00:30:49 +01001379 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001380 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001381 err = -EIDRM;
1382 goto out_unlock;
1383 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001384 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001385 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001386 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001387 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001388 }
1389 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001390 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001392 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001393 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return -ENOMEM;
1395 }
1396
Linus Torvalds4091fd942013-05-04 10:13:40 -07001397 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001398 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001399 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001401 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Al Viroce857222013-05-03 00:30:49 +01001403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 for (i = 0; i < sma->sem_nsems; i++)
1405 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001406 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001409 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 err = -EFAULT;
1411 goto out_free;
1412 }
1413 case SETALL:
1414 {
1415 int i;
1416 struct sem_undo *un;
1417
Rik van Riel6062a8d2013-04-30 19:15:44 -07001418 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001419 err = -EIDRM;
1420 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001421 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001422 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Manfred Spraul239521f2014-01-27 17:07:04 -08001424 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001426 if (sem_io == NULL) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001427 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return -ENOMEM;
1429 }
1430 }
1431
Manfred Spraul239521f2014-01-27 17:07:04 -08001432 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001433 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 err = -EFAULT;
1435 goto out_free;
1436 }
1437
1438 for (i = 0; i < nsems; i++) {
1439 if (sem_io[i] > SEMVMX) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001440 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 err = -ERANGE;
1442 goto out_free;
1443 }
1444 }
Linus Torvalds4091fd942013-05-04 10:13:40 -07001445 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001446 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001447 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001449 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001452 for (i = 0; i < nsems; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 sma->sem_base[i].semval = sem_io[i];
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001454 sma->sem_base[i].sempid = task_tgid_vnr(current);
1455 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001456
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001457 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001458 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 for (i = 0; i < nsems; i++)
1460 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 sma->sem_ctime = get_seconds();
1463 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001464 do_smart_update(sma, NULL, 0, 0, &wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 err = 0;
1466 goto out_unlock;
1467 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001468 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001471 if (semnum < 0 || semnum >= nsems)
1472 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Rik van Riel6062a8d2013-04-30 19:15:44 -07001474 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001475 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001476 err = -EIDRM;
1477 goto out_unlock;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 curr = &sma->sem_base[semnum];
1480
1481 switch (cmd) {
1482 case GETVAL:
1483 err = curr->semval;
1484 goto out_unlock;
1485 case GETPID:
1486 err = curr->sempid;
1487 goto out_unlock;
1488 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001489 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto out_unlock;
1491 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001492 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001497 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001498out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001499 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001500 wake_up_q(&wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001502 if (sem_io != fast_sem_io)
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001503 ipc_free(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return err;
1505}
1506
Pierre Peiffer016d7132008-04-29 01:00:50 -07001507static inline unsigned long
1508copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Manfred Spraul239521f2014-01-27 17:07:04 -08001510 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001512 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 case IPC_OLD:
1516 {
1517 struct semid_ds tbuf_old;
1518
Manfred Spraul239521f2014-01-27 17:07:04 -08001519 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EFAULT;
1521
Pierre Peiffer016d7132008-04-29 01:00:50 -07001522 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1523 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1524 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526 return 0;
1527 }
1528 default:
1529 return -EINVAL;
1530 }
1531}
1532
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001533/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001534 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001535 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001536 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001537 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001538static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001539 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct sem_array *sma;
1542 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001543 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 struct kern_ipc_perm *ipcp;
1545
Manfred Spraul239521f2014-01-27 17:07:04 -08001546 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001547 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001551 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001552 rcu_read_lock();
1553
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001554 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1555 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001556 if (IS_ERR(ipcp)) {
1557 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001558 goto out_unlock1;
1559 }
Steve Grubb073115d2006-04-02 17:07:33 -04001560
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001561 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001564 if (err)
1565 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001567 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001569 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001570 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001571 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001572 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001574 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001575 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1576 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001577 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 break;
1580 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001582 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001585out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001586 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001587out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001588 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001589out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001590 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return err;
1592}
1593
Al Viroe1fd1f42013-03-05 15:04:55 -05001594SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001597 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001598 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (semid < 0)
1601 return -EINVAL;
1602
1603 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001604 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Manfred Spraul239521f2014-01-27 17:07:04 -08001606 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 case IPC_INFO:
1608 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001609 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001611 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 case GETALL:
1613 case GETVAL:
1614 case GETPID:
1615 case GETNCNT:
1616 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001618 return semctl_main(ns, semid, semnum, cmd, p);
1619 case SETVAL:
1620 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 case IPC_RMID:
1622 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001623 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 default:
1625 return -EINVAL;
1626 }
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* If the task doesn't already have a undo_list, then allocate one
1630 * here. We guarantee there is only one thread using this undo list,
1631 * and current is THE ONE
1632 *
1633 * If this allocation and assignment succeeds, but later
1634 * portions of this code fail, there is no need to free the sem_undo_list.
1635 * Just let it stay associated with the task, and it'll be freed later
1636 * at exit time.
1637 *
1638 * This can block, so callers must hold no locks.
1639 */
1640static inline int get_undo_list(struct sem_undo_list **undo_listp)
1641{
1642 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 undo_list = current->sysvsem.undo_list;
1645 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001646 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (undo_list == NULL)
1648 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001649 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001651 INIT_LIST_HEAD(&undo_list->list_proc);
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 current->sysvsem.undo_list = undo_list;
1654 }
1655 *undo_listp = undo_list;
1656 return 0;
1657}
1658
Nick Pigginbf17bb72009-12-15 16:47:28 -08001659static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001661 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Nick Pigginbf17bb72009-12-15 16:47:28 -08001663 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1664 if (un->semid == semid)
1665 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001667 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Nick Pigginbf17bb72009-12-15 16:47:28 -08001670static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1671{
1672 struct sem_undo *un;
1673
Manfred Spraul239521f2014-01-27 17:07:04 -08001674 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001675
1676 un = __lookup_undo(ulp, semid);
1677 if (un) {
1678 list_del_rcu(&un->list_proc);
1679 list_add_rcu(&un->list_proc, &ulp->list_proc);
1680 }
1681 return un;
1682}
1683
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001684/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001685 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001686 * @ns: namespace
1687 * @semid: semaphore array id
1688 *
1689 * The function looks up (and if not present creates) the undo structure.
1690 * The size of the undo structure depends on the size of the semaphore
1691 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001692 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1693 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001694 */
1695static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct sem_array *sma;
1698 struct sem_undo_list *ulp;
1699 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001700 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 error = get_undo_list(&ulp);
1703 if (error)
1704 return ERR_PTR(error);
1705
Manfred Spraul380af1b2008-07-25 01:48:06 -07001706 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001707 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001709 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001710 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto out;
1712
1713 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001714 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001715 sma = sem_obtain_object_check(ns, semid);
1716 if (IS_ERR(sma)) {
1717 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001718 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001719 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001722 if (!ipc_rcu_getref(sma)) {
1723 rcu_read_unlock();
1724 un = ERR_PTR(-EIDRM);
1725 goto out;
1726 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001727 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001729 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001730 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 if (!new) {
Fabian Frederick9b24fef2016-08-02 14:03:07 -07001732 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return ERR_PTR(-ENOMEM);
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Manfred Spraul380af1b2008-07-25 01:48:06 -07001736 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd942013-05-04 10:13:40 -07001737 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001738 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001739 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001740 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001741 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 kfree(new);
1743 un = ERR_PTR(-EIDRM);
1744 goto out;
1745 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001746 spin_lock(&ulp->lock);
1747
1748 /*
1749 * step 4: check for races: did someone else allocate the undo struct?
1750 */
1751 un = lookup_undo(ulp, semid);
1752 if (un) {
1753 kfree(new);
1754 goto success;
1755 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001756 /* step 5: initialize & link new undo structure */
1757 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001758 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001759 new->semid = semid;
1760 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001761 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001762 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001763 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001764 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001765
1766success:
1767 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001768 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769out:
1770 return un;
1771}
1772
Heiko Carstensd5460c92009-01-14 14:14:27 +01001773SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1774 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 int error = -EINVAL;
1777 struct sem_array *sma;
1778 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001779 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct sem_undo *un;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001781 int max, locknum;
1782 bool undos = false, alter = false, dupsop = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct sem_queue queue;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001784 unsigned long dup = 0, jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001785 struct ipc_namespace *ns;
1786
1787 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 if (nsops < 1 || semid < 0)
1790 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001791 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001793 if (nsops > SEMOPM_FAST) {
1794 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1795 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return -ENOMEM;
1797 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001798
Manfred Spraul239521f2014-01-27 17:07:04 -08001799 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1800 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 goto out_free;
1802 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (timeout) {
1805 struct timespec _timeout;
1806 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1807 error = -EFAULT;
1808 goto out_free;
1809 }
1810 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1811 _timeout.tv_nsec >= 1000000000L) {
1812 error = -EINVAL;
1813 goto out_free;
1814 }
1815 jiffies_left = timespec_to_jiffies(&_timeout);
1816 }
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 max = 0;
1819 for (sop = sops; sop < sops + nsops; sop++) {
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001820 unsigned long mask = 1ULL << ((sop->sem_num) % BITS_PER_LONG);
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (sop->sem_num >= max)
1823 max = sop->sem_num;
1824 if (sop->sem_flg & SEM_UNDO)
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001825 undos = true;
1826 if (dup & mask) {
1827 /*
1828 * There was a previous alter access that appears
1829 * to have accessed the same semaphore, thus use
1830 * the dupsop logic. "appears", because the detection
1831 * can only check % BITS_PER_LONG.
1832 */
1833 dupsop = true;
1834 }
1835 if (sop->sem_op != 0) {
1836 alter = true;
1837 dup |= mask;
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001842 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001843 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (IS_ERR(un)) {
1845 error = PTR_ERR(un);
1846 goto out_free;
1847 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001848 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001850 rcu_read_lock();
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001853 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001854 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001855 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001856 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001858 }
1859
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001860 error = -EFBIG;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001861 if (max >= sma->sem_nsems) {
1862 rcu_read_unlock();
1863 goto out_free;
1864 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001865
1866 error = -EACCES;
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001867 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) {
1868 rcu_read_unlock();
1869 goto out_free;
1870 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001871
1872 error = security_sem_semop(sma, sops, nsops, alter);
Davidlohr Bueso248e7352016-12-14 15:06:31 -08001873 if (error) {
1874 rcu_read_unlock();
1875 goto out_free;
1876 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001877
Manfred Spraul6e224f92013-10-16 13:46:45 -07001878 error = -EIDRM;
1879 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001880 /*
1881 * We eventually might perform the following check in a lockless
1882 * fashion, considering ipc_valid_object() locking constraints.
1883 * If nsops == 1 and there is no contention for sem_perm.lock, then
1884 * only a per-semaphore lock is held and it's OK to proceed with the
1885 * check below. More details on the fine grained locking scheme
1886 * entangled here and why it's RMID race safe on comments at sem_lock()
1887 */
1888 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001889 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001891 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001893 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001894 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001895 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001897 if (un && un->semid == -1)
1898 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001899
Manfred Sprauld198cd62014-06-06 14:37:49 -07001900 queue.sops = sops;
1901 queue.nsops = nsops;
1902 queue.undo = un;
1903 queue.pid = task_tgid_vnr(current);
1904 queue.alter = alter;
Davidlohr Bueso4ce33ec2016-12-14 15:06:37 -08001905 queue.dupsop = dupsop;
Manfred Sprauld198cd62014-06-06 14:37:49 -07001906
1907 error = perform_atomic_semop(sma, &queue);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001908 if (error == 0) { /* non-blocking succesfull path */
1909 DEFINE_WAKE_Q(wake_q);
1910
1911 /*
1912 * If the operation was successful, then do
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001913 * the required updates.
1914 */
1915 if (alter)
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001916 do_smart_update(sma, sops, nsops, 1, &wake_q);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001917 else
1918 set_semotime(sma, sops);
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001919
1920 sem_unlock(sma, locknum);
1921 rcu_read_unlock();
1922 wake_up_q(&wake_q);
1923
1924 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001926 if (error < 0) /* non-blocking error path */
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001927 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08001929 /*
1930 * We need to sleep on this operation, so we put the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * task into the pending queue and go to sleep.
1932 */
Manfred Spraulb97e8202009-12-15 16:47:32 -08001933 if (nsops == 1) {
1934 struct sem *curr;
1935 curr = &sma->sem_base[sops->sem_num];
1936
Manfred Spraulf269f402013-07-08 16:01:24 -07001937 if (alter) {
1938 if (sma->complex_count) {
1939 list_add_tail(&queue.list,
1940 &sma->pending_alter);
1941 } else {
1942
1943 list_add_tail(&queue.list,
1944 &curr->pending_alter);
1945 }
1946 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001947 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001948 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001949 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001950 if (!sma->complex_count)
1951 merge_queues(sma);
1952
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001953 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001954 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001955 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001956 list_add_tail(&queue.list, &sma->pending_const);
1957
Manfred Spraulb97e8202009-12-15 16:47:32 -08001958 sma->complex_count++;
1959 }
1960
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001961 do {
1962 queue.status = -EINTR;
1963 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001964
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001965 __set_current_state(TASK_INTERRUPTIBLE);
1966 sem_unlock(sma, locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -07001967 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001969 if (timeout)
1970 jiffies_left = schedule_timeout(jiffies_left);
1971 else
1972 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001974 /*
1975 * fastpath: the semop has completed, either successfully or
1976 * not, from the syscall pov, is quite irrelevant to us at this
1977 * point; we're done.
1978 *
1979 * We _do_ care, nonetheless, about being awoken by a signal or
1980 * spuriously. The queue.status is checked again in the
1981 * slowpath (aka after taking sem_lock), such that we can detect
1982 * scenarios where we were awakened externally, during the
1983 * window between wake_q_add() and wake_up_q().
1984 */
1985 error = READ_ONCE(queue.status);
1986 if (error != -EINTR) {
1987 /*
1988 * User space could assume that semop() is a memory
1989 * barrier: Without the mb(), the cpu could
1990 * speculatively read in userspace stale data that was
1991 * overwritten by the previous owner of the semaphore.
1992 */
1993 smp_mb();
1994 goto out_free;
1995 }
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001996
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001997 rcu_read_lock();
Manfred Spraulc626bc42017-01-10 16:57:48 -08001998 locknum = sem_lock(sma, sops, nsops);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08001999
Davidlohr Bueso370b2622016-12-14 15:06:49 -08002000 if (!ipc_valid_object(&sma->sem_perm))
2001 goto out_unlock_free;
2002
2003 error = READ_ONCE(queue.status);
Davidlohr Buesob5fa01a2016-12-14 15:06:46 -08002004
2005 /*
2006 * If queue.status != -EINTR we are woken up by another process.
2007 * Leave without unlink_queue(), but with sem_unlock().
2008 */
2009 if (error != -EINTR)
2010 goto out_unlock_free;
2011
2012 /*
2013 * If an interrupt occurred we have to clean up the queue.
2014 */
2015 if (timeout && jiffies_left == 0)
2016 error = -EAGAIN;
2017 } while (error == -EINTR && !signal_pending(current)); /* spurious */
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002018
Manfred Spraulb97e8202009-12-15 16:47:32 -08002019 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002022 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002023 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002025 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 kfree(sops);
2027 return error;
2028}
2029
Heiko Carstensd5460c92009-01-14 14:14:27 +01002030SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2031 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 return sys_semtimedop(semid, tsops, nsops, NULL);
2034}
2035
2036/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2037 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 */
2039
2040int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2041{
2042 struct sem_undo_list *undo_list;
2043 int error;
2044
2045 if (clone_flags & CLONE_SYSVSEM) {
2046 error = get_undo_list(&undo_list);
2047 if (error)
2048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 atomic_inc(&undo_list->refcnt);
2050 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002051 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 tsk->sysvsem.undo_list = NULL;
2053
2054 return 0;
2055}
2056
2057/*
2058 * add semadj values to semaphores, free undo structures.
2059 * undo structures are not freed when semaphore arrays are destroyed
2060 * so some of them may be out of date.
2061 * IMPLEMENTATION NOTE: There is some confusion over whether the
2062 * set of adjustments that needs to be done should be done in an atomic
2063 * manner or not. That is, if we are attempting to decrement the semval
2064 * should we queue up and wait until we can do so legally?
2065 * The original implementation attempted to do this (queue and wait).
2066 * The current implementation does not do so. The POSIX standard
2067 * and SVID should be consulted to determine what behavior is mandated.
2068 */
2069void exit_sem(struct task_struct *tsk)
2070{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002071 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002073 ulp = tsk->sysvsem.undo_list;
2074 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002076 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002078 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return;
2080
Manfred Spraul380af1b2008-07-25 01:48:06 -07002081 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002083 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002084 int semid, i;
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002085 DEFINE_WAKE_Q(wake_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Nikolay Borisov2a1613a2016-10-11 13:55:05 -07002087 cond_resched();
2088
Manfred Spraul380af1b2008-07-25 01:48:06 -07002089 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002090 un = list_entry_rcu(ulp->list_proc.next,
2091 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002092 if (&un->list_proc == &ulp->list_proc) {
2093 /*
2094 * We must wait for freeary() before freeing this ulp,
2095 * in case we raced with last sem_undo. There is a small
2096 * possibility where we exit while freeary() didn't
2097 * finish unlocking sem_undo_list.
2098 */
2099 spin_unlock_wait(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002100 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002101 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002102 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002103 spin_lock(&ulp->lock);
2104 semid = un->semid;
2105 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002106
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002107 /* exit_sem raced with IPC_RMID, nothing to do */
2108 if (semid == -1) {
2109 rcu_read_unlock();
2110 continue;
2111 }
2112
2113 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002114 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002115 if (IS_ERR(sma)) {
2116 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002117 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Rik van Riel6062a8d2013-04-30 19:15:44 -07002120 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002121 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002122 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002123 sem_unlock(sma, -1);
2124 rcu_read_unlock();
2125 continue;
2126 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002127 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002128 if (un == NULL) {
2129 /* exit_sem raced with IPC_RMID+semget() that created
2130 * exactly the same semid. Nothing to do.
2131 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002132 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002133 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002134 continue;
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Manfred Spraul380af1b2008-07-25 01:48:06 -07002137 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002138 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002139 list_del(&un->list_id);
2140
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002141 /* we are the last process using this ulp, acquiring ulp->lock
2142 * isn't required. Besides that, we are also protected against
2143 * IPC_RMID as we hold sma->sem_perm lock now
2144 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002145 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002146
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002147 /* perform adjustments registered in un */
2148 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul239521f2014-01-27 17:07:04 -08002149 struct sem *semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002150 if (un->semadj[i]) {
2151 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 /*
2153 * Range checks of the new semaphore value,
2154 * not defined by sus:
2155 * - Some unices ignore the undo entirely
2156 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2157 * - some cap the value (e.g. FreeBSD caps
2158 * at 0, but doesn't enforce SEMVMX)
2159 *
2160 * Linux caps the semaphore value, both at 0
2161 * and at SEMVMX.
2162 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002163 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002165 if (semaphore->semval < 0)
2166 semaphore->semval = 0;
2167 if (semaphore->semval > SEMVMX)
2168 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002169 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* maybe some queued-up processes were waiting for this */
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002173 do_smart_update(sma, NULL, 0, 1, &wake_q);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002174 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002175 rcu_read_unlock();
Davidlohr Bueso9ae949f2016-12-14 15:06:34 -08002176 wake_up_q(&wake_q);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002177
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002178 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002180 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002184static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002186 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002187 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002188 time_t sem_otime;
2189
Manfred Sprauld8c63372013-09-30 13:45:07 -07002190 /*
2191 * The proc interface isn't aware of sem_lock(), it calls
2192 * ipc_lock_object() directly (in sysvipc_find_ipc).
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002193 * In order to stay compatible with sem_lock(), we must
2194 * enter / leave complex_mode.
Manfred Sprauld8c63372013-09-30 13:45:07 -07002195 */
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002196 complexmode_enter(sma);
Manfred Sprauld8c63372013-09-30 13:45:07 -07002197
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002198 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Joe Perches7f032d62015-04-15 16:17:54 -07002200 seq_printf(s,
2201 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2202 sma->sem_perm.key,
2203 sma->sem_perm.id,
2204 sma->sem_perm.mode,
2205 sma->sem_nsems,
2206 from_kuid_munged(user_ns, sma->sem_perm.uid),
2207 from_kgid_munged(user_ns, sma->sem_perm.gid),
2208 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2209 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2210 sem_otime,
2211 sma->sem_ctime);
2212
Manfred Spraul5864a2f2016-10-11 13:54:50 -07002213 complexmode_tryleave(sma);
2214
Joe Perches7f032d62015-04-15 16:17:54 -07002215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217#endif