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