]>
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 | * | |
6 | * IMPLEMENTATION NOTES ON CODE REWRITE (Eric Schenk, January 1995): | |
7 | * This code underwent a massive rewrite in order to solve some problems | |
8 | * with the original code. In particular the original code failed to | |
9 | * wake up processes that were waiting for semval to go to 0 if the | |
10 | * value went to 0 and was then incremented rapidly enough. In solving | |
11 | * this problem I have also modified the implementation so that it | |
12 | * processes pending operations in a FIFO manner, thus give a guarantee | |
13 | * that processes waiting for a lock on the semaphore won't starve | |
14 | * unless another locking process fails to unlock. | |
15 | * In addition the following two changes in behavior have been introduced: | |
16 | * - The original implementation of semop returned the value | |
17 | * last semaphore element examined on success. This does not | |
18 | * match the manual page specifications, and effectively | |
19 | * allows the user to read the semaphore even if they do not | |
20 | * have read permissions. The implementation now returns 0 | |
21 | * on success as stated in the manual page. | |
22 | * - There is some confusion over whether the set of undo adjustments | |
23 | * to be performed at exit should be done in an atomic manner. | |
24 | * That is, if we are attempting to decrement the semval should we queue | |
25 | * up and wait until we can do so legally? | |
26 | * The original implementation attempted to do this. | |
27 | * The current implementation does not do so. This is because I don't | |
28 | * think it is the right thing (TM) to do, and because I couldn't | |
29 | * see a clean way to get the old behavior with the new design. | |
30 | * The POSIX standard and SVID should be consulted to determine | |
31 | * what behavior is mandated. | |
32 | * | |
33 | * Further notes on refinement (Christoph Rohland, December 1998): | |
34 | * - The POSIX standard says, that the undo adjustments simply should | |
35 | * redo. So the current implementation is o.K. | |
36 | * - The previous code had two flaws: | |
37 | * 1) It actively gave the semaphore to the next waiting process | |
38 | * sleeping on the semaphore. Since this process did not have the | |
39 | * cpu this led to many unnecessary context switches and bad | |
40 | * performance. Now we only check which process should be able to | |
41 | * get the semaphore and if this process wants to reduce some | |
42 | * semaphore value we simply wake it up without doing the | |
43 | * operation. So it has to try to get it later. Thus e.g. the | |
44 | * running process may reacquire the semaphore during the current | |
45 | * time slice. If it only waits for zero or increases the semaphore, | |
46 | * we do the operation in advance and wake it up. | |
47 | * 2) It did not wake up all zero waiting processes. We try to do | |
48 | * better but only get the semops right which only wait for zero or | |
49 | * increase. If there are decrement operations in the operations | |
50 | * array we do the same as before. | |
51 | * | |
52 | * With the incarnation of O(1) scheduler, it becomes unnecessary to perform | |
53 | * check/retry algorithm for waking up blocked processes as the new scheduler | |
54 | * is better at handling thread switch than the old one. | |
55 | * | |
56 | * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com> | |
57 | * | |
58 | * SMP-threaded, sysctl's added | |
624dffcb | 59 | * (c) 1999 Manfred Spraul <manfred@colorfullife.com> |
1da177e4 LT |
60 | * Enforced range limit on SEM_UNDO |
61 | * (c) 2001 Red Hat Inc <alan@redhat.com> | |
62 | * Lockless wakeup | |
63 | * (c) 2003 Manfred Spraul <manfred@colorfullife.com> | |
073115d6 SG |
64 | * |
65 | * support for audit of ipc object properties and permission changes | |
66 | * Dustin Kirkland <dustin.kirkland@us.ibm.com> | |
e3893534 KK |
67 | * |
68 | * namespaces support | |
69 | * OpenVZ, SWsoft Inc. | |
70 | * Pavel Emelianov <xemul@openvz.org> | |
1da177e4 LT |
71 | */ |
72 | ||
1da177e4 LT |
73 | #include <linux/slab.h> |
74 | #include <linux/spinlock.h> | |
75 | #include <linux/init.h> | |
76 | #include <linux/proc_fs.h> | |
77 | #include <linux/time.h> | |
1da177e4 LT |
78 | #include <linux/security.h> |
79 | #include <linux/syscalls.h> | |
80 | #include <linux/audit.h> | |
c59ede7b | 81 | #include <linux/capability.h> |
19b4946c | 82 | #include <linux/seq_file.h> |
3e148c79 | 83 | #include <linux/rwsem.h> |
e3893534 | 84 | #include <linux/nsproxy.h> |
ae5e1b22 | 85 | #include <linux/ipc_namespace.h> |
5f921ae9 | 86 | |
1da177e4 LT |
87 | #include <asm/uaccess.h> |
88 | #include "util.h" | |
89 | ||
ed2ddbf8 | 90 | #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS]) |
e3893534 | 91 | |
e3893534 | 92 | #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm) |
1b531f21 ND |
93 | #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid) |
94 | #define sem_buildid(id, seq) ipc_buildid(id, seq) | |
1da177e4 | 95 | |
7748dbfa | 96 | static int newary(struct ipc_namespace *, struct ipc_params *); |
01b8b07a | 97 | static void freeary(struct ipc_namespace *, struct kern_ipc_perm *); |
1da177e4 | 98 | #ifdef CONFIG_PROC_FS |
19b4946c | 99 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it); |
1da177e4 LT |
100 | #endif |
101 | ||
102 | #define SEMMSL_FAST 256 /* 512 bytes on stack */ | |
103 | #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */ | |
104 | ||
105 | /* | |
106 | * linked list protection: | |
107 | * sem_undo.id_next, | |
108 | * sem_array.sem_pending{,last}, | |
109 | * sem_array.sem_undo: sem_lock() for read/write | |
110 | * sem_undo.proc_next: only "current" is allowed to read/write that field. | |
111 | * | |
112 | */ | |
113 | ||
e3893534 KK |
114 | #define sc_semmsl sem_ctls[0] |
115 | #define sc_semmns sem_ctls[1] | |
116 | #define sc_semopm sem_ctls[2] | |
117 | #define sc_semmni sem_ctls[3] | |
118 | ||
ed2ddbf8 | 119 | void sem_init_ns(struct ipc_namespace *ns) |
e3893534 | 120 | { |
e3893534 KK |
121 | ns->sc_semmsl = SEMMSL; |
122 | ns->sc_semmns = SEMMNS; | |
123 | ns->sc_semopm = SEMOPM; | |
124 | ns->sc_semmni = SEMMNI; | |
125 | ns->used_sems = 0; | |
ed2ddbf8 | 126 | ipc_init_ids(&ns->ids[IPC_SEM_IDS]); |
e3893534 KK |
127 | } |
128 | ||
ae5e1b22 | 129 | #ifdef CONFIG_IPC_NS |
e3893534 KK |
130 | void sem_exit_ns(struct ipc_namespace *ns) |
131 | { | |
01b8b07a | 132 | free_ipcs(ns, &sem_ids(ns), freeary); |
e3893534 | 133 | } |
ae5e1b22 | 134 | #endif |
1da177e4 LT |
135 | |
136 | void __init sem_init (void) | |
137 | { | |
ed2ddbf8 | 138 | sem_init_ns(&init_ipc_ns); |
19b4946c MW |
139 | ipc_init_proc_interface("sysvipc/sem", |
140 | " key semid perms nsems uid gid cuid cgid otime ctime\n", | |
e3893534 | 141 | IPC_SEM_IDS, sysvipc_sem_proc_show); |
1da177e4 LT |
142 | } |
143 | ||
3e148c79 ND |
144 | /* |
145 | * This routine is called in the paths where the rw_mutex is held to protect | |
146 | * access to the idr tree. | |
147 | */ | |
148 | static inline struct sem_array *sem_lock_check_down(struct ipc_namespace *ns, | |
149 | int id) | |
150 | { | |
151 | struct kern_ipc_perm *ipcp = ipc_lock_check_down(&sem_ids(ns), id); | |
152 | ||
b1ed88b4 PP |
153 | if (IS_ERR(ipcp)) |
154 | return (struct sem_array *)ipcp; | |
155 | ||
3e148c79 ND |
156 | return container_of(ipcp, struct sem_array, sem_perm); |
157 | } | |
158 | ||
159 | /* | |
160 | * sem_lock_(check_) routines are called in the paths where the rw_mutex | |
161 | * is not held. | |
162 | */ | |
023a5355 ND |
163 | static inline struct sem_array *sem_lock(struct ipc_namespace *ns, int id) |
164 | { | |
03f02c76 ND |
165 | struct kern_ipc_perm *ipcp = ipc_lock(&sem_ids(ns), id); |
166 | ||
b1ed88b4 PP |
167 | if (IS_ERR(ipcp)) |
168 | return (struct sem_array *)ipcp; | |
169 | ||
03f02c76 | 170 | return container_of(ipcp, struct sem_array, sem_perm); |
023a5355 ND |
171 | } |
172 | ||
173 | static inline struct sem_array *sem_lock_check(struct ipc_namespace *ns, | |
174 | int id) | |
175 | { | |
03f02c76 ND |
176 | struct kern_ipc_perm *ipcp = ipc_lock_check(&sem_ids(ns), id); |
177 | ||
b1ed88b4 PP |
178 | if (IS_ERR(ipcp)) |
179 | return (struct sem_array *)ipcp; | |
180 | ||
03f02c76 | 181 | return container_of(ipcp, struct sem_array, sem_perm); |
023a5355 ND |
182 | } |
183 | ||
7ca7e564 ND |
184 | static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s) |
185 | { | |
186 | ipc_rmid(&sem_ids(ns), &s->sem_perm); | |
187 | } | |
188 | ||
1da177e4 LT |
189 | /* |
190 | * Lockless wakeup algorithm: | |
191 | * Without the check/retry algorithm a lockless wakeup is possible: | |
192 | * - queue.status is initialized to -EINTR before blocking. | |
193 | * - wakeup is performed by | |
194 | * * unlinking the queue entry from sma->sem_pending | |
195 | * * setting queue.status to IN_WAKEUP | |
196 | * This is the notification for the blocked thread that a | |
197 | * result value is imminent. | |
198 | * * call wake_up_process | |
199 | * * set queue.status to the final value. | |
200 | * - the previously blocked thread checks queue.status: | |
201 | * * if it's IN_WAKEUP, then it must wait until the value changes | |
202 | * * if it's not -EINTR, then the operation was completed by | |
203 | * update_queue. semtimedop can return queue.status without | |
5f921ae9 | 204 | * performing any operation on the sem array. |
1da177e4 LT |
205 | * * otherwise it must acquire the spinlock and check what's up. |
206 | * | |
207 | * The two-stage algorithm is necessary to protect against the following | |
208 | * races: | |
209 | * - if queue.status is set after wake_up_process, then the woken up idle | |
210 | * thread could race forward and try (and fail) to acquire sma->lock | |
211 | * before update_queue had a chance to set queue.status | |
212 | * - if queue.status is written before wake_up_process and if the | |
213 | * blocked process is woken up by a signal between writing | |
214 | * queue.status and the wake_up_process, then the woken up | |
215 | * process could return from semtimedop and die by calling | |
216 | * sys_exit before wake_up_process is called. Then wake_up_process | |
217 | * will oops, because the task structure is already invalid. | |
218 | * (yes, this happened on s390 with sysv msg). | |
219 | * | |
220 | */ | |
221 | #define IN_WAKEUP 1 | |
222 | ||
f4566f04 ND |
223 | /** |
224 | * newary - Create a new semaphore set | |
225 | * @ns: namespace | |
226 | * @params: ptr to the structure that contains key, semflg and nsems | |
227 | * | |
3e148c79 | 228 | * Called with sem_ids.rw_mutex held (as a writer) |
f4566f04 ND |
229 | */ |
230 | ||
7748dbfa | 231 | static int newary(struct ipc_namespace *ns, struct ipc_params *params) |
1da177e4 LT |
232 | { |
233 | int id; | |
234 | int retval; | |
235 | struct sem_array *sma; | |
236 | int size; | |
7748dbfa ND |
237 | key_t key = params->key; |
238 | int nsems = params->u.nsems; | |
239 | int semflg = params->flg; | |
1da177e4 LT |
240 | |
241 | if (!nsems) | |
242 | return -EINVAL; | |
e3893534 | 243 | if (ns->used_sems + nsems > ns->sc_semmns) |
1da177e4 LT |
244 | return -ENOSPC; |
245 | ||
246 | size = sizeof (*sma) + nsems * sizeof (struct sem); | |
247 | sma = ipc_rcu_alloc(size); | |
248 | if (!sma) { | |
249 | return -ENOMEM; | |
250 | } | |
251 | memset (sma, 0, size); | |
252 | ||
253 | sma->sem_perm.mode = (semflg & S_IRWXUGO); | |
254 | sma->sem_perm.key = key; | |
255 | ||
256 | sma->sem_perm.security = NULL; | |
257 | retval = security_sem_alloc(sma); | |
258 | if (retval) { | |
259 | ipc_rcu_putref(sma); | |
260 | return retval; | |
261 | } | |
262 | ||
e3893534 | 263 | id = ipc_addid(&sem_ids(ns), &sma->sem_perm, ns->sc_semmni); |
283bb7fa | 264 | if (id < 0) { |
1da177e4 LT |
265 | security_sem_free(sma); |
266 | ipc_rcu_putref(sma); | |
283bb7fa | 267 | return id; |
1da177e4 | 268 | } |
e3893534 | 269 | ns->used_sems += nsems; |
1da177e4 | 270 | |
1b531f21 | 271 | sma->sem_perm.id = sem_buildid(id, sma->sem_perm.seq); |
1da177e4 LT |
272 | sma->sem_base = (struct sem *) &sma[1]; |
273 | /* sma->sem_pending = NULL; */ | |
274 | sma->sem_pending_last = &sma->sem_pending; | |
275 | /* sma->undo = NULL; */ | |
276 | sma->sem_nsems = nsems; | |
277 | sma->sem_ctime = get_seconds(); | |
278 | sem_unlock(sma); | |
279 | ||
7ca7e564 | 280 | return sma->sem_perm.id; |
1da177e4 LT |
281 | } |
282 | ||
7748dbfa | 283 | |
f4566f04 | 284 | /* |
3e148c79 | 285 | * Called with sem_ids.rw_mutex and ipcp locked. |
f4566f04 | 286 | */ |
03f02c76 | 287 | static inline int sem_security(struct kern_ipc_perm *ipcp, int semflg) |
7748dbfa | 288 | { |
03f02c76 ND |
289 | struct sem_array *sma; |
290 | ||
291 | sma = container_of(ipcp, struct sem_array, sem_perm); | |
292 | return security_sem_associate(sma, semflg); | |
7748dbfa ND |
293 | } |
294 | ||
f4566f04 | 295 | /* |
3e148c79 | 296 | * Called with sem_ids.rw_mutex and ipcp locked. |
f4566f04 | 297 | */ |
03f02c76 ND |
298 | static inline int sem_more_checks(struct kern_ipc_perm *ipcp, |
299 | struct ipc_params *params) | |
7748dbfa | 300 | { |
03f02c76 ND |
301 | struct sem_array *sma; |
302 | ||
303 | sma = container_of(ipcp, struct sem_array, sem_perm); | |
304 | if (params->u.nsems > sma->sem_nsems) | |
7748dbfa ND |
305 | return -EINVAL; |
306 | ||
307 | return 0; | |
308 | } | |
309 | ||
310 | asmlinkage long sys_semget(key_t key, int nsems, int semflg) | |
1da177e4 | 311 | { |
e3893534 | 312 | struct ipc_namespace *ns; |
7748dbfa ND |
313 | struct ipc_ops sem_ops; |
314 | struct ipc_params sem_params; | |
e3893534 KK |
315 | |
316 | ns = current->nsproxy->ipc_ns; | |
1da177e4 | 317 | |
e3893534 | 318 | if (nsems < 0 || nsems > ns->sc_semmsl) |
1da177e4 | 319 | return -EINVAL; |
7ca7e564 | 320 | |
7748dbfa ND |
321 | sem_ops.getnew = newary; |
322 | sem_ops.associate = sem_security; | |
323 | sem_ops.more_checks = sem_more_checks; | |
324 | ||
325 | sem_params.key = key; | |
326 | sem_params.flg = semflg; | |
327 | sem_params.u.nsems = nsems; | |
1da177e4 | 328 | |
7748dbfa | 329 | return ipcget(ns, &sem_ids(ns), &sem_ops, &sem_params); |
1da177e4 LT |
330 | } |
331 | ||
332 | /* Manage the doubly linked list sma->sem_pending as a FIFO: | |
333 | * insert new queue elements at the tail sma->sem_pending_last. | |
334 | */ | |
335 | static inline void append_to_queue (struct sem_array * sma, | |
336 | struct sem_queue * q) | |
337 | { | |
338 | *(q->prev = sma->sem_pending_last) = q; | |
339 | *(sma->sem_pending_last = &q->next) = NULL; | |
340 | } | |
341 | ||
342 | static inline void prepend_to_queue (struct sem_array * sma, | |
343 | struct sem_queue * q) | |
344 | { | |
345 | q->next = sma->sem_pending; | |
346 | *(q->prev = &sma->sem_pending) = q; | |
347 | if (q->next) | |
348 | q->next->prev = &q->next; | |
349 | else /* sma->sem_pending_last == &sma->sem_pending */ | |
350 | sma->sem_pending_last = &q->next; | |
351 | } | |
352 | ||
353 | static inline void remove_from_queue (struct sem_array * sma, | |
354 | struct sem_queue * q) | |
355 | { | |
356 | *(q->prev) = q->next; | |
357 | if (q->next) | |
358 | q->next->prev = q->prev; | |
359 | else /* sma->sem_pending_last == &q->next */ | |
360 | sma->sem_pending_last = q->prev; | |
361 | q->prev = NULL; /* mark as removed */ | |
362 | } | |
363 | ||
364 | /* | |
365 | * Determine whether a sequence of semaphore operations would succeed | |
366 | * all at once. Return 0 if yes, 1 if need to sleep, else return error code. | |
367 | */ | |
368 | ||
369 | static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops, | |
370 | int nsops, struct sem_undo *un, int pid) | |
371 | { | |
372 | int result, sem_op; | |
373 | struct sembuf *sop; | |
374 | struct sem * curr; | |
375 | ||
376 | for (sop = sops; sop < sops + nsops; sop++) { | |
377 | curr = sma->sem_base + sop->sem_num; | |
378 | sem_op = sop->sem_op; | |
379 | result = curr->semval; | |
380 | ||
381 | if (!sem_op && result) | |
382 | goto would_block; | |
383 | ||
384 | result += sem_op; | |
385 | if (result < 0) | |
386 | goto would_block; | |
387 | if (result > SEMVMX) | |
388 | goto out_of_range; | |
389 | if (sop->sem_flg & SEM_UNDO) { | |
390 | int undo = un->semadj[sop->sem_num] - sem_op; | |
391 | /* | |
392 | * Exceeding the undo range is an error. | |
393 | */ | |
394 | if (undo < (-SEMAEM - 1) || undo > SEMAEM) | |
395 | goto out_of_range; | |
396 | } | |
397 | curr->semval = result; | |
398 | } | |
399 | ||
400 | sop--; | |
401 | while (sop >= sops) { | |
402 | sma->sem_base[sop->sem_num].sempid = pid; | |
403 | if (sop->sem_flg & SEM_UNDO) | |
404 | un->semadj[sop->sem_num] -= sop->sem_op; | |
405 | sop--; | |
406 | } | |
407 | ||
408 | sma->sem_otime = get_seconds(); | |
409 | return 0; | |
410 | ||
411 | out_of_range: | |
412 | result = -ERANGE; | |
413 | goto undo; | |
414 | ||
415 | would_block: | |
416 | if (sop->sem_flg & IPC_NOWAIT) | |
417 | result = -EAGAIN; | |
418 | else | |
419 | result = 1; | |
420 | ||
421 | undo: | |
422 | sop--; | |
423 | while (sop >= sops) { | |
424 | sma->sem_base[sop->sem_num].semval -= sop->sem_op; | |
425 | sop--; | |
426 | } | |
427 | ||
428 | return result; | |
429 | } | |
430 | ||
431 | /* Go through the pending queue for the indicated semaphore | |
432 | * looking for tasks that can be completed. | |
433 | */ | |
434 | static void update_queue (struct sem_array * sma) | |
435 | { | |
436 | int error; | |
437 | struct sem_queue * q; | |
438 | ||
439 | q = sma->sem_pending; | |
440 | while(q) { | |
441 | error = try_atomic_semop(sma, q->sops, q->nsops, | |
442 | q->undo, q->pid); | |
443 | ||
444 | /* Does q->sleeper still need to sleep? */ | |
445 | if (error <= 0) { | |
446 | struct sem_queue *n; | |
447 | remove_from_queue(sma,q); | |
448 | q->status = IN_WAKEUP; | |
449 | /* | |
450 | * Continue scanning. The next operation | |
451 | * that must be checked depends on the type of the | |
452 | * completed operation: | |
453 | * - if the operation modified the array, then | |
454 | * restart from the head of the queue and | |
455 | * check for threads that might be waiting | |
456 | * for semaphore values to become 0. | |
457 | * - if the operation didn't modify the array, | |
458 | * then just continue. | |
459 | */ | |
460 | if (q->alter) | |
461 | n = sma->sem_pending; | |
462 | else | |
463 | n = q->next; | |
464 | wake_up_process(q->sleeper); | |
465 | /* hands-off: q will disappear immediately after | |
466 | * writing q->status. | |
467 | */ | |
1224b375 | 468 | smp_wmb(); |
1da177e4 LT |
469 | q->status = error; |
470 | q = n; | |
471 | } else { | |
472 | q = q->next; | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | /* The following counts are associated to each semaphore: | |
478 | * semncnt number of tasks waiting on semval being nonzero | |
479 | * semzcnt number of tasks waiting on semval being zero | |
480 | * This model assumes that a task waits on exactly one semaphore. | |
481 | * Since semaphore operations are to be performed atomically, tasks actually | |
482 | * wait on a whole sequence of semaphores simultaneously. | |
483 | * The counts we return here are a rough approximation, but still | |
484 | * warrant that semncnt+semzcnt>0 if the task is on the pending queue. | |
485 | */ | |
486 | static int count_semncnt (struct sem_array * sma, ushort semnum) | |
487 | { | |
488 | int semncnt; | |
489 | struct sem_queue * q; | |
490 | ||
491 | semncnt = 0; | |
492 | for (q = sma->sem_pending; q; q = q->next) { | |
493 | struct sembuf * sops = q->sops; | |
494 | int nsops = q->nsops; | |
495 | int i; | |
496 | for (i = 0; i < nsops; i++) | |
497 | if (sops[i].sem_num == semnum | |
498 | && (sops[i].sem_op < 0) | |
499 | && !(sops[i].sem_flg & IPC_NOWAIT)) | |
500 | semncnt++; | |
501 | } | |
502 | return semncnt; | |
503 | } | |
504 | static int count_semzcnt (struct sem_array * sma, ushort semnum) | |
505 | { | |
506 | int semzcnt; | |
507 | struct sem_queue * q; | |
508 | ||
509 | semzcnt = 0; | |
510 | for (q = sma->sem_pending; q; q = q->next) { | |
511 | struct sembuf * sops = q->sops; | |
512 | int nsops = q->nsops; | |
513 | int i; | |
514 | for (i = 0; i < nsops; i++) | |
515 | if (sops[i].sem_num == semnum | |
516 | && (sops[i].sem_op == 0) | |
517 | && !(sops[i].sem_flg & IPC_NOWAIT)) | |
518 | semzcnt++; | |
519 | } | |
520 | return semzcnt; | |
521 | } | |
522 | ||
3e148c79 ND |
523 | /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked |
524 | * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex | |
525 | * remains locked on exit. | |
1da177e4 | 526 | */ |
01b8b07a | 527 | static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp) |
1da177e4 LT |
528 | { |
529 | struct sem_undo *un; | |
530 | struct sem_queue *q; | |
01b8b07a | 531 | struct sem_array *sma = container_of(ipcp, struct sem_array, sem_perm); |
1da177e4 LT |
532 | |
533 | /* Invalidate the existing undo structures for this semaphore set. | |
534 | * (They will be freed without any further action in exit_sem() | |
535 | * or during the next semop.) | |
536 | */ | |
537 | for (un = sma->undo; un; un = un->id_next) | |
538 | un->semid = -1; | |
539 | ||
540 | /* Wake up all pending processes and let them fail with EIDRM. */ | |
541 | q = sma->sem_pending; | |
542 | while(q) { | |
543 | struct sem_queue *n; | |
544 | /* lazy remove_from_queue: we are killing the whole queue */ | |
545 | q->prev = NULL; | |
546 | n = q->next; | |
547 | q->status = IN_WAKEUP; | |
548 | wake_up_process(q->sleeper); /* doesn't sleep */ | |
6003a93e | 549 | smp_wmb(); |
1da177e4 LT |
550 | q->status = -EIDRM; /* hands-off q */ |
551 | q = n; | |
552 | } | |
553 | ||
7ca7e564 ND |
554 | /* Remove the semaphore set from the IDR */ |
555 | sem_rmid(ns, sma); | |
1da177e4 LT |
556 | sem_unlock(sma); |
557 | ||
e3893534 | 558 | ns->used_sems -= sma->sem_nsems; |
1da177e4 LT |
559 | security_sem_free(sma); |
560 | ipc_rcu_putref(sma); | |
561 | } | |
562 | ||
563 | static unsigned long copy_semid_to_user(void __user *buf, struct semid64_ds *in, int version) | |
564 | { | |
565 | switch(version) { | |
566 | case IPC_64: | |
567 | return copy_to_user(buf, in, sizeof(*in)); | |
568 | case IPC_OLD: | |
569 | { | |
570 | struct semid_ds out; | |
571 | ||
572 | ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm); | |
573 | ||
574 | out.sem_otime = in->sem_otime; | |
575 | out.sem_ctime = in->sem_ctime; | |
576 | out.sem_nsems = in->sem_nsems; | |
577 | ||
578 | return copy_to_user(buf, &out, sizeof(out)); | |
579 | } | |
580 | default: | |
581 | return -EINVAL; | |
582 | } | |
583 | } | |
584 | ||
4b9fcb0e PP |
585 | static int semctl_nolock(struct ipc_namespace *ns, int semid, |
586 | int cmd, int version, union semun arg) | |
1da177e4 LT |
587 | { |
588 | int err = -EINVAL; | |
589 | struct sem_array *sma; | |
590 | ||
591 | switch(cmd) { | |
592 | case IPC_INFO: | |
593 | case SEM_INFO: | |
594 | { | |
595 | struct seminfo seminfo; | |
596 | int max_id; | |
597 | ||
598 | err = security_sem_semctl(NULL, cmd); | |
599 | if (err) | |
600 | return err; | |
601 | ||
602 | memset(&seminfo,0,sizeof(seminfo)); | |
e3893534 KK |
603 | seminfo.semmni = ns->sc_semmni; |
604 | seminfo.semmns = ns->sc_semmns; | |
605 | seminfo.semmsl = ns->sc_semmsl; | |
606 | seminfo.semopm = ns->sc_semopm; | |
1da177e4 LT |
607 | seminfo.semvmx = SEMVMX; |
608 | seminfo.semmnu = SEMMNU; | |
609 | seminfo.semmap = SEMMAP; | |
610 | seminfo.semume = SEMUME; | |
3e148c79 | 611 | down_read(&sem_ids(ns).rw_mutex); |
1da177e4 | 612 | if (cmd == SEM_INFO) { |
e3893534 KK |
613 | seminfo.semusz = sem_ids(ns).in_use; |
614 | seminfo.semaem = ns->used_sems; | |
1da177e4 LT |
615 | } else { |
616 | seminfo.semusz = SEMUSZ; | |
617 | seminfo.semaem = SEMAEM; | |
618 | } | |
7ca7e564 | 619 | max_id = ipc_get_maxid(&sem_ids(ns)); |
3e148c79 | 620 | up_read(&sem_ids(ns).rw_mutex); |
1da177e4 LT |
621 | if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo))) |
622 | return -EFAULT; | |
623 | return (max_id < 0) ? 0: max_id; | |
624 | } | |
4b9fcb0e | 625 | case IPC_STAT: |
1da177e4 LT |
626 | case SEM_STAT: |
627 | { | |
628 | struct semid64_ds tbuf; | |
629 | int id; | |
630 | ||
4b9fcb0e PP |
631 | if (cmd == SEM_STAT) { |
632 | sma = sem_lock(ns, semid); | |
633 | if (IS_ERR(sma)) | |
634 | return PTR_ERR(sma); | |
635 | id = sma->sem_perm.id; | |
636 | } else { | |
637 | sma = sem_lock_check(ns, semid); | |
638 | if (IS_ERR(sma)) | |
639 | return PTR_ERR(sma); | |
640 | id = 0; | |
641 | } | |
1da177e4 LT |
642 | |
643 | err = -EACCES; | |
644 | if (ipcperms (&sma->sem_perm, S_IRUGO)) | |
645 | goto out_unlock; | |
646 | ||
647 | err = security_sem_semctl(sma, cmd); | |
648 | if (err) | |
649 | goto out_unlock; | |
650 | ||
023a5355 ND |
651 | memset(&tbuf, 0, sizeof(tbuf)); |
652 | ||
1da177e4 LT |
653 | kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm); |
654 | tbuf.sem_otime = sma->sem_otime; | |
655 | tbuf.sem_ctime = sma->sem_ctime; | |
656 | tbuf.sem_nsems = sma->sem_nsems; | |
657 | sem_unlock(sma); | |
658 | if (copy_semid_to_user (arg.buf, &tbuf, version)) | |
659 | return -EFAULT; | |
660 | return id; | |
661 | } | |
662 | default: | |
663 | return -EINVAL; | |
664 | } | |
665 | return err; | |
666 | out_unlock: | |
667 | sem_unlock(sma); | |
668 | return err; | |
669 | } | |
670 | ||
e3893534 KK |
671 | static int semctl_main(struct ipc_namespace *ns, int semid, int semnum, |
672 | int cmd, int version, union semun arg) | |
1da177e4 LT |
673 | { |
674 | struct sem_array *sma; | |
675 | struct sem* curr; | |
676 | int err; | |
677 | ushort fast_sem_io[SEMMSL_FAST]; | |
678 | ushort* sem_io = fast_sem_io; | |
679 | int nsems; | |
680 | ||
023a5355 ND |
681 | sma = sem_lock_check(ns, semid); |
682 | if (IS_ERR(sma)) | |
683 | return PTR_ERR(sma); | |
1da177e4 LT |
684 | |
685 | nsems = sma->sem_nsems; | |
686 | ||
1da177e4 LT |
687 | err = -EACCES; |
688 | if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO)) | |
689 | goto out_unlock; | |
690 | ||
691 | err = security_sem_semctl(sma, cmd); | |
692 | if (err) | |
693 | goto out_unlock; | |
694 | ||
695 | err = -EACCES; | |
696 | switch (cmd) { | |
697 | case GETALL: | |
698 | { | |
699 | ushort __user *array = arg.array; | |
700 | int i; | |
701 | ||
702 | if(nsems > SEMMSL_FAST) { | |
703 | ipc_rcu_getref(sma); | |
704 | sem_unlock(sma); | |
705 | ||
706 | sem_io = ipc_alloc(sizeof(ushort)*nsems); | |
707 | if(sem_io == NULL) { | |
708 | ipc_lock_by_ptr(&sma->sem_perm); | |
709 | ipc_rcu_putref(sma); | |
710 | sem_unlock(sma); | |
711 | return -ENOMEM; | |
712 | } | |
713 | ||
714 | ipc_lock_by_ptr(&sma->sem_perm); | |
715 | ipc_rcu_putref(sma); | |
716 | if (sma->sem_perm.deleted) { | |
717 | sem_unlock(sma); | |
718 | err = -EIDRM; | |
719 | goto out_free; | |
720 | } | |
721 | } | |
722 | ||
723 | for (i = 0; i < sma->sem_nsems; i++) | |
724 | sem_io[i] = sma->sem_base[i].semval; | |
725 | sem_unlock(sma); | |
726 | err = 0; | |
727 | if(copy_to_user(array, sem_io, nsems*sizeof(ushort))) | |
728 | err = -EFAULT; | |
729 | goto out_free; | |
730 | } | |
731 | case SETALL: | |
732 | { | |
733 | int i; | |
734 | struct sem_undo *un; | |
735 | ||
736 | ipc_rcu_getref(sma); | |
737 | sem_unlock(sma); | |
738 | ||
739 | if(nsems > SEMMSL_FAST) { | |
740 | sem_io = ipc_alloc(sizeof(ushort)*nsems); | |
741 | if(sem_io == NULL) { | |
742 | ipc_lock_by_ptr(&sma->sem_perm); | |
743 | ipc_rcu_putref(sma); | |
744 | sem_unlock(sma); | |
745 | return -ENOMEM; | |
746 | } | |
747 | } | |
748 | ||
749 | if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) { | |
750 | ipc_lock_by_ptr(&sma->sem_perm); | |
751 | ipc_rcu_putref(sma); | |
752 | sem_unlock(sma); | |
753 | err = -EFAULT; | |
754 | goto out_free; | |
755 | } | |
756 | ||
757 | for (i = 0; i < nsems; i++) { | |
758 | if (sem_io[i] > SEMVMX) { | |
759 | ipc_lock_by_ptr(&sma->sem_perm); | |
760 | ipc_rcu_putref(sma); | |
761 | sem_unlock(sma); | |
762 | err = -ERANGE; | |
763 | goto out_free; | |
764 | } | |
765 | } | |
766 | ipc_lock_by_ptr(&sma->sem_perm); | |
767 | ipc_rcu_putref(sma); | |
768 | if (sma->sem_perm.deleted) { | |
769 | sem_unlock(sma); | |
770 | err = -EIDRM; | |
771 | goto out_free; | |
772 | } | |
773 | ||
774 | for (i = 0; i < nsems; i++) | |
775 | sma->sem_base[i].semval = sem_io[i]; | |
776 | for (un = sma->undo; un; un = un->id_next) | |
777 | for (i = 0; i < nsems; i++) | |
778 | un->semadj[i] = 0; | |
779 | sma->sem_ctime = get_seconds(); | |
780 | /* maybe some queued-up processes were waiting for this */ | |
781 | update_queue(sma); | |
782 | err = 0; | |
783 | goto out_unlock; | |
784 | } | |
1da177e4 LT |
785 | /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */ |
786 | } | |
787 | err = -EINVAL; | |
788 | if(semnum < 0 || semnum >= nsems) | |
789 | goto out_unlock; | |
790 | ||
791 | curr = &sma->sem_base[semnum]; | |
792 | ||
793 | switch (cmd) { | |
794 | case GETVAL: | |
795 | err = curr->semval; | |
796 | goto out_unlock; | |
797 | case GETPID: | |
798 | err = curr->sempid; | |
799 | goto out_unlock; | |
800 | case GETNCNT: | |
801 | err = count_semncnt(sma,semnum); | |
802 | goto out_unlock; | |
803 | case GETZCNT: | |
804 | err = count_semzcnt(sma,semnum); | |
805 | goto out_unlock; | |
806 | case SETVAL: | |
807 | { | |
808 | int val = arg.val; | |
809 | struct sem_undo *un; | |
810 | err = -ERANGE; | |
811 | if (val > SEMVMX || val < 0) | |
812 | goto out_unlock; | |
813 | ||
814 | for (un = sma->undo; un; un = un->id_next) | |
815 | un->semadj[semnum] = 0; | |
816 | curr->semval = val; | |
b488893a | 817 | curr->sempid = task_tgid_vnr(current); |
1da177e4 LT |
818 | sma->sem_ctime = get_seconds(); |
819 | /* maybe some queued-up processes were waiting for this */ | |
820 | update_queue(sma); | |
821 | err = 0; | |
822 | goto out_unlock; | |
823 | } | |
824 | } | |
825 | out_unlock: | |
826 | sem_unlock(sma); | |
827 | out_free: | |
828 | if(sem_io != fast_sem_io) | |
829 | ipc_free(sem_io, sizeof(ushort)*nsems); | |
830 | return err; | |
831 | } | |
832 | ||
833 | struct sem_setbuf { | |
834 | uid_t uid; | |
835 | gid_t gid; | |
836 | mode_t mode; | |
837 | }; | |
838 | ||
839 | static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void __user *buf, int version) | |
840 | { | |
841 | switch(version) { | |
842 | case IPC_64: | |
843 | { | |
844 | struct semid64_ds tbuf; | |
845 | ||
846 | if(copy_from_user(&tbuf, buf, sizeof(tbuf))) | |
847 | return -EFAULT; | |
848 | ||
849 | out->uid = tbuf.sem_perm.uid; | |
850 | out->gid = tbuf.sem_perm.gid; | |
851 | out->mode = tbuf.sem_perm.mode; | |
852 | ||
853 | return 0; | |
854 | } | |
855 | case IPC_OLD: | |
856 | { | |
857 | struct semid_ds tbuf_old; | |
858 | ||
859 | if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old))) | |
860 | return -EFAULT; | |
861 | ||
862 | out->uid = tbuf_old.sem_perm.uid; | |
863 | out->gid = tbuf_old.sem_perm.gid; | |
864 | out->mode = tbuf_old.sem_perm.mode; | |
865 | ||
866 | return 0; | |
867 | } | |
868 | default: | |
869 | return -EINVAL; | |
870 | } | |
871 | } | |
872 | ||
e3893534 KK |
873 | static int semctl_down(struct ipc_namespace *ns, int semid, int semnum, |
874 | int cmd, int version, union semun arg) | |
1da177e4 LT |
875 | { |
876 | struct sem_array *sma; | |
877 | int err; | |
8e1c091c | 878 | struct sem_setbuf uninitialized_var(setbuf); |
1da177e4 LT |
879 | struct kern_ipc_perm *ipcp; |
880 | ||
881 | if(cmd == IPC_SET) { | |
882 | if(copy_semid_from_user (&setbuf, arg.buf, version)) | |
883 | return -EFAULT; | |
1da177e4 | 884 | } |
3e148c79 | 885 | sma = sem_lock_check_down(ns, semid); |
023a5355 ND |
886 | if (IS_ERR(sma)) |
887 | return PTR_ERR(sma); | |
1da177e4 | 888 | |
1da177e4 | 889 | ipcp = &sma->sem_perm; |
073115d6 SG |
890 | |
891 | err = audit_ipc_obj(ipcp); | |
892 | if (err) | |
893 | goto out_unlock; | |
894 | ||
ac03221a LK |
895 | if (cmd == IPC_SET) { |
896 | err = audit_ipc_set_perm(0, setbuf.uid, setbuf.gid, setbuf.mode); | |
897 | if (err) | |
898 | goto out_unlock; | |
899 | } | |
1da177e4 LT |
900 | if (current->euid != ipcp->cuid && |
901 | current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) { | |
902 | err=-EPERM; | |
903 | goto out_unlock; | |
904 | } | |
905 | ||
906 | err = security_sem_semctl(sma, cmd); | |
907 | if (err) | |
908 | goto out_unlock; | |
909 | ||
910 | switch(cmd){ | |
911 | case IPC_RMID: | |
01b8b07a | 912 | freeary(ns, ipcp); |
1da177e4 LT |
913 | err = 0; |
914 | break; | |
915 | case IPC_SET: | |
916 | ipcp->uid = setbuf.uid; | |
917 | ipcp->gid = setbuf.gid; | |
918 | ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | |
919 | | (setbuf.mode & S_IRWXUGO); | |
920 | sma->sem_ctime = get_seconds(); | |
921 | sem_unlock(sma); | |
922 | err = 0; | |
923 | break; | |
924 | default: | |
925 | sem_unlock(sma); | |
926 | err = -EINVAL; | |
927 | break; | |
928 | } | |
929 | return err; | |
930 | ||
931 | out_unlock: | |
932 | sem_unlock(sma); | |
933 | return err; | |
934 | } | |
935 | ||
936 | asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg) | |
937 | { | |
938 | int err = -EINVAL; | |
939 | int version; | |
e3893534 | 940 | struct ipc_namespace *ns; |
1da177e4 LT |
941 | |
942 | if (semid < 0) | |
943 | return -EINVAL; | |
944 | ||
945 | version = ipc_parse_version(&cmd); | |
e3893534 | 946 | ns = current->nsproxy->ipc_ns; |
1da177e4 LT |
947 | |
948 | switch(cmd) { | |
949 | case IPC_INFO: | |
950 | case SEM_INFO: | |
4b9fcb0e | 951 | case IPC_STAT: |
1da177e4 | 952 | case SEM_STAT: |
4b9fcb0e | 953 | err = semctl_nolock(ns, semid, cmd, version, arg); |
1da177e4 LT |
954 | return err; |
955 | case GETALL: | |
956 | case GETVAL: | |
957 | case GETPID: | |
958 | case GETNCNT: | |
959 | case GETZCNT: | |
1da177e4 LT |
960 | case SETVAL: |
961 | case SETALL: | |
e3893534 | 962 | err = semctl_main(ns,semid,semnum,cmd,version,arg); |
1da177e4 LT |
963 | return err; |
964 | case IPC_RMID: | |
965 | case IPC_SET: | |
3e148c79 | 966 | down_write(&sem_ids(ns).rw_mutex); |
e3893534 | 967 | err = semctl_down(ns,semid,semnum,cmd,version,arg); |
3e148c79 | 968 | up_write(&sem_ids(ns).rw_mutex); |
1da177e4 LT |
969 | return err; |
970 | default: | |
971 | return -EINVAL; | |
972 | } | |
973 | } | |
974 | ||
1da177e4 LT |
975 | /* If the task doesn't already have a undo_list, then allocate one |
976 | * here. We guarantee there is only one thread using this undo list, | |
977 | * and current is THE ONE | |
978 | * | |
979 | * If this allocation and assignment succeeds, but later | |
980 | * portions of this code fail, there is no need to free the sem_undo_list. | |
981 | * Just let it stay associated with the task, and it'll be freed later | |
982 | * at exit time. | |
983 | * | |
984 | * This can block, so callers must hold no locks. | |
985 | */ | |
986 | static inline int get_undo_list(struct sem_undo_list **undo_listp) | |
987 | { | |
988 | struct sem_undo_list *undo_list; | |
1da177e4 LT |
989 | |
990 | undo_list = current->sysvsem.undo_list; | |
991 | if (!undo_list) { | |
2453a306 | 992 | undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL); |
1da177e4 LT |
993 | if (undo_list == NULL) |
994 | return -ENOMEM; | |
00a5dfdb | 995 | spin_lock_init(&undo_list->lock); |
1da177e4 LT |
996 | atomic_set(&undo_list->refcnt, 1); |
997 | current->sysvsem.undo_list = undo_list; | |
998 | } | |
999 | *undo_listp = undo_list; | |
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid) | |
1004 | { | |
1005 | struct sem_undo **last, *un; | |
1006 | ||
1007 | last = &ulp->proc_list; | |
1008 | un = *last; | |
1009 | while(un != NULL) { | |
1010 | if(un->semid==semid) | |
1011 | break; | |
1012 | if(un->semid==-1) { | |
1013 | *last=un->proc_next; | |
1014 | kfree(un); | |
1015 | } else { | |
1016 | last=&un->proc_next; | |
1017 | } | |
1018 | un=*last; | |
1019 | } | |
1020 | return un; | |
1021 | } | |
1022 | ||
e3893534 | 1023 | static struct sem_undo *find_undo(struct ipc_namespace *ns, int semid) |
1da177e4 LT |
1024 | { |
1025 | struct sem_array *sma; | |
1026 | struct sem_undo_list *ulp; | |
1027 | struct sem_undo *un, *new; | |
1028 | int nsems; | |
1029 | int error; | |
1030 | ||
1031 | error = get_undo_list(&ulp); | |
1032 | if (error) | |
1033 | return ERR_PTR(error); | |
1034 | ||
c530c6ac | 1035 | spin_lock(&ulp->lock); |
1da177e4 | 1036 | un = lookup_undo(ulp, semid); |
c530c6ac | 1037 | spin_unlock(&ulp->lock); |
1da177e4 LT |
1038 | if (likely(un!=NULL)) |
1039 | goto out; | |
1040 | ||
1041 | /* no undo structure around - allocate one. */ | |
023a5355 ND |
1042 | sma = sem_lock_check(ns, semid); |
1043 | if (IS_ERR(sma)) | |
1044 | return ERR_PTR(PTR_ERR(sma)); | |
1045 | ||
1da177e4 LT |
1046 | nsems = sma->sem_nsems; |
1047 | ipc_rcu_getref(sma); | |
1048 | sem_unlock(sma); | |
1049 | ||
4668edc3 | 1050 | new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL); |
1da177e4 LT |
1051 | if (!new) { |
1052 | ipc_lock_by_ptr(&sma->sem_perm); | |
1053 | ipc_rcu_putref(sma); | |
1054 | sem_unlock(sma); | |
1055 | return ERR_PTR(-ENOMEM); | |
1056 | } | |
1da177e4 LT |
1057 | new->semadj = (short *) &new[1]; |
1058 | new->semid = semid; | |
1059 | ||
c530c6ac | 1060 | spin_lock(&ulp->lock); |
1da177e4 LT |
1061 | un = lookup_undo(ulp, semid); |
1062 | if (un) { | |
c530c6ac | 1063 | spin_unlock(&ulp->lock); |
1da177e4 LT |
1064 | kfree(new); |
1065 | ipc_lock_by_ptr(&sma->sem_perm); | |
1066 | ipc_rcu_putref(sma); | |
1067 | sem_unlock(sma); | |
1068 | goto out; | |
1069 | } | |
1070 | ipc_lock_by_ptr(&sma->sem_perm); | |
1071 | ipc_rcu_putref(sma); | |
1072 | if (sma->sem_perm.deleted) { | |
1073 | sem_unlock(sma); | |
c530c6ac | 1074 | spin_unlock(&ulp->lock); |
1da177e4 LT |
1075 | kfree(new); |
1076 | un = ERR_PTR(-EIDRM); | |
1077 | goto out; | |
1078 | } | |
1079 | new->proc_next = ulp->proc_list; | |
1080 | ulp->proc_list = new; | |
1081 | new->id_next = sma->undo; | |
1082 | sma->undo = new; | |
1083 | sem_unlock(sma); | |
1084 | un = new; | |
c530c6ac | 1085 | spin_unlock(&ulp->lock); |
1da177e4 LT |
1086 | out: |
1087 | return un; | |
1088 | } | |
1089 | ||
1090 | asmlinkage long sys_semtimedop(int semid, struct sembuf __user *tsops, | |
1091 | unsigned nsops, const struct timespec __user *timeout) | |
1092 | { | |
1093 | int error = -EINVAL; | |
1094 | struct sem_array *sma; | |
1095 | struct sembuf fast_sops[SEMOPM_FAST]; | |
1096 | struct sembuf* sops = fast_sops, *sop; | |
1097 | struct sem_undo *un; | |
b78755ab | 1098 | int undos = 0, alter = 0, max; |
1da177e4 LT |
1099 | struct sem_queue queue; |
1100 | unsigned long jiffies_left = 0; | |
e3893534 KK |
1101 | struct ipc_namespace *ns; |
1102 | ||
1103 | ns = current->nsproxy->ipc_ns; | |
1da177e4 LT |
1104 | |
1105 | if (nsops < 1 || semid < 0) | |
1106 | return -EINVAL; | |
e3893534 | 1107 | if (nsops > ns->sc_semopm) |
1da177e4 LT |
1108 | return -E2BIG; |
1109 | if(nsops > SEMOPM_FAST) { | |
1110 | sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL); | |
1111 | if(sops==NULL) | |
1112 | return -ENOMEM; | |
1113 | } | |
1114 | if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) { | |
1115 | error=-EFAULT; | |
1116 | goto out_free; | |
1117 | } | |
1118 | if (timeout) { | |
1119 | struct timespec _timeout; | |
1120 | if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) { | |
1121 | error = -EFAULT; | |
1122 | goto out_free; | |
1123 | } | |
1124 | if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 || | |
1125 | _timeout.tv_nsec >= 1000000000L) { | |
1126 | error = -EINVAL; | |
1127 | goto out_free; | |
1128 | } | |
1129 | jiffies_left = timespec_to_jiffies(&_timeout); | |
1130 | } | |
1131 | max = 0; | |
1132 | for (sop = sops; sop < sops + nsops; sop++) { | |
1133 | if (sop->sem_num >= max) | |
1134 | max = sop->sem_num; | |
1135 | if (sop->sem_flg & SEM_UNDO) | |
b78755ab MS |
1136 | undos = 1; |
1137 | if (sop->sem_op != 0) | |
1da177e4 LT |
1138 | alter = 1; |
1139 | } | |
1da177e4 LT |
1140 | |
1141 | retry_undos: | |
1142 | if (undos) { | |
e3893534 | 1143 | un = find_undo(ns, semid); |
1da177e4 LT |
1144 | if (IS_ERR(un)) { |
1145 | error = PTR_ERR(un); | |
1146 | goto out_free; | |
1147 | } | |
1148 | } else | |
1149 | un = NULL; | |
1150 | ||
023a5355 ND |
1151 | sma = sem_lock_check(ns, semid); |
1152 | if (IS_ERR(sma)) { | |
1153 | error = PTR_ERR(sma); | |
1da177e4 | 1154 | goto out_free; |
023a5355 ND |
1155 | } |
1156 | ||
1da177e4 | 1157 | /* |
023a5355 | 1158 | * semid identifiers are not unique - find_undo may have |
1da177e4 LT |
1159 | * allocated an undo structure, it was invalidated by an RMID |
1160 | * and now a new array with received the same id. Check and retry. | |
1161 | */ | |
1162 | if (un && un->semid == -1) { | |
1163 | sem_unlock(sma); | |
1164 | goto retry_undos; | |
1165 | } | |
1166 | error = -EFBIG; | |
1167 | if (max >= sma->sem_nsems) | |
1168 | goto out_unlock_free; | |
1169 | ||
1170 | error = -EACCES; | |
1171 | if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO)) | |
1172 | goto out_unlock_free; | |
1173 | ||
1174 | error = security_sem_semop(sma, sops, nsops, alter); | |
1175 | if (error) | |
1176 | goto out_unlock_free; | |
1177 | ||
b488893a | 1178 | error = try_atomic_semop (sma, sops, nsops, un, task_tgid_vnr(current)); |
1da177e4 LT |
1179 | if (error <= 0) { |
1180 | if (alter && error == 0) | |
1181 | update_queue (sma); | |
1182 | goto out_unlock_free; | |
1183 | } | |
1184 | ||
1185 | /* We need to sleep on this operation, so we put the current | |
1186 | * task into the pending queue and go to sleep. | |
1187 | */ | |
1188 | ||
1189 | queue.sma = sma; | |
1190 | queue.sops = sops; | |
1191 | queue.nsops = nsops; | |
1192 | queue.undo = un; | |
b488893a | 1193 | queue.pid = task_tgid_vnr(current); |
1da177e4 LT |
1194 | queue.id = semid; |
1195 | queue.alter = alter; | |
1196 | if (alter) | |
1197 | append_to_queue(sma ,&queue); | |
1198 | else | |
1199 | prepend_to_queue(sma ,&queue); | |
1200 | ||
1201 | queue.status = -EINTR; | |
1202 | queue.sleeper = current; | |
1203 | current->state = TASK_INTERRUPTIBLE; | |
1204 | sem_unlock(sma); | |
1205 | ||
1206 | if (timeout) | |
1207 | jiffies_left = schedule_timeout(jiffies_left); | |
1208 | else | |
1209 | schedule(); | |
1210 | ||
1211 | error = queue.status; | |
1212 | while(unlikely(error == IN_WAKEUP)) { | |
1213 | cpu_relax(); | |
1214 | error = queue.status; | |
1215 | } | |
1216 | ||
1217 | if (error != -EINTR) { | |
1218 | /* fast path: update_queue already obtained all requested | |
1219 | * resources */ | |
1220 | goto out_free; | |
1221 | } | |
1222 | ||
e3893534 | 1223 | sma = sem_lock(ns, semid); |
023a5355 | 1224 | if (IS_ERR(sma)) { |
27315c96 | 1225 | BUG_ON(queue.prev != NULL); |
1da177e4 LT |
1226 | error = -EIDRM; |
1227 | goto out_free; | |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | * If queue.status != -EINTR we are woken up by another process | |
1232 | */ | |
1233 | error = queue.status; | |
1234 | if (error != -EINTR) { | |
1235 | goto out_unlock_free; | |
1236 | } | |
1237 | ||
1238 | /* | |
1239 | * If an interrupt occurred we have to clean up the queue | |
1240 | */ | |
1241 | if (timeout && jiffies_left == 0) | |
1242 | error = -EAGAIN; | |
1243 | remove_from_queue(sma,&queue); | |
1244 | goto out_unlock_free; | |
1245 | ||
1246 | out_unlock_free: | |
1247 | sem_unlock(sma); | |
1248 | out_free: | |
1249 | if(sops != fast_sops) | |
1250 | kfree(sops); | |
1251 | return error; | |
1252 | } | |
1253 | ||
1254 | asmlinkage long sys_semop (int semid, struct sembuf __user *tsops, unsigned nsops) | |
1255 | { | |
1256 | return sys_semtimedop(semid, tsops, nsops, NULL); | |
1257 | } | |
1258 | ||
1259 | /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between | |
1260 | * parent and child tasks. | |
1da177e4 LT |
1261 | */ |
1262 | ||
1263 | int copy_semundo(unsigned long clone_flags, struct task_struct *tsk) | |
1264 | { | |
1265 | struct sem_undo_list *undo_list; | |
1266 | int error; | |
1267 | ||
1268 | if (clone_flags & CLONE_SYSVSEM) { | |
1269 | error = get_undo_list(&undo_list); | |
1270 | if (error) | |
1271 | return error; | |
1da177e4 LT |
1272 | atomic_inc(&undo_list->refcnt); |
1273 | tsk->sysvsem.undo_list = undo_list; | |
1274 | } else | |
1275 | tsk->sysvsem.undo_list = NULL; | |
1276 | ||
1277 | return 0; | |
1278 | } | |
1279 | ||
1280 | /* | |
1281 | * add semadj values to semaphores, free undo structures. | |
1282 | * undo structures are not freed when semaphore arrays are destroyed | |
1283 | * so some of them may be out of date. | |
1284 | * IMPLEMENTATION NOTE: There is some confusion over whether the | |
1285 | * set of adjustments that needs to be done should be done in an atomic | |
1286 | * manner or not. That is, if we are attempting to decrement the semval | |
1287 | * should we queue up and wait until we can do so legally? | |
1288 | * The original implementation attempted to do this (queue and wait). | |
1289 | * The current implementation does not do so. The POSIX standard | |
1290 | * and SVID should be consulted to determine what behavior is mandated. | |
1291 | */ | |
1292 | void exit_sem(struct task_struct *tsk) | |
1293 | { | |
1294 | struct sem_undo_list *undo_list; | |
1295 | struct sem_undo *u, **up; | |
e3893534 | 1296 | struct ipc_namespace *ns; |
1da177e4 LT |
1297 | |
1298 | undo_list = tsk->sysvsem.undo_list; | |
1299 | if (!undo_list) | |
1300 | return; | |
1301 | ||
1302 | if (!atomic_dec_and_test(&undo_list->refcnt)) | |
1303 | return; | |
1304 | ||
e3893534 | 1305 | ns = tsk->nsproxy->ipc_ns; |
1da177e4 LT |
1306 | /* There's no need to hold the semundo list lock, as current |
1307 | * is the last task exiting for this undo list. | |
1308 | */ | |
1309 | for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) { | |
1310 | struct sem_array *sma; | |
1311 | int nsems, i; | |
1312 | struct sem_undo *un, **unp; | |
1313 | int semid; | |
1314 | ||
1315 | semid = u->semid; | |
1316 | ||
1317 | if(semid == -1) | |
1318 | continue; | |
e3893534 | 1319 | sma = sem_lock(ns, semid); |
023a5355 | 1320 | if (IS_ERR(sma)) |
1da177e4 LT |
1321 | continue; |
1322 | ||
1323 | if (u->semid == -1) | |
1324 | goto next_entry; | |
1325 | ||
1b531f21 | 1326 | BUG_ON(sem_checkid(sma, u->semid)); |
1da177e4 LT |
1327 | |
1328 | /* remove u from the sma->undo list */ | |
1329 | for (unp = &sma->undo; (un = *unp); unp = &un->id_next) { | |
1330 | if (u == un) | |
1331 | goto found; | |
1332 | } | |
1333 | printk ("exit_sem undo list error id=%d\n", u->semid); | |
1334 | goto next_entry; | |
1335 | found: | |
1336 | *unp = un->id_next; | |
1337 | /* perform adjustments registered in u */ | |
1338 | nsems = sma->sem_nsems; | |
1339 | for (i = 0; i < nsems; i++) { | |
5f921ae9 | 1340 | struct sem * semaphore = &sma->sem_base[i]; |
1da177e4 | 1341 | if (u->semadj[i]) { |
5f921ae9 | 1342 | semaphore->semval += u->semadj[i]; |
1da177e4 LT |
1343 | /* |
1344 | * Range checks of the new semaphore value, | |
1345 | * not defined by sus: | |
1346 | * - Some unices ignore the undo entirely | |
1347 | * (e.g. HP UX 11i 11.22, Tru64 V5.1) | |
1348 | * - some cap the value (e.g. FreeBSD caps | |
1349 | * at 0, but doesn't enforce SEMVMX) | |
1350 | * | |
1351 | * Linux caps the semaphore value, both at 0 | |
1352 | * and at SEMVMX. | |
1353 | * | |
1354 | * Manfred <manfred@colorfullife.com> | |
1355 | */ | |
5f921ae9 IM |
1356 | if (semaphore->semval < 0) |
1357 | semaphore->semval = 0; | |
1358 | if (semaphore->semval > SEMVMX) | |
1359 | semaphore->semval = SEMVMX; | |
b488893a | 1360 | semaphore->sempid = task_tgid_vnr(current); |
1da177e4 LT |
1361 | } |
1362 | } | |
1363 | sma->sem_otime = get_seconds(); | |
1364 | /* maybe some queued-up processes were waiting for this */ | |
1365 | update_queue(sma); | |
1366 | next_entry: | |
1367 | sem_unlock(sma); | |
1368 | } | |
1369 | kfree(undo_list); | |
1370 | } | |
1371 | ||
1372 | #ifdef CONFIG_PROC_FS | |
19b4946c | 1373 | static int sysvipc_sem_proc_show(struct seq_file *s, void *it) |
1da177e4 | 1374 | { |
19b4946c MW |
1375 | struct sem_array *sma = it; |
1376 | ||
1377 | return seq_printf(s, | |
1378 | "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n", | |
1379 | sma->sem_perm.key, | |
7ca7e564 | 1380 | sma->sem_perm.id, |
19b4946c MW |
1381 | sma->sem_perm.mode, |
1382 | sma->sem_nsems, | |
1383 | sma->sem_perm.uid, | |
1384 | sma->sem_perm.gid, | |
1385 | sma->sem_perm.cuid, | |
1386 | sma->sem_perm.cgid, | |
1387 | sma->sem_otime, | |
1388 | sma->sem_ctime); | |
1da177e4 LT |
1389 | } |
1390 | #endif |