blob: 84dff3df11a4ab8088cf6577588a6866e784a03b [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>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070014 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
Steve Grubb073115d2006-04-02 17:07:33 -040016 *
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
Kirill Korotaeve3893532006-10-02 02:18:22 -070019 *
20 * namespaces support
21 * OpenVZ, SWsoft Inc.
22 * Pavel Emelianov <xemul@openvz.org>
Manfred Spraulc5cf6352010-05-26 14:43:43 -070023 *
24 * Implementation notes: (May 2010)
25 * This file implements System V semaphores.
26 *
27 * User space visible behavior:
28 * - FIFO ordering for semop() operations (just FIFO, not starvation
29 * protection)
30 * - multiple semaphore operations that alter the same semaphore in
31 * one semop() are handled.
32 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
33 * SETALL calls.
34 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
35 * - undo adjustments at process exit are limited to 0..SEMVMX.
36 * - namespace are supported.
37 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
38 * to /proc/sys/kernel/sem.
39 * - statistics about the usage are reported in /proc/sysvipc/sem.
40 *
41 * Internals:
42 * - scalability:
43 * - all global variables are read-mostly.
44 * - semop() calls and semctl(RMID) are synchronized by RCU.
45 * - most operations do write operations (actually: spin_lock calls) to
46 * the per-semaphore array structure.
47 * Thus: Perfect SMP scaling between independent semaphore arrays.
48 * If multiple semaphores in one array are used, then cache line
49 * trashing on the semaphore array spinlock will limit the scaling.
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -070050 * - semncnt and semzcnt are calculated on demand in count_semcnt()
Manfred Spraulc5cf6352010-05-26 14:43:43 -070051 * - the task that performs a successful semop() scans the list of all
52 * sleeping tasks and completes any pending operations that can be fulfilled.
53 * Semaphores are actively given to waiting tasks (necessary for FIFO).
54 * (see update_queue())
55 * - To improve the scalability, the actual wake-up calls are performed after
56 * dropping all locks. (see wake_up_sem_queue_prepare(),
57 * wake_up_sem_queue_do())
58 * - All work is done by the waker, the woken up task does not have to do
59 * anything - not even acquiring a lock or dropping a refcount.
60 * - A woken up task may not even touch the semaphore array anymore, it may
61 * have been destroyed already by a semctl(RMID).
62 * - The synchronizations between wake-ups due to a timeout/signal and a
63 * wake-up due to a completed semaphore operation is achieved by using an
64 * intermediate state (IN_WAKEUP).
65 * - UNDO values are stored in an array (one per process and per
66 * semaphore array, lazily allocated). For backwards compatibility, multiple
67 * modes for the UNDO variables are supported (per process, per thread)
68 * (see copy_semundo, CLONE_SYSVSEM)
69 * - There are two lists of the pending operations: a per-array list
70 * and per-semaphore list (stored in the array). This allows to achieve FIFO
71 * ordering without always scanning all pending operations.
72 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/slab.h>
76#include <linux/spinlock.h>
77#include <linux/init.h>
78#include <linux/proc_fs.h>
79#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/security.h>
81#include <linux/syscalls.h>
82#include <linux/audit.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080083#include <linux/capability.h>
Mike Waychison19b49462005-09-06 15:17:10 -070084#include <linux/seq_file.h>
Nadia Derbey3e148c72007-10-18 23:40:54 -070085#include <linux/rwsem.h>
Kirill Korotaeve3893532006-10-02 02:18:22 -070086#include <linux/nsproxy.h>
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080087#include <linux/ipc_namespace.h>
Ingo Molnar5f921ae2006-03-26 01:37:17 -080088
Paul McQuade7153e402014-06-06 14:37:37 -070089#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include "util.h"
91
Manfred Spraule57940d2011-11-02 13:38:54 -070092/* One semaphore structure for each semaphore in the system. */
93struct sem {
94 int semval; /* current value */
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -070095 /*
96 * PID of the process that last modified the semaphore. For
97 * Linux, specifically these are:
98 * - semop
99 * - semctl, via SETVAL and SETALL.
100 * - at task exit when performing undo adjustments (see exit_sem).
101 */
102 int sempid;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700103 spinlock_t lock; /* spinlock for fine-grained semtimedop */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700104 struct list_head pending_alter; /* pending single-sop operations */
105 /* that alter the semaphore */
106 struct list_head pending_const; /* pending single-sop operations */
107 /* that do not alter the semaphore*/
Manfred Sprauld12e1e52013-07-08 16:01:25 -0700108 time_t sem_otime; /* candidate for sem_otime */
Manfred Spraulf5c936c2013-07-08 16:01:22 -0700109} ____cacheline_aligned_in_smp;
Manfred Spraule57940d2011-11-02 13:38:54 -0700110
111/* One queue for each sleeping process in the system. */
112struct sem_queue {
Manfred Spraule57940d2011-11-02 13:38:54 -0700113 struct list_head list; /* queue of pending operations */
114 struct task_struct *sleeper; /* this process */
115 struct sem_undo *undo; /* undo structure */
116 int pid; /* process id of requesting process */
117 int status; /* completion status of operation */
118 struct sembuf *sops; /* array of pending operations */
Manfred Sprauled247b72014-06-06 14:37:49 -0700119 struct sembuf *blocking; /* the operation that blocked */
Manfred Spraule57940d2011-11-02 13:38:54 -0700120 int nsops; /* number of operations */
121 int alter; /* does *sops alter the array? */
122};
123
124/* Each task has a list of undo requests. They are executed automatically
125 * when the process exits.
126 */
127struct sem_undo {
128 struct list_head list_proc; /* per-process list: *
129 * all undos from one process
130 * rcu protected */
131 struct rcu_head rcu; /* rcu struct for sem_undo */
132 struct sem_undo_list *ulp; /* back ptr to sem_undo_list */
133 struct list_head list_id; /* per semaphore array list:
134 * all undos for one array */
135 int semid; /* semaphore set identifier */
136 short *semadj; /* array of adjustments */
137 /* one per semaphore */
138};
139
140/* sem_undo_list controls shared access to the list of sem_undo structures
141 * that may be shared among all a CLONE_SYSVSEM task group.
142 */
143struct sem_undo_list {
144 atomic_t refcnt;
145 spinlock_t lock;
146 struct list_head list_proc;
147};
148
149
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800150#define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Nadia Derbey1b531f22007-10-18 23:40:55 -0700152#define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700154static int newary(struct ipc_namespace *, struct ipc_params *);
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800155static void freeary(struct ipc_namespace *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -0700157static int sysvipc_sem_proc_show(struct seq_file *s, void *it);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#endif
159
160#define SEMMSL_FAST 256 /* 512 bytes on stack */
161#define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
162
163/*
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700164 * Locking:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * sem_undo.id_next,
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700166 * sem_array.complex_count,
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700167 * sem_array.pending{_alter,_cont},
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700168 * sem_array.sem_undo: global sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * sem_undo.proc_next: only "current" is allowed to read/write that field.
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700170 *
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700171 * sem_array.sem_base[i].pending_{const,alter}:
172 * global or semaphore sem_lock() for read/write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174
Kirill Korotaeve3893532006-10-02 02:18:22 -0700175#define sc_semmsl sem_ctls[0]
176#define sc_semmns sem_ctls[1]
177#define sc_semopm sem_ctls[2]
178#define sc_semmni sem_ctls[3]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800180void sem_init_ns(struct ipc_namespace *ns)
Kirill Korotaeve3893532006-10-02 02:18:22 -0700181{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700182 ns->sc_semmsl = SEMMSL;
183 ns->sc_semmns = SEMMNS;
184 ns->sc_semopm = SEMOPM;
185 ns->sc_semmni = SEMMNI;
186 ns->used_sems = 0;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800187 ipc_init_ids(&ns->ids[IPC_SEM_IDS]);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700188}
189
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800190#ifdef CONFIG_IPC_NS
Kirill Korotaeve3893532006-10-02 02:18:22 -0700191void sem_exit_ns(struct ipc_namespace *ns)
192{
Pierre Peiffer01b8b072008-02-08 04:18:57 -0800193 free_ipcs(ns, &sem_ids(ns), freeary);
Serge E. Hallyn7d6feeb2009-12-15 16:47:27 -0800194 idr_destroy(&ns->ids[IPC_SEM_IDS].ipcs_idr);
Kirill Korotaeve3893532006-10-02 02:18:22 -0700195}
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -0800196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Manfred Spraul239521f2014-01-27 17:07:04 -0800198void __init sem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Pierre Peiffered2ddbf2008-02-08 04:18:57 -0800200 sem_init_ns(&init_ipc_ns);
Mike Waychison19b49462005-09-06 15:17:10 -0700201 ipc_init_proc_interface("sysvipc/sem",
202 " key semid perms nsems uid gid cuid cgid otime ctime\n",
Kirill Korotaeve3893532006-10-02 02:18:22 -0700203 IPC_SEM_IDS, sysvipc_sem_proc_show);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Manfred Spraulf269f402013-07-08 16:01:24 -0700206/**
207 * unmerge_queues - unmerge queues, if possible.
208 * @sma: semaphore array
209 *
210 * The function unmerges the wait queues if complex_count is 0.
211 * It must be called prior to dropping the global semaphore array lock.
212 */
213static void unmerge_queues(struct sem_array *sma)
214{
215 struct sem_queue *q, *tq;
216
217 /* complex operations still around? */
218 if (sma->complex_count)
219 return;
220 /*
221 * We will switch back to simple mode.
222 * Move all pending operation back into the per-semaphore
223 * queues.
224 */
225 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
226 struct sem *curr;
227 curr = &sma->sem_base[q->sops[0].sem_num];
228
229 list_add_tail(&q->list, &curr->pending_alter);
230 }
231 INIT_LIST_HEAD(&sma->pending_alter);
232}
233
234/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800235 * merge_queues - merge single semop queues into global queue
Manfred Spraulf269f402013-07-08 16:01:24 -0700236 * @sma: semaphore array
237 *
238 * This function merges all per-semaphore queues into the global queue.
239 * It is necessary to achieve FIFO ordering for the pending single-sop
240 * operations when a multi-semop operation must sleep.
241 * Only the alter operations must be moved, the const operations can stay.
242 */
243static void merge_queues(struct sem_array *sma)
244{
245 int i;
246 for (i = 0; i < sma->sem_nsems; i++) {
247 struct sem *sem = sma->sem_base + i;
248
249 list_splice_init(&sem->pending_alter, &sma->pending_alter);
250 }
251}
252
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700253static void sem_rcu_free(struct rcu_head *head)
254{
255 struct ipc_rcu *p = container_of(head, struct ipc_rcu, rcu);
256 struct sem_array *sma = ipc_rcu_to_struct(p);
257
258 security_sem_free(sma);
259 ipc_rcu_free(head);
260}
261
Nadia Derbey3e148c72007-10-18 23:40:54 -0700262/*
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700263 * Wait until all currently ongoing simple ops have completed.
264 * Caller must own sem_perm.lock.
265 * New simple ops cannot start, because simple ops first check
266 * that sem_perm.lock is free.
Manfred Spraul6d07b682013-09-30 13:45:06 -0700267 * that a) sem_perm.lock is free and b) complex_count is 0.
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700268 */
269static void sem_wait_array(struct sem_array *sma)
270{
271 int i;
272 struct sem *sem;
273
Manfred Spraul6d07b682013-09-30 13:45:06 -0700274 if (sma->complex_count) {
275 /* The thread that increased sma->complex_count waited on
276 * all sem->lock locks. Thus we don't need to wait again.
277 */
278 return;
279 }
280
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700281 for (i = 0; i < sma->sem_nsems; i++) {
282 sem = sma->sem_base + i;
283 spin_unlock_wait(&sem->lock);
284 }
Peter Zijlstra33ac2792016-05-24 13:17:12 +0200285 smp_acquire__after_ctrl_dep();
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700286}
287
288/*
Rik van Riel6062a8d2013-04-30 19:15:44 -0700289 * If the request contains only one semaphore operation, and there are
290 * no complex transactions pending, lock only the semaphore involved.
291 * Otherwise, lock the entire semaphore array, since we either have
292 * multiple semaphores in our own semops, or we need to look at
293 * semaphores from other pending complex operations.
Rik van Riel6062a8d2013-04-30 19:15:44 -0700294 */
295static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
296 int nsops)
297{
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700298 struct sem *sem;
Rik van Riel6062a8d2013-04-30 19:15:44 -0700299
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700300 if (nsops != 1) {
301 /* Complex operation - acquire a full lock */
302 ipc_lock_object(&sma->sem_perm);
303
304 /* And wait until all simple ops that are processed
305 * right now have dropped their locks.
306 */
307 sem_wait_array(sma);
308 return -1;
309 }
310
311 /*
312 * Only one semaphore affected - try to optimize locking.
313 * The rules are:
314 * - optimized locking is possible if no complex operation
315 * is either enqueued or processed right now.
316 * - The test for enqueued complex ops is simple:
317 * sma->complex_count != 0
318 * - Testing for complex ops that are processed right now is
319 * a bit more difficult. Complex ops acquire the full lock
320 * and first wait that the running simple ops have completed.
321 * (see above)
322 * Thus: If we own a simple lock and the global lock is free
323 * and complex_count is now 0, then it will stay 0 and
324 * thus just locking sem->lock is sufficient.
325 */
326 sem = sma->sem_base + sops->sem_num;
327
328 if (sma->complex_count == 0) {
329 /*
330 * It appears that no complex operation is around.
331 * Acquire the per-semaphore lock.
332 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700333 spin_lock(&sem->lock);
334
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700335 /* Then check that the global lock is free */
336 if (!spin_is_locked(&sma->sem_perm.lock)) {
Manfred Spraul2e094ab2014-12-12 16:58:11 -0800337 /*
Manfred Spraul3ed1f8a2015-08-14 15:35:10 -0700338 * We need a memory barrier with acquire semantics,
339 * otherwise we can race with another thread that does:
Manfred Spraul2e094ab2014-12-12 16:58:11 -0800340 * complex_count++;
341 * spin_unlock(sem_perm.lock);
342 */
Peter Zijlstra33ac2792016-05-24 13:17:12 +0200343 smp_acquire__after_ctrl_dep();
Rik van Riel6062a8d2013-04-30 19:15:44 -0700344
Manfred Spraul2e094ab2014-12-12 16:58:11 -0800345 /*
346 * Now repeat the test of complex_count:
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700347 * It can't change anymore until we drop sem->lock.
348 * Thus: if is now 0, then it will stay 0.
349 */
350 if (sma->complex_count == 0) {
351 /* fast path successful! */
352 return sops->sem_num;
353 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700354 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700355 spin_unlock(&sem->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700356 }
Manfred Spraul5e9d5272013-09-30 13:45:04 -0700357
358 /* slow path: acquire the full lock */
359 ipc_lock_object(&sma->sem_perm);
360
361 if (sma->complex_count == 0) {
362 /* False alarm:
363 * There is no complex operation, thus we can switch
364 * back to the fast path.
365 */
366 spin_lock(&sem->lock);
367 ipc_unlock_object(&sma->sem_perm);
368 return sops->sem_num;
369 } else {
370 /* Not a false alarm, thus complete the sequence for a
371 * full lock.
372 */
373 sem_wait_array(sma);
374 return -1;
375 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700376}
377
378static inline void sem_unlock(struct sem_array *sma, int locknum)
379{
380 if (locknum == -1) {
Manfred Spraulf269f402013-07-08 16:01:24 -0700381 unmerge_queues(sma);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700382 ipc_unlock_object(&sma->sem_perm);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700383 } else {
384 struct sem *sem = sma->sem_base + locknum;
385 spin_unlock(&sem->lock);
386 }
Rik van Riel6062a8d2013-04-30 19:15:44 -0700387}
388
389/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700390 * sem_lock_(check_) routines are called in the paths where the rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -0700391 * is not held.
Linus Torvalds321310c2013-05-04 10:47:57 -0700392 *
393 * The caller holds the RCU read lock.
Nadia Derbey3e148c72007-10-18 23:40:54 -0700394 */
Rik van Riel6062a8d2013-04-30 19:15:44 -0700395static inline struct sem_array *sem_obtain_lock(struct ipc_namespace *ns,
396 int id, struct sembuf *sops, int nsops, int *locknum)
Nadia Derbey023a5352007-10-18 23:40:51 -0700397{
Rik van Rielc460b662013-04-30 19:15:35 -0700398 struct kern_ipc_perm *ipcp;
399 struct sem_array *sma;
Nadia Derbey03f02c72007-10-18 23:40:51 -0700400
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700401 ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Linus Torvalds321310c2013-05-04 10:47:57 -0700402 if (IS_ERR(ipcp))
403 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800404
Rik van Riel6062a8d2013-04-30 19:15:44 -0700405 sma = container_of(ipcp, struct sem_array, sem_perm);
406 *locknum = sem_lock(sma, sops, nsops);
Rik van Rielc460b662013-04-30 19:15:35 -0700407
408 /* ipc_rmid() may have already freed the ID while sem_lock
409 * was spinning: verify that the structure is still valid
410 */
Rafael Aquini72a8ff22014-01-27 17:07:02 -0800411 if (ipc_valid_object(ipcp))
Rik van Rielc460b662013-04-30 19:15:35 -0700412 return container_of(ipcp, struct sem_array, sem_perm);
413
Rik van Riel6062a8d2013-04-30 19:15:44 -0700414 sem_unlock(sma, *locknum);
Linus Torvalds321310c2013-05-04 10:47:57 -0700415 return ERR_PTR(-EINVAL);
Nadia Derbey023a5352007-10-18 23:40:51 -0700416}
417
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700418static inline struct sem_array *sem_obtain_object(struct ipc_namespace *ns, int id)
419{
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700420 struct kern_ipc_perm *ipcp = ipc_obtain_object_idr(&sem_ids(ns), id);
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700421
422 if (IS_ERR(ipcp))
423 return ERR_CAST(ipcp);
424
425 return container_of(ipcp, struct sem_array, sem_perm);
426}
427
Davidlohr Bueso16df3672013-04-30 19:15:29 -0700428static inline struct sem_array *sem_obtain_object_check(struct ipc_namespace *ns,
429 int id)
430{
431 struct kern_ipc_perm *ipcp = ipc_obtain_object_check(&sem_ids(ns), id);
432
433 if (IS_ERR(ipcp))
434 return ERR_CAST(ipcp);
Pierre Peifferb1ed88b2008-02-06 01:36:23 -0800435
Nadia Derbey03f02c72007-10-18 23:40:51 -0700436 return container_of(ipcp, struct sem_array, sem_perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700437}
438
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700439static inline void sem_lock_and_putref(struct sem_array *sma)
440{
Rik van Riel6062a8d2013-04-30 19:15:44 -0700441 sem_lock(sma, NULL, -1);
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700442 ipc_rcu_putref(sma, ipc_rcu_free);
Pierre Peiffer6ff37972008-04-29 01:00:46 -0700443}
444
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700445static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
446{
447 ipc_rmid(&sem_ids(ns), &s->sem_perm);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/*
451 * Lockless wakeup algorithm:
452 * Without the check/retry algorithm a lockless wakeup is possible:
453 * - queue.status is initialized to -EINTR before blocking.
454 * - wakeup is performed by
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700455 * * unlinking the queue entry from the pending list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * * setting queue.status to IN_WAKEUP
457 * This is the notification for the blocked thread that a
458 * result value is imminent.
459 * * call wake_up_process
460 * * set queue.status to the final value.
461 * - the previously blocked thread checks queue.status:
Manfred Spraul239521f2014-01-27 17:07:04 -0800462 * * if it's IN_WAKEUP, then it must wait until the value changes
463 * * if it's not -EINTR, then the operation was completed by
464 * update_queue. semtimedop can return queue.status without
465 * performing any operation on the sem array.
466 * * otherwise it must acquire the spinlock and check what's up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 *
468 * The two-stage algorithm is necessary to protect against the following
469 * races:
470 * - if queue.status is set after wake_up_process, then the woken up idle
471 * thread could race forward and try (and fail) to acquire sma->lock
472 * before update_queue had a chance to set queue.status
473 * - if queue.status is written before wake_up_process and if the
474 * blocked process is woken up by a signal between writing
475 * queue.status and the wake_up_process, then the woken up
476 * process could return from semtimedop and die by calling
477 * sys_exit before wake_up_process is called. Then wake_up_process
478 * will oops, because the task structure is already invalid.
479 * (yes, this happened on s390 with sysv msg).
480 *
481 */
482#define IN_WAKEUP 1
483
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700484/**
485 * newary - Create a new semaphore set
486 * @ns: namespace
487 * @params: ptr to the structure that contains key, semflg and nsems
488 *
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700489 * Called with sem_ids.rwsem held (as a writer)
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700490 */
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700491static int newary(struct ipc_namespace *ns, struct ipc_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int id;
494 int retval;
495 struct sem_array *sma;
496 int size;
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700497 key_t key = params->key;
498 int nsems = params->u.nsems;
499 int semflg = params->flg;
Manfred Spraulb97e8202009-12-15 16:47:32 -0800500 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 if (!nsems)
503 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -0700504 if (ns->used_sems + nsems > ns->sc_semmns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -ENOSPC;
506
Manfred Spraul239521f2014-01-27 17:07:04 -0800507 size = sizeof(*sma) + nsems * sizeof(struct sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 sma = ipc_rcu_alloc(size);
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800509 if (!sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -ENOMEM;
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -0800511
Manfred Spraul239521f2014-01-27 17:07:04 -0800512 memset(sma, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 sma->sem_perm.mode = (semflg & S_IRWXUGO);
515 sma->sem_perm.key = key;
516
517 sma->sem_perm.security = NULL;
518 retval = security_sem_alloc(sma);
519 if (retval) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -0700520 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return retval;
522 }
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 sma->sem_base = (struct sem *) &sma[1];
Manfred Spraulb97e8202009-12-15 16:47:32 -0800525
Rik van Riel6062a8d2013-04-30 19:15:44 -0700526 for (i = 0; i < nsems; i++) {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700527 INIT_LIST_HEAD(&sma->sem_base[i].pending_alter);
528 INIT_LIST_HEAD(&sma->sem_base[i].pending_const);
Rik van Riel6062a8d2013-04-30 19:15:44 -0700529 spin_lock_init(&sma->sem_base[i].lock);
530 }
Manfred Spraulb97e8202009-12-15 16:47:32 -0800531
532 sma->complex_count = 0;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700533 INIT_LIST_HEAD(&sma->pending_alter);
534 INIT_LIST_HEAD(&sma->pending_const);
Manfred Spraul4daa28f2008-07-25 01:48:04 -0700535 INIT_LIST_HEAD(&sma->list_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sma->sem_nsems = nsems;
537 sma->sem_ctime = get_seconds();
Manfred Spraule8577d12014-12-02 15:59:34 -0800538
539 id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni);
540 if (id < 0) {
541 ipc_rcu_putref(sma, sem_rcu_free);
542 return id;
543 }
544 ns->used_sems += nsems;
545
Rik van Riel6062a8d2013-04-30 19:15:44 -0700546 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -0700547 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700549 return sma->sem_perm.id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700552
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700553/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700554 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700555 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700556static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700558 struct sem_array *sma;
559
560 sma = container_of(ipcp, struct sem_array, sem_perm);
561 return security_sem_associate(sma, semflg);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700562}
563
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700564/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700565 * Called with sem_ids.rwsem and ipcp locked.
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700566 */
Nadia Derbey03f02c72007-10-18 23:40:51 -0700567static inline int sem_more_checks(struct kern_ipc_perm *ipcp,
568 struct ipc_params *params)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700569{
Nadia Derbey03f02c72007-10-18 23:40:51 -0700570 struct sem_array *sma;
571
572 sma = container_of(ipcp, struct sem_array, sem_perm);
573 if (params->u.nsems > sma->sem_nsems)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700574 return -EINVAL;
575
576 return 0;
577}
578
Heiko Carstensd5460c92009-01-14 14:14:27 +0100579SYSCALL_DEFINE3(semget, key_t, key, int, nsems, int, semflg)
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700580{
Kirill Korotaeve3893532006-10-02 02:18:22 -0700581 struct ipc_namespace *ns;
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700582 static const struct ipc_ops sem_ops = {
583 .getnew = newary,
584 .associate = sem_security,
585 .more_checks = sem_more_checks,
586 };
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700587 struct ipc_params sem_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Kirill Korotaeve3893532006-10-02 02:18:22 -0700589 ns = current->nsproxy->ipc_ns;
590
591 if (nsems < 0 || nsems > ns->sc_semmsl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return -EINVAL;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700593
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700594 sem_params.key = key;
595 sem_params.flg = semflg;
596 sem_params.u.nsems = nsems;
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700597
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700598 return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Petr Mladek78f50092014-01-27 17:07:00 -0800601/**
602 * perform_atomic_semop - Perform (if possible) a semaphore operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700603 * @sma: semaphore array
Manfred Sprauld198cd62014-06-06 14:37:49 -0700604 * @q: struct sem_queue that describes the operation
Manfred Spraul758a6ba32013-07-08 16:01:26 -0700605 *
606 * Returns 0 if the operation was possible.
607 * Returns 1 if the operation is impossible, the caller must sleep.
608 * Negative values are error codes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Manfred Sprauld198cd62014-06-06 14:37:49 -0700610static int perform_atomic_semop(struct sem_array *sma, struct sem_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Manfred Sprauld198cd62014-06-06 14:37:49 -0700612 int result, sem_op, nsops, pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct sembuf *sop;
Manfred Spraul239521f2014-01-27 17:07:04 -0800614 struct sem *curr;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700615 struct sembuf *sops;
616 struct sem_undo *un;
617
618 sops = q->sops;
619 nsops = q->nsops;
620 un = q->undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 for (sop = sops; sop < sops + nsops; sop++) {
623 curr = sma->sem_base + sop->sem_num;
624 sem_op = sop->sem_op;
625 result = curr->semval;
Petr Mladek78f50092014-01-27 17:07:00 -0800626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (!sem_op && result)
628 goto would_block;
629
630 result += sem_op;
631 if (result < 0)
632 goto would_block;
633 if (result > SEMVMX)
634 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (sop->sem_flg & SEM_UNDO) {
637 int undo = un->semadj[sop->sem_num] - sem_op;
Petr Mladek78f50092014-01-27 17:07:00 -0800638 /* Exceeding the undo range is an error. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
640 goto out_of_range;
Petr Mladek78f50092014-01-27 17:07:00 -0800641 un->semadj[sop->sem_num] = undo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Petr Mladek78f50092014-01-27 17:07:00 -0800643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 curr->semval = result;
645 }
646
647 sop--;
Manfred Sprauld198cd62014-06-06 14:37:49 -0700648 pid = q->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 while (sop >= sops) {
650 sma->sem_base[sop->sem_num].sempid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 sop--;
652 }
Petr Mladek78f50092014-01-27 17:07:00 -0800653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655
656out_of_range:
657 result = -ERANGE;
658 goto undo;
659
660would_block:
Manfred Sprauled247b72014-06-06 14:37:49 -0700661 q->blocking = sop;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (sop->sem_flg & IPC_NOWAIT)
664 result = -EAGAIN;
665 else
666 result = 1;
667
668undo:
669 sop--;
670 while (sop >= sops) {
Petr Mladek78f50092014-01-27 17:07:00 -0800671 sem_op = sop->sem_op;
672 sma->sem_base[sop->sem_num].semval -= sem_op;
673 if (sop->sem_flg & SEM_UNDO)
674 un->semadj[sop->sem_num] += sem_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sop--;
676 }
677
678 return result;
679}
680
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700681/** wake_up_sem_queue_prepare(q, error): Prepare wake-up
682 * @q: queue entry that must be signaled
683 * @error: Error value for the signal
684 *
685 * Prepare the wake-up of the queue entry q.
Nick Piggind4212092009-12-15 16:47:30 -0800686 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700687static void wake_up_sem_queue_prepare(struct list_head *pt,
688 struct sem_queue *q, int error)
Nick Piggind4212092009-12-15 16:47:30 -0800689{
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700690 if (list_empty(pt)) {
691 /*
692 * Hold preempt off so that we don't get preempted and have the
693 * wakee busy-wait until we're scheduled back on.
694 */
695 preempt_disable();
696 }
Nick Piggind4212092009-12-15 16:47:30 -0800697 q->status = IN_WAKEUP;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700698 q->pid = error;
699
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700700 list_add_tail(&q->list, pt);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700701}
702
703/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800704 * wake_up_sem_queue_do - do the actual wake-up
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700705 * @pt: list of tasks to be woken up
706 *
707 * Do the actual wake-up.
708 * The function is called without any locks held, thus the semaphore array
709 * could be destroyed already and the tasks can disappear as soon as the
710 * status is set to the actual return code.
711 */
712static void wake_up_sem_queue_do(struct list_head *pt)
713{
714 struct sem_queue *q, *t;
715 int did_something;
716
717 did_something = !list_empty(pt);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700718 list_for_each_entry_safe(q, t, pt, list) {
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700719 wake_up_process(q->sleeper);
720 /* q can disappear immediately after writing q->status. */
721 smp_wmb();
722 q->status = q->pid;
723 }
724 if (did_something)
725 preempt_enable();
Nick Piggind4212092009-12-15 16:47:30 -0800726}
727
Manfred Spraulb97e8202009-12-15 16:47:32 -0800728static void unlink_queue(struct sem_array *sma, struct sem_queue *q)
729{
730 list_del(&q->list);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700731 if (q->nsops > 1)
Manfred Spraulb97e8202009-12-15 16:47:32 -0800732 sma->complex_count--;
733}
734
Manfred Spraulfd5db422010-05-26 14:43:40 -0700735/** check_restart(sma, q)
736 * @sma: semaphore array
737 * @q: the operation that just completed
738 *
739 * update_queue is O(N^2) when it restarts scanning the whole queue of
740 * waiting operations. Therefore this function checks if the restart is
741 * really necessary. It is called after a previously waiting operation
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700742 * modified the array.
743 * Note that wait-for-zero operations are handled without restart.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700744 */
745static int check_restart(struct sem_array *sma, struct sem_queue *q)
746{
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700747 /* pending complex alter operations are too difficult to analyse */
748 if (!list_empty(&sma->pending_alter))
Manfred Spraulfd5db422010-05-26 14:43:40 -0700749 return 1;
750
751 /* we were a sleeping complex operation. Too difficult */
752 if (q->nsops > 1)
753 return 1;
754
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700755 /* It is impossible that someone waits for the new value:
756 * - complex operations always restart.
757 * - wait-for-zero are handled seperately.
758 * - q is a previously sleeping simple operation that
759 * altered the array. It must be a decrement, because
760 * simple increments never sleep.
761 * - If there are older (higher priority) decrements
762 * in the queue, then they have observed the original
763 * semval value and couldn't proceed. The operation
764 * decremented to value - thus they won't proceed either.
765 */
766 return 0;
767}
Manfred Spraulfd5db422010-05-26 14:43:40 -0700768
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700769/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800770 * wake_const_ops - wake up non-alter tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700771 * @sma: semaphore array.
772 * @semnum: semaphore that was modified.
773 * @pt: list head for the tasks that must be woken up.
774 *
775 * wake_const_ops must be called after a semaphore in a semaphore array
776 * was set to 0. If complex const operations are pending, wake_const_ops must
777 * be called with semnum = -1, as well as with the number of each modified
778 * semaphore.
779 * The tasks that must be woken up are added to @pt. The return code
780 * is stored in q->pid.
781 * The function returns 1 if at least one operation was completed successfully.
782 */
783static int wake_const_ops(struct sem_array *sma, int semnum,
784 struct list_head *pt)
785{
786 struct sem_queue *q;
787 struct list_head *walk;
788 struct list_head *pending_list;
789 int semop_completed = 0;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700790
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700791 if (semnum == -1)
792 pending_list = &sma->pending_const;
793 else
794 pending_list = &sma->sem_base[semnum].pending_const;
795
796 walk = pending_list->next;
797 while (walk != pending_list) {
798 int error;
799
800 q = container_of(walk, struct sem_queue, list);
801 walk = walk->next;
802
Manfred Sprauld198cd62014-06-06 14:37:49 -0700803 error = perform_atomic_semop(sma, q);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700804
805 if (error <= 0) {
806 /* operation completed, remove from queue & wakeup */
807
808 unlink_queue(sma, q);
809
810 wake_up_sem_queue_prepare(pt, q, error);
811 if (error == 0)
812 semop_completed = 1;
813 }
814 }
815 return semop_completed;
816}
817
818/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800819 * do_smart_wakeup_zero - wakeup all wait for zero tasks
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700820 * @sma: semaphore array
821 * @sops: operations that were performed
822 * @nsops: number of operations
823 * @pt: list head of the tasks that must be woken up.
824 *
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800825 * Checks all required queue for wait-for-zero operations, based
826 * on the actual changes that were performed on the semaphore array.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700827 * The function returns 1 if at least one operation was completed successfully.
828 */
829static int do_smart_wakeup_zero(struct sem_array *sma, struct sembuf *sops,
830 int nsops, struct list_head *pt)
831{
832 int i;
833 int semop_completed = 0;
834 int got_zero = 0;
835
836 /* first: the per-semaphore queues, if known */
837 if (sops) {
838 for (i = 0; i < nsops; i++) {
839 int num = sops[i].sem_num;
840
841 if (sma->sem_base[num].semval == 0) {
842 got_zero = 1;
843 semop_completed |= wake_const_ops(sma, num, pt);
844 }
845 }
846 } else {
847 /*
848 * No sops means modified semaphores not known.
849 * Assume all were changed.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700850 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700851 for (i = 0; i < sma->sem_nsems; i++) {
852 if (sma->sem_base[i].semval == 0) {
853 got_zero = 1;
854 semop_completed |= wake_const_ops(sma, i, pt);
855 }
856 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700857 }
858 /*
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700859 * If one of the modified semaphores got 0,
860 * then check the global queue, too.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700861 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700862 if (got_zero)
863 semop_completed |= wake_const_ops(sma, -1, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700864
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700865 return semop_completed;
Manfred Spraulfd5db422010-05-26 14:43:40 -0700866}
867
Manfred Spraul636c6be2009-12-15 16:47:33 -0800868
869/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800870 * update_queue - look for tasks that can be completed.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800871 * @sma: semaphore array.
872 * @semnum: semaphore that was modified.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700873 * @pt: list head for the tasks that must be woken up.
Manfred Spraul636c6be2009-12-15 16:47:33 -0800874 *
875 * update_queue must be called after a semaphore in a semaphore array
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700876 * was modified. If multiple semaphores were modified, update_queue must
877 * be called with semnum = -1, as well as with the number of each modified
878 * semaphore.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700879 * The tasks that must be woken up are added to @pt. The return code
880 * is stored in q->pid.
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700881 * The function internally checks if const operations can now succeed.
882 *
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700883 * The function return 1 if at least one semop was completed successfully.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700885static int update_queue(struct sem_array *sma, int semnum, struct list_head *pt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Manfred Spraul636c6be2009-12-15 16:47:33 -0800887 struct sem_queue *q;
888 struct list_head *walk;
889 struct list_head *pending_list;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700890 int semop_completed = 0;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800891
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700892 if (semnum == -1)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700893 pending_list = &sma->pending_alter;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700894 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700895 pending_list = &sma->sem_base[semnum].pending_alter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Nick Piggin9cad2002009-12-15 16:47:29 -0800897again:
Manfred Spraul636c6be2009-12-15 16:47:33 -0800898 walk = pending_list->next;
899 while (walk != pending_list) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700900 int error, restart;
Manfred Spraul636c6be2009-12-15 16:47:33 -0800901
Rik van Riel9f1bc2c92013-04-30 19:15:39 -0700902 q = container_of(walk, struct sem_queue, list);
Manfred Spraul636c6be2009-12-15 16:47:33 -0800903 walk = walk->next;
Nick Piggin9cad2002009-12-15 16:47:29 -0800904
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800905 /* If we are scanning the single sop, per-semaphore list of
906 * one semaphore and that semaphore is 0, then it is not
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700907 * necessary to scan further: simple increments
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800908 * that affect only one entry succeed immediately and cannot
909 * be in the per semaphore pending queue, and decrements
910 * cannot be successful if the value is already 0.
911 */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700912 if (semnum != -1 && sma->sem_base[semnum].semval == 0)
Manfred Sprauld987f8b2009-12-15 16:47:34 -0800913 break;
914
Manfred Sprauld198cd62014-06-06 14:37:49 -0700915 error = perform_atomic_semop(sma, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* Does q->sleeper still need to sleep? */
Nick Piggin9cad2002009-12-15 16:47:29 -0800918 if (error > 0)
919 continue;
Manfred Spraula1193f82008-07-25 01:48:06 -0700920
Manfred Spraulb97e8202009-12-15 16:47:32 -0800921 unlink_queue(sma, q);
Manfred Spraula1193f82008-07-25 01:48:06 -0700922
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700923 if (error) {
Manfred Spraulfd5db422010-05-26 14:43:40 -0700924 restart = 0;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700925 } else {
926 semop_completed = 1;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700927 do_smart_wakeup_zero(sma, q->sops, q->nsops, pt);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700928 restart = check_restart(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700929 }
Manfred Spraulfd5db422010-05-26 14:43:40 -0700930
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700931 wake_up_sem_queue_prepare(pt, q, error);
Manfred Spraulfd5db422010-05-26 14:43:40 -0700932 if (restart)
Nick Piggin9cad2002009-12-15 16:47:29 -0800933 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700935 return semop_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700938/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800939 * set_semotime - set sem_otime
Manfred Spraul0e8c6652013-09-30 13:45:25 -0700940 * @sma: semaphore array
941 * @sops: operations that modified the array, may be NULL
942 *
943 * sem_otime is replicated to avoid cache line trashing.
944 * This function sets one instance to the current time.
945 */
946static void set_semotime(struct sem_array *sma, struct sembuf *sops)
947{
948 if (sops == NULL) {
949 sma->sem_base[0].sem_otime = get_seconds();
950 } else {
951 sma->sem_base[sops[0].sem_num].sem_otime =
952 get_seconds();
953 }
954}
955
956/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -0800957 * do_smart_update - optimized update_queue
Manfred Spraulfd5db422010-05-26 14:43:40 -0700958 * @sma: semaphore array
959 * @sops: operations that were performed
960 * @nsops: number of operations
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700961 * @otime: force setting otime
962 * @pt: list head of the tasks that must be woken up.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700963 *
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700964 * do_smart_update() does the required calls to update_queue and wakeup_zero,
965 * based on the actual changes that were performed on the semaphore array.
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700966 * Note that the function does not do the actual wake-up: the caller is
967 * responsible for calling wake_up_sem_queue_do(@pt).
968 * It is safe to perform this call after dropping all locks.
Manfred Spraulfd5db422010-05-26 14:43:40 -0700969 */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -0700970static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsops,
971 int otime, struct list_head *pt)
Manfred Spraulfd5db422010-05-26 14:43:40 -0700972{
973 int i;
974
Manfred Spraul1a82e9e2013-07-08 16:01:23 -0700975 otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
976
Manfred Spraulf269f402013-07-08 16:01:24 -0700977 if (!list_empty(&sma->pending_alter)) {
978 /* semaphore array uses the global queue - just process it. */
979 otime |= update_queue(sma, -1, pt);
980 } else {
981 if (!sops) {
982 /*
983 * No sops, thus the modified semaphores are not
984 * known. Check all.
985 */
986 for (i = 0; i < sma->sem_nsems; i++)
987 otime |= update_queue(sma, i, pt);
988 } else {
989 /*
990 * Check the semaphores that were increased:
991 * - No complex ops, thus all sleeping ops are
992 * decrease.
993 * - if we decreased the value, then any sleeping
994 * semaphore ops wont be able to run: If the
995 * previous value was too small, then the new
996 * value will be too small, too.
997 */
998 for (i = 0; i < nsops; i++) {
999 if (sops[i].sem_op > 0) {
1000 otime |= update_queue(sma,
1001 sops[i].sem_num, pt);
1002 }
Manfred Spraulab465df2013-05-26 11:08:52 +02001003 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001004 }
Manfred Spraulfd5db422010-05-26 14:43:40 -07001005 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001006 if (otime)
1007 set_semotime(sma, sops);
Manfred Spraulfd5db422010-05-26 14:43:40 -07001008}
1009
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001010/*
Manfred Spraulb220c572014-06-06 14:37:51 -07001011 * check_qop: Test if a queued operation sleeps on the semaphore semnum
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001012 */
1013static int check_qop(struct sem_array *sma, int semnum, struct sem_queue *q,
1014 bool count_zero)
1015{
Manfred Spraulb220c572014-06-06 14:37:51 -07001016 struct sembuf *sop = q->blocking;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001017
Manfred Spraul9b44ee22014-06-06 14:37:52 -07001018 /*
1019 * Linux always (since 0.99.10) reported a task as sleeping on all
1020 * semaphores. This violates SUS, therefore it was changed to the
1021 * standard compliant behavior.
1022 * Give the administrators a chance to notice that an application
1023 * might misbehave because it relies on the Linux behavior.
1024 */
1025 pr_info_once("semctl(GETNCNT/GETZCNT) is since 3.16 Single Unix Specification compliant.\n"
1026 "The task %s (%d) triggered the difference, watch for misbehavior.\n",
1027 current->comm, task_pid_nr(current));
1028
Manfred Spraulb220c572014-06-06 14:37:51 -07001029 if (sop->sem_num != semnum)
1030 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001031
Manfred Spraulb220c572014-06-06 14:37:51 -07001032 if (count_zero && sop->sem_op == 0)
1033 return 1;
1034 if (!count_zero && sop->sem_op < 0)
1035 return 1;
1036
1037 return 0;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/* The following counts are associated to each semaphore:
1041 * semncnt number of tasks waiting on semval being nonzero
1042 * semzcnt number of tasks waiting on semval being zero
Manfred Spraulb220c572014-06-06 14:37:51 -07001043 *
1044 * Per definition, a task waits only on the semaphore of the first semop
1045 * that cannot proceed, even if additional operation would block, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001047static int count_semcnt(struct sem_array *sma, ushort semnum,
1048 bool count_zero)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001050 struct list_head *l;
Manfred Spraul239521f2014-01-27 17:07:04 -08001051 struct sem_queue *q;
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001052 int semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001054 semcnt = 0;
1055 /* First: check the simple operations. They are easy to evaluate */
1056 if (count_zero)
1057 l = &sma->sem_base[semnum].pending_const;
1058 else
1059 l = &sma->sem_base[semnum].pending_alter;
1060
1061 list_for_each_entry(q, l, list) {
1062 /* all task on a per-semaphore list sleep on exactly
1063 * that semaphore
1064 */
1065 semcnt++;
Rik van Rielde2657f2013-05-09 16:59:59 -04001066 }
1067
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001068 /* Then: check the complex operations. */
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001069 list_for_each_entry(q, &sma->pending_alter, list) {
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001070 semcnt += check_qop(sma, semnum, q, count_zero);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001072 if (count_zero) {
1073 list_for_each_entry(q, &sma->pending_const, list) {
1074 semcnt += check_qop(sma, semnum, q, count_zero);
1075 }
Rik van Rielebc2e5e2013-05-09 16:53:28 -04001076 }
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001077 return semcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001080/* Free a semaphore set. freeary() is called with sem_ids.rwsem locked
1081 * as a writer and the spinlock for this semaphore set hold. sem_ids.rwsem
Nadia Derbey3e148c72007-10-18 23:40:54 -07001082 * remains locked on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001084static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Manfred Spraul380af1b2008-07-25 01:48:06 -07001086 struct sem_undo *un, *tu;
1087 struct sem_queue *q, *tq;
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001088 struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001089 struct list_head tasks;
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001090 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Manfred Spraul380af1b2008-07-25 01:48:06 -07001092 /* Free the existing undo structures for this semaphore set. */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001093 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001094 list_for_each_entry_safe(un, tu, &sma->list_id, list_id) {
1095 list_del(&un->list_id);
1096 spin_lock(&un->ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 un->semid = -1;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001098 list_del_rcu(&un->list_proc);
1099 spin_unlock(&un->ulp->lock);
Lai Jiangshan693a8b62011-03-18 12:09:35 +08001100 kfree_rcu(un, rcu);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* Wake up all pending processes and let them fail with EIDRM. */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001104 INIT_LIST_HEAD(&tasks);
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001105 list_for_each_entry_safe(q, tq, &sma->pending_const, list) {
1106 unlink_queue(sma, q);
1107 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1108 }
1109
1110 list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
Manfred Spraulb97e8202009-12-15 16:47:32 -08001111 unlink_queue(sma, q);
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001112 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001114 for (i = 0; i < sma->sem_nsems; i++) {
1115 struct sem *sem = sma->sem_base + i;
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001116 list_for_each_entry_safe(q, tq, &sem->pending_const, list) {
1117 unlink_queue(sma, q);
1118 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1119 }
1120 list_for_each_entry_safe(q, tq, &sem->pending_alter, list) {
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001121 unlink_queue(sma, q);
1122 wake_up_sem_queue_prepare(&tasks, q, -EIDRM);
1123 }
1124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001126 /* Remove the semaphore set from the IDR */
1127 sem_rmid(ns, sma);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001128 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001129 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001131 wake_up_sem_queue_do(&tasks);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001132 ns->used_sems -= sma->sem_nsems;
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001133 ipc_rcu_putref(sma, sem_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version)
1137{
Manfred Spraul239521f2014-01-27 17:07:04 -08001138 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 case IPC_64:
1140 return copy_to_user(buf, in, sizeof(*in));
1141 case IPC_OLD:
1142 {
1143 struct semid_ds out;
1144
Dan Rosenberg982f7c22010-09-30 15:15:31 -07001145 memset(&out, 0, sizeof(out));
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
1148
1149 out.sem_otime = in->sem_otime;
1150 out.sem_ctime = in->sem_ctime;
1151 out.sem_nsems = in->sem_nsems;
1152
1153 return copy_to_user(buf, &out, sizeof(out));
1154 }
1155 default:
1156 return -EINVAL;
1157 }
1158}
1159
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001160static time_t get_semotime(struct sem_array *sma)
1161{
1162 int i;
1163 time_t res;
1164
1165 res = sma->sem_base[0].sem_otime;
1166 for (i = 1; i < sma->sem_nsems; i++) {
1167 time_t to = sma->sem_base[i].sem_otime;
1168
1169 if (to > res)
1170 res = to;
1171 }
1172 return res;
1173}
1174
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001175static int semctl_nolock(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001176 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Amerigo Wange5cc9c72009-12-15 16:47:35 -08001178 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct sem_array *sma;
1180
Manfred Spraul239521f2014-01-27 17:07:04 -08001181 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 case IPC_INFO:
1183 case SEM_INFO:
1184 {
1185 struct seminfo seminfo;
1186 int max_id;
1187
1188 err = security_sem_semctl(NULL, cmd);
1189 if (err)
1190 return err;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001191
Manfred Spraul239521f2014-01-27 17:07:04 -08001192 memset(&seminfo, 0, sizeof(seminfo));
Kirill Korotaeve3893532006-10-02 02:18:22 -07001193 seminfo.semmni = ns->sc_semmni;
1194 seminfo.semmns = ns->sc_semmns;
1195 seminfo.semmsl = ns->sc_semmsl;
1196 seminfo.semopm = ns->sc_semopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 seminfo.semvmx = SEMVMX;
1198 seminfo.semmnu = SEMMNU;
1199 seminfo.semmap = SEMMAP;
1200 seminfo.semume = SEMUME;
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001201 down_read(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (cmd == SEM_INFO) {
Kirill Korotaeve3893532006-10-02 02:18:22 -07001203 seminfo.semusz = sem_ids(ns).in_use;
1204 seminfo.semaem = ns->used_sems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 } else {
1206 seminfo.semusz = SEMUSZ;
1207 seminfo.semaem = SEMAEM;
1208 }
Nadia Derbey7ca7e562007-10-18 23:40:48 -07001209 max_id = ipc_get_maxid(&sem_ids(ns));
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001210 up_read(&sem_ids(ns).rwsem);
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001211 if (copy_to_user(p, &seminfo, sizeof(struct seminfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return -EFAULT;
Manfred Spraul239521f2014-01-27 17:07:04 -08001213 return (max_id < 0) ? 0 : max_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001215 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case SEM_STAT:
1217 {
1218 struct semid64_ds tbuf;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001219 int id = 0;
1220
1221 memset(&tbuf, 0, sizeof(tbuf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Linus Torvalds941b0302013-05-04 11:04:29 -07001223 rcu_read_lock();
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001224 if (cmd == SEM_STAT) {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001225 sma = sem_obtain_object(ns, semid);
1226 if (IS_ERR(sma)) {
1227 err = PTR_ERR(sma);
1228 goto out_unlock;
1229 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001230 id = sma->sem_perm.id;
1231 } else {
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001232 sma = sem_obtain_object_check(ns, semid);
1233 if (IS_ERR(sma)) {
1234 err = PTR_ERR(sma);
1235 goto out_unlock;
1236 }
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 err = -EACCES;
Serge E. Hallynb0e77592011-03-23 16:43:24 -07001240 if (ipcperms(ns, &sma->sem_perm, S_IRUGO))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 goto out_unlock;
1242
1243 err = security_sem_semctl(sma, cmd);
1244 if (err)
1245 goto out_unlock;
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
Manfred Sprauld12e1e52013-07-08 16:01:25 -07001248 tbuf.sem_otime = get_semotime(sma);
1249 tbuf.sem_ctime = sma->sem_ctime;
1250 tbuf.sem_nsems = sma->sem_nsems;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001251 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001252 if (copy_semid_to_user(p, &tbuf, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EFAULT;
1254 return id;
1255 }
1256 default:
1257 return -EINVAL;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259out_unlock:
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001260 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return err;
1262}
1263
Al Viroe1fd1f42013-03-05 15:04:55 -05001264static int semctl_setval(struct ipc_namespace *ns, int semid, int semnum,
1265 unsigned long arg)
1266{
1267 struct sem_undo *un;
1268 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001269 struct sem *curr;
Al Viroe1fd1f42013-03-05 15:04:55 -05001270 int err;
Al Viroe1fd1f42013-03-05 15:04:55 -05001271 struct list_head tasks;
1272 int val;
1273#if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1274 /* big-endian 64bit */
1275 val = arg >> 32;
1276#else
1277 /* 32bit or little-endian 64bit */
1278 val = arg;
1279#endif
1280
Rik van Riel6062a8d2013-04-30 19:15:44 -07001281 if (val > SEMVMX || val < 0)
1282 return -ERANGE;
Al Viroe1fd1f42013-03-05 15:04:55 -05001283
1284 INIT_LIST_HEAD(&tasks);
Al Viroe1fd1f42013-03-05 15:04:55 -05001285
Rik van Riel6062a8d2013-04-30 19:15:44 -07001286 rcu_read_lock();
1287 sma = sem_obtain_object_check(ns, semid);
1288 if (IS_ERR(sma)) {
1289 rcu_read_unlock();
1290 return PTR_ERR(sma);
1291 }
1292
1293 if (semnum < 0 || semnum >= sma->sem_nsems) {
1294 rcu_read_unlock();
1295 return -EINVAL;
1296 }
1297
1298
1299 if (ipcperms(ns, &sma->sem_perm, S_IWUGO)) {
1300 rcu_read_unlock();
1301 return -EACCES;
1302 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001303
1304 err = security_sem_semctl(sma, SETVAL);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001305 if (err) {
1306 rcu_read_unlock();
1307 return -EACCES;
1308 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001309
Rik van Riel6062a8d2013-04-30 19:15:44 -07001310 sem_lock(sma, NULL, -1);
Al Viroe1fd1f42013-03-05 15:04:55 -05001311
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001312 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001313 sem_unlock(sma, -1);
1314 rcu_read_unlock();
1315 return -EIDRM;
1316 }
1317
Al Viroe1fd1f42013-03-05 15:04:55 -05001318 curr = &sma->sem_base[semnum];
1319
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001320 ipc_assert_locked_object(&sma->sem_perm);
Al Viroe1fd1f42013-03-05 15:04:55 -05001321 list_for_each_entry(un, &sma->list_id, list_id)
1322 un->semadj[semnum] = 0;
1323
1324 curr->semval = val;
1325 curr->sempid = task_tgid_vnr(current);
1326 sma->sem_ctime = get_seconds();
1327 /* maybe some queued-up processes were waiting for this */
1328 do_smart_update(sma, NULL, 0, 0, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001329 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001330 rcu_read_unlock();
Al Viroe1fd1f42013-03-05 15:04:55 -05001331 wake_up_sem_queue_do(&tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001332 return 0;
Al Viroe1fd1f42013-03-05 15:04:55 -05001333}
1334
Kirill Korotaeve3893532006-10-02 02:18:22 -07001335static int semctl_main(struct ipc_namespace *ns, int semid, int semnum,
Al Viroe1fd1f42013-03-05 15:04:55 -05001336 int cmd, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
1338 struct sem_array *sma;
Manfred Spraul239521f2014-01-27 17:07:04 -08001339 struct sem *curr;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001340 int err, nsems;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 ushort fast_sem_io[SEMMSL_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001342 ushort *sem_io = fast_sem_io;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001343 struct list_head tasks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001345 INIT_LIST_HEAD(&tasks);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001346
1347 rcu_read_lock();
1348 sma = sem_obtain_object_check(ns, semid);
1349 if (IS_ERR(sma)) {
1350 rcu_read_unlock();
1351 return PTR_ERR(sma);
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 nsems = sma->sem_nsems;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 err = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001357 if (ipcperms(ns, &sma->sem_perm, cmd == SETALL ? S_IWUGO : S_IRUGO))
1358 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 err = security_sem_semctl(sma, cmd);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001361 if (err)
1362 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 err = -EACCES;
1365 switch (cmd) {
1366 case GETALL:
1367 {
Al Viroe1fd1f42013-03-05 15:04:55 -05001368 ushort __user *array = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 int i;
1370
Al Viroce857222013-05-03 00:30:49 +01001371 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001372 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001373 err = -EIDRM;
1374 goto out_unlock;
1375 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001376 if (nsems > SEMMSL_FAST) {
Al Viroce857222013-05-03 00:30:49 +01001377 if (!ipc_rcu_getref(sma)) {
Al Viroce857222013-05-03 00:30:49 +01001378 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001379 goto out_unlock;
Al Viroce857222013-05-03 00:30:49 +01001380 }
1381 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001382 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001384 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001385 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return -ENOMEM;
1387 }
1388
Linus Torvalds4091fd92013-05-04 10:13:40 -07001389 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001390 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001391 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001393 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
Al Viroce857222013-05-03 00:30:49 +01001395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 for (i = 0; i < sma->sem_nsems; i++)
1397 sem_io[i] = sma->sem_base[i].semval;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001398 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001399 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 err = 0;
Manfred Spraul239521f2014-01-27 17:07:04 -08001401 if (copy_to_user(array, sem_io, nsems*sizeof(ushort)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 err = -EFAULT;
1403 goto out_free;
1404 }
1405 case SETALL:
1406 {
1407 int i;
1408 struct sem_undo *un;
1409
Rik van Riel6062a8d2013-04-30 19:15:44 -07001410 if (!ipc_rcu_getref(sma)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001411 err = -EIDRM;
1412 goto out_rcu_wakeup;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001413 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001414 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Manfred Spraul239521f2014-01-27 17:07:04 -08001416 if (nsems > SEMMSL_FAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 sem_io = ipc_alloc(sizeof(ushort)*nsems);
Manfred Spraul239521f2014-01-27 17:07:04 -08001418 if (sem_io == NULL) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001419 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -ENOMEM;
1421 }
1422 }
1423
Manfred Spraul239521f2014-01-27 17:07:04 -08001424 if (copy_from_user(sem_io, p, nsems*sizeof(ushort))) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001425 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 err = -EFAULT;
1427 goto out_free;
1428 }
1429
1430 for (i = 0; i < nsems; i++) {
1431 if (sem_io[i] > SEMVMX) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001432 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 err = -ERANGE;
1434 goto out_free;
1435 }
1436 }
Linus Torvalds4091fd92013-05-04 10:13:40 -07001437 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001438 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001439 if (!ipc_valid_object(&sma->sem_perm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 err = -EIDRM;
Manfred Spraul6e224f92013-10-16 13:46:45 -07001441 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001444 for (i = 0; i < nsems; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 sma->sem_base[i].semval = sem_io[i];
Davidlohr Buesoa5f4db82016-03-22 14:27:48 -07001446 sma->sem_base[i].sempid = task_tgid_vnr(current);
1447 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001448
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001449 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001450 list_for_each_entry(un, &sma->list_id, list_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 for (i = 0; i < nsems; i++)
1452 un->semadj[i] = 0;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 sma->sem_ctime = get_seconds();
1455 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001456 do_smart_update(sma, NULL, 0, 0, &tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 err = 0;
1458 goto out_unlock;
1459 }
Al Viroe1fd1f42013-03-05 15:04:55 -05001460 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 err = -EINVAL;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001463 if (semnum < 0 || semnum >= nsems)
1464 goto out_rcu_wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Rik van Riel6062a8d2013-04-30 19:15:44 -07001466 sem_lock(sma, NULL, -1);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001467 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07001468 err = -EIDRM;
1469 goto out_unlock;
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 curr = &sma->sem_base[semnum];
1472
1473 switch (cmd) {
1474 case GETVAL:
1475 err = curr->semval;
1476 goto out_unlock;
1477 case GETPID:
1478 err = curr->sempid;
1479 goto out_unlock;
1480 case GETNCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001481 err = count_semcnt(sma, semnum, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out_unlock;
1483 case GETZCNT:
Manfred Spraul2f2ed41d2014-06-06 14:37:48 -07001484 err = count_semcnt(sma, semnum, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488out_unlock:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001489 sem_unlock(sma, -1);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001490out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001491 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001492 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08001494 if (sem_io != fast_sem_io)
Tetsuo Handa1d5cfdb2016-01-22 15:11:02 -08001495 ipc_free(sem_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return err;
1497}
1498
Pierre Peiffer016d7132008-04-29 01:00:50 -07001499static inline unsigned long
1500copy_semid_from_user(struct semid64_ds *out, void __user *buf, int version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Manfred Spraul239521f2014-01-27 17:07:04 -08001502 switch (version) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case IPC_64:
Pierre Peiffer016d7132008-04-29 01:00:50 -07001504 if (copy_from_user(out, buf, sizeof(*out)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 case IPC_OLD:
1508 {
1509 struct semid_ds tbuf_old;
1510
Manfred Spraul239521f2014-01-27 17:07:04 -08001511 if (copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return -EFAULT;
1513
Pierre Peiffer016d7132008-04-29 01:00:50 -07001514 out->sem_perm.uid = tbuf_old.sem_perm.uid;
1515 out->sem_perm.gid = tbuf_old.sem_perm.gid;
1516 out->sem_perm.mode = tbuf_old.sem_perm.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 return 0;
1519 }
1520 default:
1521 return -EINVAL;
1522 }
1523}
1524
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001525/*
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001526 * This function handles some semctl commands which require the rwsem
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001527 * to be held in write mode.
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001528 * NOTE: no locks must be held, the rwsem is taken inside this function.
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001529 */
Pierre Peiffer21a48262008-04-29 01:00:49 -07001530static int semctl_down(struct ipc_namespace *ns, int semid,
Al Viroe1fd1f42013-03-05 15:04:55 -05001531 int cmd, int version, void __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 struct sem_array *sma;
1534 int err;
Pierre Peiffer016d7132008-04-29 01:00:50 -07001535 struct semid64_ds semid64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 struct kern_ipc_perm *ipcp;
1537
Manfred Spraul239521f2014-01-27 17:07:04 -08001538 if (cmd == IPC_SET) {
Al Viroe1fd1f42013-03-05 15:04:55 -05001539 if (copy_semid_from_user(&semid64, p, version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001543 down_write(&sem_ids(ns).rwsem);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001544 rcu_read_lock();
1545
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001546 ipcp = ipcctl_pre_down_nolock(ns, &sem_ids(ns), semid, cmd,
1547 &semid64.sem_perm, 0);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001548 if (IS_ERR(ipcp)) {
1549 err = PTR_ERR(ipcp);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001550 goto out_unlock1;
1551 }
Steve Grubb073115d2006-04-02 17:07:33 -04001552
Pierre Peiffera5f75e72008-04-29 01:00:54 -07001553 sma = container_of(ipcp, struct sem_array, sem_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
1555 err = security_sem_semctl(sma, cmd);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001556 if (err)
1557 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001559 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 case IPC_RMID:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001561 sem_lock(sma, NULL, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001562 /* freeary unlocks the ipc object and rcu */
Pierre Peiffer01b8b072008-02-08 04:18:57 -08001563 freeary(ns, ipcp);
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001564 goto out_up;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 case IPC_SET:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001566 sem_lock(sma, NULL, -1);
Eric W. Biederman1efdb692012-02-07 16:54:11 -08001567 err = ipc_update_perm(&semid64.sem_perm, ipcp);
1568 if (err)
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001569 goto out_unlock0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 sma->sem_ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 err = -EINVAL;
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001574 goto out_unlock1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001577out_unlock0:
Rik van Riel6062a8d2013-04-30 19:15:44 -07001578 sem_unlock(sma, -1);
Davidlohr Bueso7b4cc5d2013-07-08 16:01:12 -07001579out_unlock1:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001580 rcu_read_unlock();
Pierre Peiffer522bb2a2008-04-29 01:00:49 -07001581out_up:
Davidlohr Buesod9a605e2013-09-11 14:26:24 -07001582 up_write(&sem_ids(ns).rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return err;
1584}
1585
Al Viroe1fd1f42013-03-05 15:04:55 -05001586SYSCALL_DEFINE4(semctl, int, semid, int, semnum, int, cmd, unsigned long, arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int version;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001589 struct ipc_namespace *ns;
Al Viroe1fd1f42013-03-05 15:04:55 -05001590 void __user *p = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 if (semid < 0)
1593 return -EINVAL;
1594
1595 version = ipc_parse_version(&cmd);
Kirill Korotaeve3893532006-10-02 02:18:22 -07001596 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Manfred Spraul239521f2014-01-27 17:07:04 -08001598 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 case IPC_INFO:
1600 case SEM_INFO:
Pierre Peiffer4b9fcb02008-02-08 04:18:56 -08001601 case IPC_STAT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 case SEM_STAT:
Al Viroe1fd1f42013-03-05 15:04:55 -05001603 return semctl_nolock(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 case GETALL:
1605 case GETVAL:
1606 case GETPID:
1607 case GETNCNT:
1608 case GETZCNT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 case SETALL:
Al Viroe1fd1f42013-03-05 15:04:55 -05001610 return semctl_main(ns, semid, semnum, cmd, p);
1611 case SETVAL:
1612 return semctl_setval(ns, semid, semnum, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 case IPC_RMID:
1614 case IPC_SET:
Al Viroe1fd1f42013-03-05 15:04:55 -05001615 return semctl_down(ns, semid, cmd, version, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 default:
1617 return -EINVAL;
1618 }
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/* If the task doesn't already have a undo_list, then allocate one
1622 * here. We guarantee there is only one thread using this undo list,
1623 * and current is THE ONE
1624 *
1625 * If this allocation and assignment succeeds, but later
1626 * portions of this code fail, there is no need to free the sem_undo_list.
1627 * Just let it stay associated with the task, and it'll be freed later
1628 * at exit time.
1629 *
1630 * This can block, so callers must hold no locks.
1631 */
1632static inline int get_undo_list(struct sem_undo_list **undo_listp)
1633{
1634 struct sem_undo_list *undo_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 undo_list = current->sysvsem.undo_list;
1637 if (!undo_list) {
Matt Helsley2453a302006-10-02 02:18:25 -07001638 undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 if (undo_list == NULL)
1640 return -ENOMEM;
Ingo Molnar00a5dfd2005-08-05 23:05:27 +02001641 spin_lock_init(&undo_list->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 atomic_set(&undo_list->refcnt, 1);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001643 INIT_LIST_HEAD(&undo_list->list_proc);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 current->sysvsem.undo_list = undo_list;
1646 }
1647 *undo_listp = undo_list;
1648 return 0;
1649}
1650
Nick Pigginbf17bb72009-12-15 16:47:28 -08001651static struct sem_undo *__lookup_undo(struct sem_undo_list *ulp, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Nick Pigginbf17bb72009-12-15 16:47:28 -08001653 struct sem_undo *un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Nick Pigginbf17bb72009-12-15 16:47:28 -08001655 list_for_each_entry_rcu(un, &ulp->list_proc, list_proc) {
1656 if (un->semid == semid)
1657 return un;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001659 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Nick Pigginbf17bb72009-12-15 16:47:28 -08001662static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
1663{
1664 struct sem_undo *un;
1665
Manfred Spraul239521f2014-01-27 17:07:04 -08001666 assert_spin_locked(&ulp->lock);
Nick Pigginbf17bb72009-12-15 16:47:28 -08001667
1668 un = __lookup_undo(ulp, semid);
1669 if (un) {
1670 list_del_rcu(&un->list_proc);
1671 list_add_rcu(&un->list_proc, &ulp->list_proc);
1672 }
1673 return un;
1674}
1675
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001676/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001677 * find_alloc_undo - lookup (and if not present create) undo array
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001678 * @ns: namespace
1679 * @semid: semaphore array id
1680 *
1681 * The function looks up (and if not present creates) the undo structure.
1682 * The size of the undo structure depends on the size of the semaphore
1683 * array, thus the alloc path is not that straightforward.
Manfred Spraul380af1b2008-07-25 01:48:06 -07001684 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1685 * performs a rcu_read_lock().
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001686 */
1687static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 struct sem_array *sma;
1690 struct sem_undo_list *ulp;
1691 struct sem_undo *un, *new;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001692 int nsems, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 error = get_undo_list(&ulp);
1695 if (error)
1696 return ERR_PTR(error);
1697
Manfred Spraul380af1b2008-07-25 01:48:06 -07001698 rcu_read_lock();
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001699 spin_lock(&ulp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 un = lookup_undo(ulp, semid);
Pierre Peifferc530c6a2007-10-18 23:40:55 -07001701 spin_unlock(&ulp->lock);
Manfred Spraul239521f2014-01-27 17:07:04 -08001702 if (likely(un != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto out;
1704
1705 /* no undo structure around - allocate one. */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001706 /* step 1: figure out the size of the semaphore array */
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001707 sma = sem_obtain_object_check(ns, semid);
1708 if (IS_ERR(sma)) {
1709 rcu_read_unlock();
Julia Lawall4de85cd2010-05-26 14:43:44 -07001710 return ERR_CAST(sma);
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001711 }
Nadia Derbey023a5352007-10-18 23:40:51 -07001712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 nsems = sma->sem_nsems;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001714 if (!ipc_rcu_getref(sma)) {
1715 rcu_read_unlock();
1716 un = ERR_PTR(-EIDRM);
1717 goto out;
1718 }
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001719 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001721 /* step 2: allocate new undo structure */
Burman Yan4668edc2006-12-06 20:38:51 -08001722 new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (!new) {
Davidlohr Bueso53dad6d2013-09-23 17:04:45 -07001724 ipc_rcu_putref(sma, ipc_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return ERR_PTR(-ENOMEM);
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Manfred Spraul380af1b2008-07-25 01:48:06 -07001728 /* step 3: Acquire the lock on semaphore array */
Linus Torvalds4091fd92013-05-04 10:13:40 -07001729 rcu_read_lock();
Pierre Peiffer6ff37972008-04-29 01:00:46 -07001730 sem_lock_and_putref(sma);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001731 if (!ipc_valid_object(&sma->sem_perm)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001732 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001733 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 kfree(new);
1735 un = ERR_PTR(-EIDRM);
1736 goto out;
1737 }
Manfred Spraul380af1b2008-07-25 01:48:06 -07001738 spin_lock(&ulp->lock);
1739
1740 /*
1741 * step 4: check for races: did someone else allocate the undo struct?
1742 */
1743 un = lookup_undo(ulp, semid);
1744 if (un) {
1745 kfree(new);
1746 goto success;
1747 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001748 /* step 5: initialize & link new undo structure */
1749 new->semadj = (short *) &new[1];
Manfred Spraul380af1b2008-07-25 01:48:06 -07001750 new->ulp = ulp;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001751 new->semid = semid;
1752 assert_spin_locked(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07001753 list_add_rcu(&new->list_proc, &ulp->list_proc);
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07001754 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001755 list_add(&new->list_id, &sma->list_id);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001756 un = new;
Manfred Spraul380af1b2008-07-25 01:48:06 -07001757
1758success:
1759 spin_unlock(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001760 sem_unlock(sma, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761out:
1762 return un;
1763}
1764
Manfred Spraulc61284e2010-07-20 13:24:23 -07001765
1766/**
Davidlohr Bueso8001c852014-01-27 17:07:05 -08001767 * get_queue_result - retrieve the result code from sem_queue
Manfred Spraulc61284e2010-07-20 13:24:23 -07001768 * @q: Pointer to queue structure
1769 *
1770 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1771 * q->status, then we must loop until the value is replaced with the final
1772 * value: This may happen if a task is woken up by an unrelated event (e.g.
1773 * signal) and in parallel the task is woken up by another task because it got
1774 * the requested semaphores.
1775 *
1776 * The function can be called with or without holding the semaphore spinlock.
1777 */
1778static int get_queue_result(struct sem_queue *q)
1779{
1780 int error;
1781
1782 error = q->status;
1783 while (unlikely(error == IN_WAKEUP)) {
1784 cpu_relax();
1785 error = q->status;
1786 }
1787
1788 return error;
1789}
1790
Heiko Carstensd5460c92009-01-14 14:14:27 +01001791SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
1792 unsigned, nsops, const struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 int error = -EINVAL;
1795 struct sem_array *sma;
1796 struct sembuf fast_sops[SEMOPM_FAST];
Manfred Spraul239521f2014-01-27 17:07:04 -08001797 struct sembuf *sops = fast_sops, *sop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct sem_undo *un;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001799 int undos = 0, alter = 0, max, locknum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 struct sem_queue queue;
1801 unsigned long jiffies_left = 0;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001802 struct ipc_namespace *ns;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001803 struct list_head tasks;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001804
1805 ns = current->nsproxy->ipc_ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 if (nsops < 1 || semid < 0)
1808 return -EINVAL;
Kirill Korotaeve3893532006-10-02 02:18:22 -07001809 if (nsops > ns->sc_semopm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return -E2BIG;
Manfred Spraul239521f2014-01-27 17:07:04 -08001811 if (nsops > SEMOPM_FAST) {
1812 sops = kmalloc(sizeof(*sops)*nsops, GFP_KERNEL);
1813 if (sops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return -ENOMEM;
1815 }
Manfred Spraul239521f2014-01-27 17:07:04 -08001816 if (copy_from_user(sops, tsops, nsops * sizeof(*tsops))) {
1817 error = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto out_free;
1819 }
1820 if (timeout) {
1821 struct timespec _timeout;
1822 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
1823 error = -EFAULT;
1824 goto out_free;
1825 }
1826 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1827 _timeout.tv_nsec >= 1000000000L) {
1828 error = -EINVAL;
1829 goto out_free;
1830 }
1831 jiffies_left = timespec_to_jiffies(&_timeout);
1832 }
1833 max = 0;
1834 for (sop = sops; sop < sops + nsops; sop++) {
1835 if (sop->sem_num >= max)
1836 max = sop->sem_num;
1837 if (sop->sem_flg & SEM_UNDO)
Manfred Spraulb78755a2005-06-23 00:10:06 -07001838 undos = 1;
1839 if (sop->sem_op != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 alter = 1;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Rik van Riel6062a8d2013-04-30 19:15:44 -07001843 INIT_LIST_HEAD(&tasks);
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (undos) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001846 /* On success, find_alloc_undo takes the rcu_read_lock */
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001847 un = find_alloc_undo(ns, semid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (IS_ERR(un)) {
1849 error = PTR_ERR(un);
1850 goto out_free;
1851 }
Rik van Riel6062a8d2013-04-30 19:15:44 -07001852 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 un = NULL;
Rik van Riel6062a8d2013-04-30 19:15:44 -07001854 rcu_read_lock();
1855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001857 sma = sem_obtain_object_check(ns, semid);
Nadia Derbey023a5352007-10-18 23:40:51 -07001858 if (IS_ERR(sma)) {
Rik van Riel6062a8d2013-04-30 19:15:44 -07001859 rcu_read_unlock();
Nadia Derbey023a5352007-10-18 23:40:51 -07001860 error = PTR_ERR(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 goto out_free;
Nadia Derbey023a5352007-10-18 23:40:51 -07001862 }
1863
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001864 error = -EFBIG;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001865 if (max >= sma->sem_nsems)
1866 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001867
1868 error = -EACCES;
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001869 if (ipcperms(ns, &sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1870 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001871
1872 error = security_sem_semop(sma, sops, nsops, alter);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07001873 if (error)
1874 goto out_rcu_wakeup;
Davidlohr Bueso16df3672013-04-30 19:15:29 -07001875
Manfred Spraul6e224f92013-10-16 13:46:45 -07001876 error = -EIDRM;
1877 locknum = sem_lock(sma, sops, nsops);
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08001878 /*
1879 * We eventually might perform the following check in a lockless
1880 * fashion, considering ipc_valid_object() locking constraints.
1881 * If nsops == 1 and there is no contention for sem_perm.lock, then
1882 * only a per-semaphore lock is held and it's OK to proceed with the
1883 * check below. More details on the fine grained locking scheme
1884 * entangled here and why it's RMID race safe on comments at sem_lock()
1885 */
1886 if (!ipc_valid_object(&sma->sem_perm))
Manfred Spraul6e224f92013-10-16 13:46:45 -07001887 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001889 * semid identifiers are not unique - find_alloc_undo may have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 * allocated an undo structure, it was invalidated by an RMID
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001891 * and now a new array with received the same id. Check and fail.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001892 * This case can be detected checking un->semid. The existence of
Manfred Spraul380af1b2008-07-25 01:48:06 -07001893 * "un" itself is guaranteed by rcu.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07001895 if (un && un->semid == -1)
1896 goto out_unlock_free;
Manfred Spraul4daa28f2008-07-25 01:48:04 -07001897
Manfred Sprauld198cd62014-06-06 14:37:49 -07001898 queue.sops = sops;
1899 queue.nsops = nsops;
1900 queue.undo = un;
1901 queue.pid = task_tgid_vnr(current);
1902 queue.alter = alter;
1903
1904 error = perform_atomic_semop(sma, &queue);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001905 if (error == 0) {
1906 /* If the operation was successful, then do
1907 * the required updates.
1908 */
1909 if (alter)
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07001910 do_smart_update(sma, sops, nsops, 1, &tasks);
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001911 else
1912 set_semotime(sma, sops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Manfred Spraul0e8c6652013-09-30 13:45:25 -07001914 if (error <= 0)
1915 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /* We need to sleep on this operation, so we put the current
1918 * task into the pending queue and go to sleep.
1919 */
Paul McQuade46c0a8c2014-06-06 14:37:37 -07001920
Manfred Spraulb97e8202009-12-15 16:47:32 -08001921 if (nsops == 1) {
1922 struct sem *curr;
1923 curr = &sma->sem_base[sops->sem_num];
1924
Manfred Spraulf269f402013-07-08 16:01:24 -07001925 if (alter) {
1926 if (sma->complex_count) {
1927 list_add_tail(&queue.list,
1928 &sma->pending_alter);
1929 } else {
1930
1931 list_add_tail(&queue.list,
1932 &curr->pending_alter);
1933 }
1934 } else {
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001935 list_add_tail(&queue.list, &curr->pending_const);
Manfred Spraulf269f402013-07-08 16:01:24 -07001936 }
Manfred Spraulb97e8202009-12-15 16:47:32 -08001937 } else {
Manfred Spraulf269f402013-07-08 16:01:24 -07001938 if (!sma->complex_count)
1939 merge_queues(sma);
1940
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001941 if (alter)
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001942 list_add_tail(&queue.list, &sma->pending_alter);
Rik van Riel9f1bc2c92013-04-30 19:15:39 -07001943 else
Manfred Spraul1a82e9e2013-07-08 16:01:23 -07001944 list_add_tail(&queue.list, &sma->pending_const);
1945
Manfred Spraulb97e8202009-12-15 16:47:32 -08001946 sma->complex_count++;
1947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 queue.status = -EINTR;
1950 queue.sleeper = current;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07001951
1952sleep_again:
Davidlohr Bueso52644c92015-02-17 13:47:55 -08001953 __set_current_state(TASK_INTERRUPTIBLE);
Rik van Riel6062a8d2013-04-30 19:15:44 -07001954 sem_unlock(sma, locknum);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07001955 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 if (timeout)
1958 jiffies_left = schedule_timeout(jiffies_left);
1959 else
1960 schedule();
1961
Manfred Spraulc61284e2010-07-20 13:24:23 -07001962 error = get_queue_result(&queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (error != -EINTR) {
1965 /* fast path: update_queue already obtained all requested
Manfred Spraulc61284e2010-07-20 13:24:23 -07001966 * resources.
1967 * Perform a smp_mb(): User space could assume that semop()
1968 * is a memory barrier: Without the mb(), the cpu could
1969 * speculatively read in user space stale data that was
1970 * overwritten by the previous owner of the semaphore.
1971 */
1972 smp_mb();
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto out_free;
1975 }
1976
Linus Torvalds321310c2013-05-04 10:47:57 -07001977 rcu_read_lock();
Rik van Riel6062a8d2013-04-30 19:15:44 -07001978 sma = sem_obtain_lock(ns, semid, sops, nsops, &locknum);
Manfred Sprauld694ad62011-07-25 17:11:47 -07001979
1980 /*
1981 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1982 */
1983 error = get_queue_result(&queue);
1984
1985 /*
1986 * Array removed? If yes, leave without sem_unlock().
1987 */
Nadia Derbey023a5352007-10-18 23:40:51 -07001988 if (IS_ERR(sma)) {
Linus Torvalds321310c2013-05-04 10:47:57 -07001989 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto out_free;
1991 }
1992
Manfred Spraulc61284e2010-07-20 13:24:23 -07001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 /*
Manfred Sprauld694ad62011-07-25 17:11:47 -07001995 * If queue.status != -EINTR we are woken up by another process.
1996 * Leave without unlink_queue(), but with sem_unlock().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 */
Davidlohr Bueso3ab08fe2014-01-27 17:07:06 -08001998 if (error != -EINTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto out_unlock_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * If an interrupt occurred we have to clean up the queue
2003 */
2004 if (timeout && jiffies_left == 0)
2005 error = -EAGAIN;
Manfred Spraul0b0577f2011-11-02 13:38:52 -07002006
2007 /*
2008 * If the wakeup was spurious, just retry
2009 */
2010 if (error == -EINTR && !signal_pending(current))
2011 goto sleep_again;
2012
Manfred Spraulb97e8202009-12-15 16:47:32 -08002013 unlink_queue(sma, &queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015out_unlock_free:
Rik van Riel6062a8d2013-04-30 19:15:44 -07002016 sem_unlock(sma, locknum);
Linus Torvaldsc728b9c2013-05-04 11:04:29 -07002017out_rcu_wakeup:
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002018 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002019 wake_up_sem_queue_do(&tasks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020out_free:
Manfred Spraul239521f2014-01-27 17:07:04 -08002021 if (sops != fast_sops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 kfree(sops);
2023 return error;
2024}
2025
Heiko Carstensd5460c92009-01-14 14:14:27 +01002026SYSCALL_DEFINE3(semop, int, semid, struct sembuf __user *, tsops,
2027 unsigned, nsops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 return sys_semtimedop(semid, tsops, nsops, NULL);
2030}
2031
2032/* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
2033 * parent and child tasks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
2035
2036int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
2037{
2038 struct sem_undo_list *undo_list;
2039 int error;
2040
2041 if (clone_flags & CLONE_SYSVSEM) {
2042 error = get_undo_list(&undo_list);
2043 if (error)
2044 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 atomic_inc(&undo_list->refcnt);
2046 tsk->sysvsem.undo_list = undo_list;
Paul McQuade46c0a8c2014-06-06 14:37:37 -07002047 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 tsk->sysvsem.undo_list = NULL;
2049
2050 return 0;
2051}
2052
2053/*
2054 * add semadj values to semaphores, free undo structures.
2055 * undo structures are not freed when semaphore arrays are destroyed
2056 * so some of them may be out of date.
2057 * IMPLEMENTATION NOTE: There is some confusion over whether the
2058 * set of adjustments that needs to be done should be done in an atomic
2059 * manner or not. That is, if we are attempting to decrement the semval
2060 * should we queue up and wait until we can do so legally?
2061 * The original implementation attempted to do this (queue and wait).
2062 * The current implementation does not do so. The POSIX standard
2063 * and SVID should be consulted to determine what behavior is mandated.
2064 */
2065void exit_sem(struct task_struct *tsk)
2066{
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002067 struct sem_undo_list *ulp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002069 ulp = tsk->sysvsem.undo_list;
2070 if (!ulp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return;
Manfred Spraul9edff4a2008-04-29 01:00:57 -07002072 tsk->sysvsem.undo_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002074 if (!atomic_dec_and_test(&ulp->refcnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return;
2076
Manfred Spraul380af1b2008-07-25 01:48:06 -07002077 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 struct sem_array *sma;
Manfred Spraul380af1b2008-07-25 01:48:06 -07002079 struct sem_undo *un;
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002080 struct list_head tasks;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002081 int semid, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Manfred Spraul380af1b2008-07-25 01:48:06 -07002083 rcu_read_lock();
Jiri Pirko05725f72009-04-14 20:17:16 +02002084 un = list_entry_rcu(ulp->list_proc.next,
2085 struct sem_undo, list_proc);
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002086 if (&un->list_proc == &ulp->list_proc) {
2087 /*
2088 * We must wait for freeary() before freeing this ulp,
2089 * in case we raced with last sem_undo. There is a small
2090 * possibility where we exit while freeary() didn't
2091 * finish unlocking sem_undo_list.
2092 */
2093 spin_unlock_wait(&ulp->lock);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002094 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002095 break;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002096 }
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002097 spin_lock(&ulp->lock);
2098 semid = un->semid;
2099 spin_unlock(&ulp->lock);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002100
Herton R. Krzesinski602b8592015-08-14 15:35:02 -07002101 /* exit_sem raced with IPC_RMID, nothing to do */
2102 if (semid == -1) {
2103 rcu_read_unlock();
2104 continue;
2105 }
2106
2107 sma = sem_obtain_object_check(tsk->nsproxy->ipc_ns, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002108 /* exit_sem raced with IPC_RMID, nothing to do */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002109 if (IS_ERR(sma)) {
2110 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002111 continue;
Rik van Riel6062a8d2013-04-30 19:15:44 -07002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Rik van Riel6062a8d2013-04-30 19:15:44 -07002114 sem_lock(sma, NULL, -1);
Manfred Spraul6e224f92013-10-16 13:46:45 -07002115 /* exit_sem raced with IPC_RMID, nothing to do */
Rafael Aquini0f3d2b02014-01-27 17:07:01 -08002116 if (!ipc_valid_object(&sma->sem_perm)) {
Manfred Spraul6e224f92013-10-16 13:46:45 -07002117 sem_unlock(sma, -1);
2118 rcu_read_unlock();
2119 continue;
2120 }
Nick Pigginbf17bb72009-12-15 16:47:28 -08002121 un = __lookup_undo(ulp, semid);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002122 if (un == NULL) {
2123 /* exit_sem raced with IPC_RMID+semget() that created
2124 * exactly the same semid. Nothing to do.
2125 */
Rik van Riel6062a8d2013-04-30 19:15:44 -07002126 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002127 rcu_read_unlock();
Manfred Spraul380af1b2008-07-25 01:48:06 -07002128 continue;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Manfred Spraul380af1b2008-07-25 01:48:06 -07002131 /* remove un from the linked lists */
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -07002132 ipc_assert_locked_object(&sma->sem_perm);
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002133 list_del(&un->list_id);
2134
Herton R. Krzesinskia9795582015-08-14 15:35:05 -07002135 /* we are the last process using this ulp, acquiring ulp->lock
2136 * isn't required. Besides that, we are also protected against
2137 * IPC_RMID as we hold sma->sem_perm lock now
2138 */
Manfred Spraul380af1b2008-07-25 01:48:06 -07002139 list_del_rcu(&un->list_proc);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002140
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002141 /* perform adjustments registered in un */
2142 for (i = 0; i < sma->sem_nsems; i++) {
Manfred Spraul239521f2014-01-27 17:07:04 -08002143 struct sem *semaphore = &sma->sem_base[i];
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002144 if (un->semadj[i]) {
2145 semaphore->semval += un->semadj[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * Range checks of the new semaphore value,
2148 * not defined by sus:
2149 * - Some unices ignore the undo entirely
2150 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
2151 * - some cap the value (e.g. FreeBSD caps
2152 * at 0, but doesn't enforce SEMVMX)
2153 *
2154 * Linux caps the semaphore value, both at 0
2155 * and at SEMVMX.
2156 *
Manfred Spraul239521f2014-01-27 17:07:04 -08002157 * Manfred <manfred@colorfullife.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 */
Ingo Molnar5f921ae2006-03-26 01:37:17 -08002159 if (semaphore->semval < 0)
2160 semaphore->semval = 0;
2161 if (semaphore->semval > SEMVMX)
2162 semaphore->semval = SEMVMX;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002163 semaphore->sempid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* maybe some queued-up processes were waiting for this */
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002167 INIT_LIST_HEAD(&tasks);
2168 do_smart_update(sma, NULL, 0, 1, &tasks);
Rik van Riel6062a8d2013-04-30 19:15:44 -07002169 sem_unlock(sma, -1);
Linus Torvalds6d49dab2013-05-03 15:04:40 -07002170 rcu_read_unlock();
Manfred Spraul0a2b9d42010-05-26 14:43:41 -07002171 wake_up_sem_queue_do(&tasks);
Manfred Spraul380af1b2008-07-25 01:48:06 -07002172
Lai Jiangshan693a8b62011-03-18 12:09:35 +08002173 kfree_rcu(un, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
Manfred Spraul4daa28f2008-07-25 01:48:04 -07002175 kfree(ulp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
2178#ifdef CONFIG_PROC_FS
Mike Waychison19b49462005-09-06 15:17:10 -07002179static int sysvipc_sem_proc_show(struct seq_file *s, void *it)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Eric W. Biederman1efdb692012-02-07 16:54:11 -08002181 struct user_namespace *user_ns = seq_user_ns(s);
Mike Waychison19b49462005-09-06 15:17:10 -07002182 struct sem_array *sma = it;
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002183 time_t sem_otime;
2184
Manfred Sprauld8c63372013-09-30 13:45:07 -07002185 /*
2186 * The proc interface isn't aware of sem_lock(), it calls
2187 * ipc_lock_object() directly (in sysvipc_find_ipc).
2188 * In order to stay compatible with sem_lock(), we must wait until
2189 * all simple semop() calls have left their critical regions.
2190 */
2191 sem_wait_array(sma);
2192
Manfred Sprauld12e1e52013-07-08 16:01:25 -07002193 sem_otime = get_semotime(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Joe Perches7f032d62015-04-15 16:17:54 -07002195 seq_printf(s,
2196 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
2197 sma->sem_perm.key,
2198 sma->sem_perm.id,
2199 sma->sem_perm.mode,
2200 sma->sem_nsems,
2201 from_kuid_munged(user_ns, sma->sem_perm.uid),
2202 from_kgid_munged(user_ns, sma->sem_perm.gid),
2203 from_kuid_munged(user_ns, sma->sem_perm.cuid),
2204 from_kgid_munged(user_ns, sma->sem_perm.cgid),
2205 sem_otime,
2206 sma->sem_ctime);
2207
2208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210#endif