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