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