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