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