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