r4k_genex.S isn't really R4000-specific so rename to genex.S.
[linux-2.6/linux-mips.git] / ipc / sem.c
blobd1a54864f753091cb23af52dcd118d14c319ecbb
1 /*
2 * linux/ipc/sem.c
3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
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.
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.
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.
56 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
58 * SMP-threaded, sysctl's added
59 * (c) 1999 Manfred Spraul <manfreds@colorfullife.com>
60 * Enforced range limit on SEM_UNDO
61 * (c) 2001 Red Hat Inc <alan@redhat.com>
64 #include <linux/config.h>
65 #include <linux/slab.h>
66 #include <linux/spinlock.h>
67 #include <linux/init.h>
68 #include <linux/proc_fs.h>
69 #include <linux/time.h>
70 #include <linux/smp_lock.h>
71 #include <linux/security.h>
72 #include <asm/uaccess.h>
73 #include "util.h"
76 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
77 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
78 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
79 #define sem_checkid(sma, semid) \
80 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
81 #define sem_buildid(id, seq) \
82 ipc_buildid(&sem_ids, id, seq)
83 static struct ipc_ids sem_ids;
85 static int newary (key_t, int, int);
86 static void freeary (struct sem_array *sma, int id);
87 #ifdef CONFIG_PROC_FS
88 static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data);
89 #endif
91 #define SEMMSL_FAST 256 /* 512 bytes on stack */
92 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
95 * linked list protection:
96 * sem_undo.id_next,
97 * sem_array.sem_pending{,last},
98 * sem_array.sem_undo: sem_lock() for read/write
99 * sem_undo.proc_next: only "current" is allowed to read/write that field.
103 int sem_ctls[4] = {SEMMSL, SEMMNS, SEMOPM, SEMMNI};
104 #define sc_semmsl (sem_ctls[0])
105 #define sc_semmns (sem_ctls[1])
106 #define sc_semopm (sem_ctls[2])
107 #define sc_semmni (sem_ctls[3])
109 static int used_sems;
111 void __init sem_init (void)
113 used_sems = 0;
114 ipc_init_ids(&sem_ids,sc_semmni);
116 #ifdef CONFIG_PROC_FS
117 create_proc_read_entry("sysvipc/sem", 0, 0, sysvipc_sem_read_proc, NULL);
118 #endif
121 static int newary (key_t key, int nsems, int semflg)
123 int id;
124 int retval;
125 struct sem_array *sma;
126 int size;
128 if (!nsems)
129 return -EINVAL;
130 if (used_sems + nsems > sc_semmns)
131 return -ENOSPC;
133 size = sizeof (*sma) + nsems * sizeof (struct sem);
134 sma = ipc_rcu_alloc(size);
135 if (!sma) {
136 return -ENOMEM;
138 memset (sma, 0, size);
140 sma->sem_perm.mode = (semflg & S_IRWXUGO);
141 sma->sem_perm.key = key;
143 sma->sem_perm.security = NULL;
144 retval = security_sem_alloc(sma);
145 if (retval) {
146 ipc_rcu_free(sma, size);
147 return retval;
150 id = ipc_addid(&sem_ids, &sma->sem_perm, sc_semmni);
151 if(id == -1) {
152 security_sem_free(sma);
153 ipc_rcu_free(sma, size);
154 return -ENOSPC;
156 used_sems += nsems;
158 sma->sem_base = (struct sem *) &sma[1];
159 /* sma->sem_pending = NULL; */
160 sma->sem_pending_last = &sma->sem_pending;
161 /* sma->undo = NULL; */
162 sma->sem_nsems = nsems;
163 sma->sem_ctime = get_seconds();
164 sem_unlock(sma);
166 return sem_buildid(id, sma->sem_perm.seq);
169 asmlinkage long sys_semget (key_t key, int nsems, int semflg)
171 int id, err = -EINVAL;
172 struct sem_array *sma;
174 if (nsems < 0 || nsems > sc_semmsl)
175 return -EINVAL;
176 down(&sem_ids.sem);
178 if (key == IPC_PRIVATE) {
179 err = newary(key, nsems, semflg);
180 } else if ((id = ipc_findkey(&sem_ids, key)) == -1) { /* key not used */
181 if (!(semflg & IPC_CREAT))
182 err = -ENOENT;
183 else
184 err = newary(key, nsems, semflg);
185 } else if (semflg & IPC_CREAT && semflg & IPC_EXCL) {
186 err = -EEXIST;
187 } else {
188 sma = sem_lock(id);
189 if(sma==NULL)
190 BUG();
191 if (nsems > sma->sem_nsems)
192 err = -EINVAL;
193 else if (ipcperms(&sma->sem_perm, semflg))
194 err = -EACCES;
195 else {
196 int semid = sem_buildid(id, sma->sem_perm.seq);
197 err = security_sem_associate(sma, semflg);
198 if (!err)
199 err = semid;
201 sem_unlock(sma);
204 up(&sem_ids.sem);
205 return err;
208 /* doesn't acquire the sem_lock on error! */
209 static int sem_revalidate(int semid, struct sem_array* sma, int nsems, short flg)
211 struct sem_array* smanew;
213 smanew = sem_lock(semid);
214 if(smanew==NULL)
215 return -EIDRM;
216 if(smanew != sma || sem_checkid(sma,semid) || sma->sem_nsems != nsems) {
217 sem_unlock(smanew);
218 return -EIDRM;
221 if (flg && ipcperms(&sma->sem_perm, flg)) {
222 sem_unlock(smanew);
223 return -EACCES;
225 return 0;
227 /* Manage the doubly linked list sma->sem_pending as a FIFO:
228 * insert new queue elements at the tail sma->sem_pending_last.
230 static inline void append_to_queue (struct sem_array * sma,
231 struct sem_queue * q)
233 *(q->prev = sma->sem_pending_last) = q;
234 *(sma->sem_pending_last = &q->next) = NULL;
237 static inline void prepend_to_queue (struct sem_array * sma,
238 struct sem_queue * q)
240 q->next = sma->sem_pending;
241 *(q->prev = &sma->sem_pending) = q;
242 if (q->next)
243 q->next->prev = &q->next;
244 else /* sma->sem_pending_last == &sma->sem_pending */
245 sma->sem_pending_last = &q->next;
248 static inline void remove_from_queue (struct sem_array * sma,
249 struct sem_queue * q)
251 *(q->prev) = q->next;
252 if (q->next)
253 q->next->prev = q->prev;
254 else /* sma->sem_pending_last == &q->next */
255 sma->sem_pending_last = q->prev;
256 q->prev = NULL; /* mark as removed */
260 * Determine whether a sequence of semaphore operations would succeed
261 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
264 static int try_atomic_semop (struct sem_array * sma, struct sembuf * sops,
265 int nsops, struct sem_undo *un, int pid)
267 int result, sem_op;
268 struct sembuf *sop;
269 struct sem * curr;
271 for (sop = sops; sop < sops + nsops; sop++) {
272 curr = sma->sem_base + sop->sem_num;
273 sem_op = sop->sem_op;
274 result = curr->semval;
276 if (!sem_op && result)
277 goto would_block;
279 result += sem_op;
280 if (result < 0)
281 goto would_block;
282 if (result > SEMVMX)
283 goto out_of_range;
284 if (sop->sem_flg & SEM_UNDO) {
285 int undo = un->semadj[sop->sem_num] - sem_op;
287 * Exceeding the undo range is an error.
289 if (undo < (-SEMAEM - 1) || undo > SEMAEM)
290 goto out_of_range;
292 curr->semval = result;
295 sop--;
296 while (sop >= sops) {
297 sma->sem_base[sop->sem_num].sempid = pid;
298 if (sop->sem_flg & SEM_UNDO)
299 un->semadj[sop->sem_num] -= sop->sem_op;
300 sop--;
303 sma->sem_otime = get_seconds();
304 return 0;
306 out_of_range:
307 result = -ERANGE;
308 goto undo;
310 would_block:
311 if (sop->sem_flg & IPC_NOWAIT)
312 result = -EAGAIN;
313 else
314 result = 1;
316 undo:
317 sop--;
318 while (sop >= sops) {
319 sma->sem_base[sop->sem_num].semval -= sop->sem_op;
320 sop--;
323 return result;
326 /* Go through the pending queue for the indicated semaphore
327 * looking for tasks that can be completed.
329 static void update_queue (struct sem_array * sma)
331 int error;
332 struct sem_queue * q;
334 for (q = sma->sem_pending; q; q = q->next) {
336 error = try_atomic_semop(sma, q->sops, q->nsops,
337 q->undo, q->pid);
339 /* Does q->sleeper still need to sleep? */
340 if (error <= 0) {
341 q->status = error;
342 remove_from_queue(sma,q);
343 wake_up_process(q->sleeper);
348 /* The following counts are associated to each semaphore:
349 * semncnt number of tasks waiting on semval being nonzero
350 * semzcnt number of tasks waiting on semval being zero
351 * This model assumes that a task waits on exactly one semaphore.
352 * Since semaphore operations are to be performed atomically, tasks actually
353 * wait on a whole sequence of semaphores simultaneously.
354 * The counts we return here are a rough approximation, but still
355 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
357 static int count_semncnt (struct sem_array * sma, ushort semnum)
359 int semncnt;
360 struct sem_queue * q;
362 semncnt = 0;
363 for (q = sma->sem_pending; q; q = q->next) {
364 struct sembuf * sops = q->sops;
365 int nsops = q->nsops;
366 int i;
367 for (i = 0; i < nsops; i++)
368 if (sops[i].sem_num == semnum
369 && (sops[i].sem_op < 0)
370 && !(sops[i].sem_flg & IPC_NOWAIT))
371 semncnt++;
373 return semncnt;
375 static int count_semzcnt (struct sem_array * sma, ushort semnum)
377 int semzcnt;
378 struct sem_queue * q;
380 semzcnt = 0;
381 for (q = sma->sem_pending; q; q = q->next) {
382 struct sembuf * sops = q->sops;
383 int nsops = q->nsops;
384 int i;
385 for (i = 0; i < nsops; i++)
386 if (sops[i].sem_num == semnum
387 && (sops[i].sem_op == 0)
388 && !(sops[i].sem_flg & IPC_NOWAIT))
389 semzcnt++;
391 return semzcnt;
394 /* Free a semaphore set. freeary() is called with sem_ids.sem down and
395 * the spinlock for this semaphore set hold. sem_ids.sem remains locked
396 * on exit.
398 static void freeary (struct sem_array *sma, int id)
400 struct sem_undo *un;
401 struct sem_queue *q;
402 int size;
404 /* Invalidate the existing undo structures for this semaphore set.
405 * (They will be freed without any further action in exit_sem()
406 * or during the next semop.)
408 for (un = sma->undo; un; un = un->id_next)
409 un->semid = -1;
411 /* Wake up all pending processes and let them fail with EIDRM. */
412 for (q = sma->sem_pending; q; q = q->next) {
413 q->status = -EIDRM;
414 q->prev = NULL;
415 wake_up_process(q->sleeper); /* doesn't sleep */
418 /* Remove the semaphore set from the ID array*/
419 sma = sem_rmid(id);
420 sem_unlock(sma);
422 used_sems -= sma->sem_nsems;
423 size = sizeof (*sma) + sma->sem_nsems * sizeof (struct sem);
424 security_sem_free(sma);
425 ipc_rcu_free(sma, size);
428 static unsigned long copy_semid_to_user(void *buf, struct semid64_ds *in, int version)
430 switch(version) {
431 case IPC_64:
432 return copy_to_user(buf, in, sizeof(*in));
433 case IPC_OLD:
435 struct semid_ds out;
437 ipc64_perm_to_ipc_perm(&in->sem_perm, &out.sem_perm);
439 out.sem_otime = in->sem_otime;
440 out.sem_ctime = in->sem_ctime;
441 out.sem_nsems = in->sem_nsems;
443 return copy_to_user(buf, &out, sizeof(out));
445 default:
446 return -EINVAL;
450 static int semctl_nolock(int semid, int semnum, int cmd, int version, union semun arg)
452 int err = -EINVAL;
453 struct sem_array *sma;
455 switch(cmd) {
456 case IPC_INFO:
457 case SEM_INFO:
459 struct seminfo seminfo;
460 int max_id;
462 err = security_sem_semctl(NULL, cmd);
463 if (err)
464 return err;
466 memset(&seminfo,0,sizeof(seminfo));
467 seminfo.semmni = sc_semmni;
468 seminfo.semmns = sc_semmns;
469 seminfo.semmsl = sc_semmsl;
470 seminfo.semopm = sc_semopm;
471 seminfo.semvmx = SEMVMX;
472 seminfo.semmnu = SEMMNU;
473 seminfo.semmap = SEMMAP;
474 seminfo.semume = SEMUME;
475 down(&sem_ids.sem);
476 if (cmd == SEM_INFO) {
477 seminfo.semusz = sem_ids.in_use;
478 seminfo.semaem = used_sems;
479 } else {
480 seminfo.semusz = SEMUSZ;
481 seminfo.semaem = SEMAEM;
483 max_id = sem_ids.max_id;
484 up(&sem_ids.sem);
485 if (copy_to_user (arg.__buf, &seminfo, sizeof(struct seminfo)))
486 return -EFAULT;
487 return (max_id < 0) ? 0: max_id;
489 case SEM_STAT:
491 struct semid64_ds tbuf;
492 int id;
494 if(semid >= sem_ids.size)
495 return -EINVAL;
497 memset(&tbuf,0,sizeof(tbuf));
499 sma = sem_lock(semid);
500 if(sma == NULL)
501 return -EINVAL;
503 err = -EACCES;
504 if (ipcperms (&sma->sem_perm, S_IRUGO))
505 goto out_unlock;
507 err = security_sem_semctl(sma, cmd);
508 if (err)
509 goto out_unlock;
511 id = sem_buildid(semid, sma->sem_perm.seq);
513 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
514 tbuf.sem_otime = sma->sem_otime;
515 tbuf.sem_ctime = sma->sem_ctime;
516 tbuf.sem_nsems = sma->sem_nsems;
517 sem_unlock(sma);
518 if (copy_semid_to_user (arg.buf, &tbuf, version))
519 return -EFAULT;
520 return id;
522 default:
523 return -EINVAL;
525 return err;
526 out_unlock:
527 sem_unlock(sma);
528 return err;
531 static int semctl_main(int semid, int semnum, int cmd, int version, union semun arg)
533 struct sem_array *sma;
534 struct sem* curr;
535 int err;
536 ushort fast_sem_io[SEMMSL_FAST];
537 ushort* sem_io = fast_sem_io;
538 int nsems;
540 sma = sem_lock(semid);
541 if(sma==NULL)
542 return -EINVAL;
544 nsems = sma->sem_nsems;
546 err=-EIDRM;
547 if (sem_checkid(sma,semid))
548 goto out_unlock;
550 err = -EACCES;
551 if (ipcperms (&sma->sem_perm, (cmd==SETVAL||cmd==SETALL)?S_IWUGO:S_IRUGO))
552 goto out_unlock;
554 err = security_sem_semctl(sma, cmd);
555 if (err)
556 goto out_unlock;
558 err = -EACCES;
559 switch (cmd) {
560 case GETALL:
562 ushort *array = arg.array;
563 int i;
565 if(nsems > SEMMSL_FAST) {
566 sem_unlock(sma);
567 sem_io = ipc_alloc(sizeof(ushort)*nsems);
568 if(sem_io == NULL)
569 return -ENOMEM;
570 err = sem_revalidate(semid, sma, nsems, S_IRUGO);
571 if(err)
572 goto out_free;
575 for (i = 0; i < sma->sem_nsems; i++)
576 sem_io[i] = sma->sem_base[i].semval;
577 sem_unlock(sma);
578 err = 0;
579 if(copy_to_user(array, sem_io, nsems*sizeof(ushort)))
580 err = -EFAULT;
581 goto out_free;
583 case SETALL:
585 int i;
586 struct sem_undo *un;
588 sem_unlock(sma);
590 if(nsems > SEMMSL_FAST) {
591 sem_io = ipc_alloc(sizeof(ushort)*nsems);
592 if(sem_io == NULL)
593 return -ENOMEM;
596 if (copy_from_user (sem_io, arg.array, nsems*sizeof(ushort))) {
597 err = -EFAULT;
598 goto out_free;
601 for (i = 0; i < nsems; i++) {
602 if (sem_io[i] > SEMVMX) {
603 err = -ERANGE;
604 goto out_free;
607 err = sem_revalidate(semid, sma, nsems, S_IWUGO);
608 if(err)
609 goto out_free;
611 for (i = 0; i < nsems; i++)
612 sma->sem_base[i].semval = sem_io[i];
613 for (un = sma->undo; un; un = un->id_next)
614 for (i = 0; i < nsems; i++)
615 un->semadj[i] = 0;
616 sma->sem_ctime = get_seconds();
617 /* maybe some queued-up processes were waiting for this */
618 update_queue(sma);
619 err = 0;
620 goto out_unlock;
622 case IPC_STAT:
624 struct semid64_ds tbuf;
625 memset(&tbuf,0,sizeof(tbuf));
626 kernel_to_ipc64_perm(&sma->sem_perm, &tbuf.sem_perm);
627 tbuf.sem_otime = sma->sem_otime;
628 tbuf.sem_ctime = sma->sem_ctime;
629 tbuf.sem_nsems = sma->sem_nsems;
630 sem_unlock(sma);
631 if (copy_semid_to_user (arg.buf, &tbuf, version))
632 return -EFAULT;
633 return 0;
635 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
637 err = -EINVAL;
638 if(semnum < 0 || semnum >= nsems)
639 goto out_unlock;
641 curr = &sma->sem_base[semnum];
643 switch (cmd) {
644 case GETVAL:
645 err = curr->semval;
646 goto out_unlock;
647 case GETPID:
648 err = curr->sempid;
649 goto out_unlock;
650 case GETNCNT:
651 err = count_semncnt(sma,semnum);
652 goto out_unlock;
653 case GETZCNT:
654 err = count_semzcnt(sma,semnum);
655 goto out_unlock;
656 case SETVAL:
658 int val = arg.val;
659 struct sem_undo *un;
660 err = -ERANGE;
661 if (val > SEMVMX || val < 0)
662 goto out_unlock;
664 for (un = sma->undo; un; un = un->id_next)
665 un->semadj[semnum] = 0;
666 curr->semval = val;
667 curr->sempid = current->pid;
668 sma->sem_ctime = get_seconds();
669 /* maybe some queued-up processes were waiting for this */
670 update_queue(sma);
671 err = 0;
672 goto out_unlock;
675 out_unlock:
676 sem_unlock(sma);
677 out_free:
678 if(sem_io != fast_sem_io)
679 ipc_free(sem_io, sizeof(ushort)*nsems);
680 return err;
683 struct sem_setbuf {
684 uid_t uid;
685 gid_t gid;
686 mode_t mode;
689 static inline unsigned long copy_semid_from_user(struct sem_setbuf *out, void *buf, int version)
691 switch(version) {
692 case IPC_64:
694 struct semid64_ds tbuf;
696 if(copy_from_user(&tbuf, buf, sizeof(tbuf)))
697 return -EFAULT;
699 out->uid = tbuf.sem_perm.uid;
700 out->gid = tbuf.sem_perm.gid;
701 out->mode = tbuf.sem_perm.mode;
703 return 0;
705 case IPC_OLD:
707 struct semid_ds tbuf_old;
709 if(copy_from_user(&tbuf_old, buf, sizeof(tbuf_old)))
710 return -EFAULT;
712 out->uid = tbuf_old.sem_perm.uid;
713 out->gid = tbuf_old.sem_perm.gid;
714 out->mode = tbuf_old.sem_perm.mode;
716 return 0;
718 default:
719 return -EINVAL;
723 static int semctl_down(int semid, int semnum, int cmd, int version, union semun arg)
725 struct sem_array *sma;
726 int err;
727 struct sem_setbuf setbuf;
728 struct kern_ipc_perm *ipcp;
730 if(cmd == IPC_SET) {
731 if(copy_semid_from_user (&setbuf, arg.buf, version))
732 return -EFAULT;
734 sma = sem_lock(semid);
735 if(sma==NULL)
736 return -EINVAL;
738 if (sem_checkid(sma,semid)) {
739 err=-EIDRM;
740 goto out_unlock;
742 ipcp = &sma->sem_perm;
744 if (current->euid != ipcp->cuid &&
745 current->euid != ipcp->uid && !capable(CAP_SYS_ADMIN)) {
746 err=-EPERM;
747 goto out_unlock;
750 err = security_sem_semctl(sma, cmd);
751 if (err)
752 goto out_unlock;
754 switch(cmd){
755 case IPC_RMID:
756 freeary(sma, semid);
757 err = 0;
758 break;
759 case IPC_SET:
760 ipcp->uid = setbuf.uid;
761 ipcp->gid = setbuf.gid;
762 ipcp->mode = (ipcp->mode & ~S_IRWXUGO)
763 | (setbuf.mode & S_IRWXUGO);
764 sma->sem_ctime = get_seconds();
765 sem_unlock(sma);
766 err = 0;
767 break;
768 default:
769 sem_unlock(sma);
770 err = -EINVAL;
771 break;
773 return err;
775 out_unlock:
776 sem_unlock(sma);
777 return err;
780 asmlinkage long sys_semctl (int semid, int semnum, int cmd, union semun arg)
782 int err = -EINVAL;
783 int version;
785 if (semid < 0)
786 return -EINVAL;
788 version = ipc_parse_version(&cmd);
790 switch(cmd) {
791 case IPC_INFO:
792 case SEM_INFO:
793 case SEM_STAT:
794 err = semctl_nolock(semid,semnum,cmd,version,arg);
795 return err;
796 case GETALL:
797 case GETVAL:
798 case GETPID:
799 case GETNCNT:
800 case GETZCNT:
801 case IPC_STAT:
802 case SETVAL:
803 case SETALL:
804 err = semctl_main(semid,semnum,cmd,version,arg);
805 return err;
806 case IPC_RMID:
807 case IPC_SET:
808 down(&sem_ids.sem);
809 err = semctl_down(semid,semnum,cmd,version,arg);
810 up(&sem_ids.sem);
811 return err;
812 default:
813 return -EINVAL;
817 static inline void lock_semundo(void)
819 struct sem_undo_list *undo_list;
821 undo_list = current->sysvsem.undo_list;
822 if ((undo_list != NULL) && (atomic_read(&undo_list->refcnt) != 1))
823 spin_lock(&undo_list->lock);
826 /* This code has an interaction with copy_semundo().
827 * Consider; two tasks are sharing the undo_list. task1
828 * acquires the undo_list lock in lock_semundo(). If task2 now
829 * exits before task1 releases the lock (by calling
830 * unlock_semundo()), then task1 will never call spin_unlock().
831 * This leave the sem_undo_list in a locked state. If task1 now creats task3
832 * and once again shares the sem_undo_list, the sem_undo_list will still be
833 * locked, and future SEM_UNDO operations will deadlock. This case is
834 * dealt with in copy_semundo() by having it reinitialize the spin lock when
835 * the refcnt goes from 1 to 2.
837 static inline void unlock_semundo(void)
839 struct sem_undo_list *undo_list;
841 undo_list = current->sysvsem.undo_list;
842 if ((undo_list != NULL) && (atomic_read(&undo_list->refcnt) != 1))
843 spin_unlock(&undo_list->lock);
847 /* If the task doesn't already have a undo_list, then allocate one
848 * here. We guarantee there is only one thread using this undo list,
849 * and current is THE ONE
851 * If this allocation and assignment succeeds, but later
852 * portions of this code fail, there is no need to free the sem_undo_list.
853 * Just let it stay associated with the task, and it'll be freed later
854 * at exit time.
856 * This can block, so callers must hold no locks.
858 static inline int get_undo_list(struct sem_undo_list **undo_listp)
860 struct sem_undo_list *undo_list;
861 int size;
863 undo_list = current->sysvsem.undo_list;
864 if (!undo_list) {
865 size = sizeof(struct sem_undo_list);
866 undo_list = (struct sem_undo_list *) kmalloc(size, GFP_KERNEL);
867 if (undo_list == NULL)
868 return -ENOMEM;
869 memset(undo_list, 0, size);
870 /* don't initialize unodhd->lock here. It's done
871 * in copy_semundo() instead.
873 atomic_set(&undo_list->refcnt, 1);
874 current->sysvsem.undo_list = undo_list;
876 *undo_listp = undo_list;
877 return 0;
880 static struct sem_undo *lookup_undo(struct sem_undo_list *ulp, int semid)
882 struct sem_undo **last, *un;
884 last = &ulp->proc_list;
885 un = *last;
886 while(un != NULL) {
887 if(un->semid==semid)
888 break;
889 if(un->semid==-1) {
890 *last=un->proc_next;
891 kfree(un);
892 } else {
893 last=&un->proc_next;
895 un=*last;
897 return un;
900 static struct sem_undo *find_undo(int semid)
902 struct sem_array *sma;
903 struct sem_undo_list *ulp;
904 struct sem_undo *un, *new;
905 int nsems;
906 int error;
908 error = get_undo_list(&ulp);
909 if (error)
910 return ERR_PTR(error);
912 lock_semundo();
913 un = lookup_undo(ulp, semid);
914 unlock_semundo();
915 if (likely(un!=NULL))
916 goto out;
918 /* no undo structure around - allocate one. */
919 sma = sem_lock(semid);
920 un = ERR_PTR(-EINVAL);
921 if(sma==NULL)
922 goto out;
923 un = ERR_PTR(-EIDRM);
924 if (sem_checkid(sma,semid))
925 goto out_unlock;
926 nsems = sma->sem_nsems;
927 sem_unlock(sma);
929 new = (struct sem_undo *) kmalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
930 if (!new)
931 return ERR_PTR(-ENOMEM);
932 memset(new, 0, sizeof(struct sem_undo) + sizeof(short)*nsems);
933 new->semadj = (short *) &new[1];
934 new->semid = semid;
936 lock_semundo();
937 un = lookup_undo(ulp, semid);
938 if (un) {
939 unlock_semundo();
940 kfree(new);
941 goto out;
943 error = sem_revalidate(semid, sma, nsems, 0);
944 if (error) {
945 sem_unlock(sma);
946 unlock_semundo();
947 kfree(new);
948 un = ERR_PTR(error);
949 goto out;
951 new->proc_next = ulp->proc_list;
952 ulp->proc_list = new;
953 new->id_next = sma->undo;
954 sma->undo = new;
955 sem_unlock(sma);
956 un = new;
957 out_unlock:
958 unlock_semundo();
959 out:
960 return un;
963 asmlinkage long sys_semop (int semid, struct sembuf *tsops, unsigned nsops)
965 return sys_semtimedop(semid, tsops, nsops, NULL);
968 asmlinkage long sys_semtimedop(int semid, struct sembuf *tsops,
969 unsigned nsops, const struct timespec *timeout)
971 int error = -EINVAL;
972 struct sem_array *sma;
973 struct sembuf fast_sops[SEMOPM_FAST];
974 struct sembuf* sops = fast_sops, *sop;
975 struct sem_undo *un;
976 int undos = 0, decrease = 0, alter = 0, max;
977 struct sem_queue queue;
978 unsigned long jiffies_left = 0;
980 if (nsops < 1 || semid < 0)
981 return -EINVAL;
982 if (nsops > sc_semopm)
983 return -E2BIG;
984 if(nsops > SEMOPM_FAST) {
985 sops = kmalloc(sizeof(*sops)*nsops,GFP_KERNEL);
986 if(sops==NULL)
987 return -ENOMEM;
989 if (copy_from_user (sops, tsops, nsops * sizeof(*tsops))) {
990 error=-EFAULT;
991 goto out_free;
993 if (timeout) {
994 struct timespec _timeout;
995 if (copy_from_user(&_timeout, timeout, sizeof(*timeout))) {
996 error = -EFAULT;
997 goto out_free;
999 if (_timeout.tv_sec < 0 || _timeout.tv_nsec < 0 ||
1000 _timeout.tv_nsec >= 1000000000L) {
1001 error = -EINVAL;
1002 goto out_free;
1004 jiffies_left = timespec_to_jiffies(&_timeout);
1006 max = 0;
1007 for (sop = sops; sop < sops + nsops; sop++) {
1008 if (sop->sem_num >= max)
1009 max = sop->sem_num;
1010 if (sop->sem_flg & SEM_UNDO)
1011 undos++;
1012 if (sop->sem_op < 0)
1013 decrease = 1;
1014 if (sop->sem_op > 0)
1015 alter = 1;
1017 alter |= decrease;
1019 retry_undos:
1020 if (undos) {
1021 un = find_undo(semid);
1022 if (IS_ERR(un)) {
1023 error = PTR_ERR(un);
1024 goto out_free;
1026 } else
1027 un = NULL;
1029 sma = sem_lock(semid);
1030 error=-EINVAL;
1031 if(sma==NULL)
1032 goto out_free;
1033 error = -EIDRM;
1034 if (sem_checkid(sma,semid))
1035 goto out_unlock_free;
1037 * semid identifies are not unique - find_undo may have
1038 * allocated an undo structure, it was invalidated by an RMID
1039 * and now a new array with received the same id. Check and retry.
1041 if (un && un->semid == -1)
1042 goto retry_undos;
1043 error = -EFBIG;
1044 if (max >= sma->sem_nsems)
1045 goto out_unlock_free;
1047 error = -EACCES;
1048 if (ipcperms(&sma->sem_perm, alter ? S_IWUGO : S_IRUGO))
1049 goto out_unlock_free;
1051 error = security_sem_semop(sma, sops, nsops, alter);
1052 if (error)
1053 goto out_unlock_free;
1055 error = try_atomic_semop (sma, sops, nsops, un, current->pid);
1056 if (error <= 0)
1057 goto update;
1059 /* We need to sleep on this operation, so we put the current
1060 * task into the pending queue and go to sleep.
1063 queue.sma = sma;
1064 queue.sops = sops;
1065 queue.nsops = nsops;
1066 queue.undo = un;
1067 queue.pid = current->pid;
1068 queue.id = semid;
1069 if (alter)
1070 append_to_queue(sma ,&queue);
1071 else
1072 prepend_to_queue(sma ,&queue);
1074 queue.status = -EINTR;
1075 queue.sleeper = current;
1076 current->state = TASK_INTERRUPTIBLE;
1077 sem_unlock(sma);
1079 if (timeout)
1080 jiffies_left = schedule_timeout(jiffies_left);
1081 else
1082 schedule();
1084 sma = sem_lock(semid);
1085 if(sma==NULL) {
1086 if(queue.prev != NULL)
1087 BUG();
1088 error = -EIDRM;
1089 goto out_free;
1093 * If queue.status != -EINTR we are woken up by another process
1095 error = queue.status;
1096 if (queue.status != -EINTR) {
1097 goto out_unlock_free;
1101 * If an interrupt occurred we have to clean up the queue
1103 if (timeout && jiffies_left == 0)
1104 error = -EAGAIN;
1105 remove_from_queue(sma,&queue);
1106 goto out_unlock_free;
1108 update:
1109 if (alter)
1110 update_queue (sma);
1111 out_unlock_free:
1112 sem_unlock(sma);
1113 out_free:
1114 if(sops != fast_sops)
1115 kfree(sops);
1116 return error;
1119 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1120 * parent and child tasks.
1122 * See the notes above unlock_semundo() regarding the spin_lock_init()
1123 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1124 * because of the reasoning in the comment above unlock_semundo.
1127 int copy_semundo(unsigned long clone_flags, struct task_struct *tsk)
1129 struct sem_undo_list *undo_list;
1130 int error;
1132 if (clone_flags & CLONE_SYSVSEM) {
1133 error = get_undo_list(&undo_list);
1134 if (error)
1135 return error;
1136 if (atomic_read(&undo_list->refcnt) == 1)
1137 spin_lock_init(&undo_list->lock);
1138 atomic_inc(&undo_list->refcnt);
1139 tsk->sysvsem.undo_list = undo_list;
1140 } else
1141 tsk->sysvsem.undo_list = NULL;
1143 return 0;
1147 * add semadj values to semaphores, free undo structures.
1148 * undo structures are not freed when semaphore arrays are destroyed
1149 * so some of them may be out of date.
1150 * IMPLEMENTATION NOTE: There is some confusion over whether the
1151 * set of adjustments that needs to be done should be done in an atomic
1152 * manner or not. That is, if we are attempting to decrement the semval
1153 * should we queue up and wait until we can do so legally?
1154 * The original implementation attempted to do this (queue and wait).
1155 * The current implementation does not do so. The POSIX standard
1156 * and SVID should be consulted to determine what behavior is mandated.
1158 void exit_sem(struct task_struct *tsk)
1160 struct sem_undo_list *undo_list;
1161 struct sem_undo *u, **up;
1163 undo_list = tsk->sysvsem.undo_list;
1164 if (!undo_list)
1165 return;
1167 if (!atomic_dec_and_test(&undo_list->refcnt))
1168 return;
1170 /* There's no need to hold the semundo list lock, as current
1171 * is the last task exiting for this undo list.
1173 for (up = &undo_list->proc_list; (u = *up); *up = u->proc_next, kfree(u)) {
1174 struct sem_array *sma;
1175 int nsems, i;
1176 struct sem_undo *un, **unp;
1177 int semid;
1179 semid = u->semid;
1181 if(semid == -1)
1182 continue;
1183 sma = sem_lock(semid);
1184 if (sma == NULL)
1185 continue;
1187 if (u->semid == -1)
1188 goto next_entry;
1190 BUG_ON(sem_checkid(sma,u->semid));
1192 /* remove u from the sma->undo list */
1193 for (unp = &sma->undo; (un = *unp); unp = &un->id_next) {
1194 if (u == un)
1195 goto found;
1197 printk ("exit_sem undo list error id=%d\n", u->semid);
1198 goto next_entry;
1199 found:
1200 *unp = un->id_next;
1201 /* perform adjustments registered in u */
1202 nsems = sma->sem_nsems;
1203 for (i = 0; i < nsems; i++) {
1204 struct sem * sem = &sma->sem_base[i];
1205 if (u->semadj[i]) {
1206 sem->semval += u->semadj[i];
1207 if (sem->semval < 0)
1208 sem->semval = 0; /* shouldn't happen */
1209 sem->sempid = current->pid;
1212 sma->sem_otime = get_seconds();
1213 /* maybe some queued-up processes were waiting for this */
1214 update_queue(sma);
1215 next_entry:
1216 sem_unlock(sma);
1218 kfree(undo_list);
1221 #ifdef CONFIG_PROC_FS
1222 static int sysvipc_sem_read_proc(char *buffer, char **start, off_t offset, int length, int *eof, void *data)
1224 off_t pos = 0;
1225 off_t begin = 0;
1226 int i, len = 0;
1228 len += sprintf(buffer, " key semid perms nsems uid gid cuid cgid otime ctime\n");
1229 down(&sem_ids.sem);
1231 for(i = 0; i <= sem_ids.max_id; i++) {
1232 struct sem_array *sma;
1233 sma = sem_lock(i);
1234 if(sma) {
1235 len += sprintf(buffer + len, "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
1236 sma->sem_perm.key,
1237 sem_buildid(i,sma->sem_perm.seq),
1238 sma->sem_perm.mode,
1239 sma->sem_nsems,
1240 sma->sem_perm.uid,
1241 sma->sem_perm.gid,
1242 sma->sem_perm.cuid,
1243 sma->sem_perm.cgid,
1244 sma->sem_otime,
1245 sma->sem_ctime);
1246 sem_unlock(sma);
1248 pos += len;
1249 if(pos < offset) {
1250 len = 0;
1251 begin = pos;
1253 if(pos > offset + length)
1254 goto done;
1257 *eof = 1;
1258 done:
1259 up(&sem_ids.sem);
1260 *start = buffer + (offset - begin);
1261 len -= (offset - begin);
1262 if(len > length)
1263 len = length;
1264 if(len < 0)
1265 len = 0;
1266 return len;
1268 #endif