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>
63 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
66 #include <linux/config.h>
67 #include <linux/slab.h>
68 #include <linux/spinlock.h>
69 #include <linux/init.h>
70 #include <linux/proc_fs.h>
71 #include <linux/time.h>
72 #include <linux/smp_lock.h>
73 #include <linux/security.h>
74 #include <linux/syscalls.h>
75 #include <linux/audit.h>
76 #include <linux/capability.h>
77 #include <linux/seq_file.h>
78 #include <asm/uaccess.h>
82 #define sem_lock(id) ((struct sem_array*)ipc_lock(&sem_ids,id))
83 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
84 #define sem_rmid(id) ((struct sem_array*)ipc_rmid(&sem_ids,id))
85 #define sem_checkid(sma, semid) \
86 ipc_checkid(&sem_ids,&sma->sem_perm,semid)
87 #define sem_buildid(id, seq) \
88 ipc_buildid(&sem_ids, id, seq)
89 static struct ipc_ids sem_ids
;
91 static int newary (key_t
, int, int);
92 static void freeary (struct sem_array
*sma
, int id
);
94 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
);
97 #define SEMMSL_FAST 256 /* 512 bytes on stack */
98 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
101 * linked list protection:
103 * sem_array.sem_pending{,last},
104 * sem_array.sem_undo: sem_lock() for read/write
105 * sem_undo.proc_next: only "current" is allowed to read/write that field.
109 int sem_ctls
[4] = {SEMMSL
, SEMMNS
, SEMOPM
, SEMMNI
};
110 #define sc_semmsl (sem_ctls[0])
111 #define sc_semmns (sem_ctls[1])
112 #define sc_semopm (sem_ctls[2])
113 #define sc_semmni (sem_ctls[3])
115 static int used_sems
;
117 void __init
sem_init (void)
120 ipc_init_ids(&sem_ids
,sc_semmni
);
121 ipc_init_proc_interface("sysvipc/sem",
122 " key semid perms nsems uid gid cuid cgid otime ctime\n",
124 sysvipc_sem_proc_show
);
128 * Lockless wakeup algorithm:
129 * Without the check/retry algorithm a lockless wakeup is possible:
130 * - queue.status is initialized to -EINTR before blocking.
131 * - wakeup is performed by
132 * * unlinking the queue entry from sma->sem_pending
133 * * setting queue.status to IN_WAKEUP
134 * This is the notification for the blocked thread that a
135 * result value is imminent.
136 * * call wake_up_process
137 * * set queue.status to the final value.
138 * - the previously blocked thread checks queue.status:
139 * * if it's IN_WAKEUP, then it must wait until the value changes
140 * * if it's not -EINTR, then the operation was completed by
141 * update_queue. semtimedop can return queue.status without
142 * performing any operation on the semaphore array.
143 * * otherwise it must acquire the spinlock and check what's up.
145 * The two-stage algorithm is necessary to protect against the following
147 * - if queue.status is set after wake_up_process, then the woken up idle
148 * thread could race forward and try (and fail) to acquire sma->lock
149 * before update_queue had a chance to set queue.status
150 * - if queue.status is written before wake_up_process and if the
151 * blocked process is woken up by a signal between writing
152 * queue.status and the wake_up_process, then the woken up
153 * process could return from semtimedop and die by calling
154 * sys_exit before wake_up_process is called. Then wake_up_process
155 * will oops, because the task structure is already invalid.
156 * (yes, this happened on s390 with sysv msg).
161 static int newary (key_t key
, int nsems
, int semflg
)
165 struct sem_array
*sma
;
170 if (used_sems
+ nsems
> sc_semmns
)
173 size
= sizeof (*sma
) + nsems
* sizeof (struct sem
);
174 sma
= ipc_rcu_alloc(size
);
178 memset (sma
, 0, size
);
180 sma
->sem_perm
.mode
= (semflg
& S_IRWXUGO
);
181 sma
->sem_perm
.key
= key
;
183 sma
->sem_perm
.security
= NULL
;
184 retval
= security_sem_alloc(sma
);
190 id
= ipc_addid(&sem_ids
, &sma
->sem_perm
, sc_semmni
);
192 security_sem_free(sma
);
198 sma
->sem_id
= sem_buildid(id
, sma
->sem_perm
.seq
);
199 sma
->sem_base
= (struct sem
*) &sma
[1];
200 /* sma->sem_pending = NULL; */
201 sma
->sem_pending_last
= &sma
->sem_pending
;
202 /* sma->undo = NULL; */
203 sma
->sem_nsems
= nsems
;
204 sma
->sem_ctime
= get_seconds();
210 asmlinkage
long sys_semget (key_t key
, int nsems
, int semflg
)
212 int id
, err
= -EINVAL
;
213 struct sem_array
*sma
;
215 if (nsems
< 0 || nsems
> sc_semmsl
)
219 if (key
== IPC_PRIVATE
) {
220 err
= newary(key
, nsems
, semflg
);
221 } else if ((id
= ipc_findkey(&sem_ids
, key
)) == -1) { /* key not used */
222 if (!(semflg
& IPC_CREAT
))
225 err
= newary(key
, nsems
, semflg
);
226 } else if (semflg
& IPC_CREAT
&& semflg
& IPC_EXCL
) {
232 if (nsems
> sma
->sem_nsems
)
234 else if (ipcperms(&sma
->sem_perm
, semflg
))
237 int semid
= sem_buildid(id
, sma
->sem_perm
.seq
);
238 err
= security_sem_associate(sma
, semflg
);
249 /* Manage the doubly linked list sma->sem_pending as a FIFO:
250 * insert new queue elements at the tail sma->sem_pending_last.
252 static inline void append_to_queue (struct sem_array
* sma
,
253 struct sem_queue
* q
)
255 *(q
->prev
= sma
->sem_pending_last
) = q
;
256 *(sma
->sem_pending_last
= &q
->next
) = NULL
;
259 static inline void prepend_to_queue (struct sem_array
* sma
,
260 struct sem_queue
* q
)
262 q
->next
= sma
->sem_pending
;
263 *(q
->prev
= &sma
->sem_pending
) = q
;
265 q
->next
->prev
= &q
->next
;
266 else /* sma->sem_pending_last == &sma->sem_pending */
267 sma
->sem_pending_last
= &q
->next
;
270 static inline void remove_from_queue (struct sem_array
* sma
,
271 struct sem_queue
* q
)
273 *(q
->prev
) = q
->next
;
275 q
->next
->prev
= q
->prev
;
276 else /* sma->sem_pending_last == &q->next */
277 sma
->sem_pending_last
= q
->prev
;
278 q
->prev
= NULL
; /* mark as removed */
282 * Determine whether a sequence of semaphore operations would succeed
283 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
286 static int try_atomic_semop (struct sem_array
* sma
, struct sembuf
* sops
,
287 int nsops
, struct sem_undo
*un
, int pid
)
293 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
294 curr
= sma
->sem_base
+ sop
->sem_num
;
295 sem_op
= sop
->sem_op
;
296 result
= curr
->semval
;
298 if (!sem_op
&& result
)
306 if (sop
->sem_flg
& SEM_UNDO
) {
307 int undo
= un
->semadj
[sop
->sem_num
] - sem_op
;
309 * Exceeding the undo range is an error.
311 if (undo
< (-SEMAEM
- 1) || undo
> SEMAEM
)
314 curr
->semval
= result
;
318 while (sop
>= sops
) {
319 sma
->sem_base
[sop
->sem_num
].sempid
= pid
;
320 if (sop
->sem_flg
& SEM_UNDO
)
321 un
->semadj
[sop
->sem_num
] -= sop
->sem_op
;
325 sma
->sem_otime
= get_seconds();
333 if (sop
->sem_flg
& IPC_NOWAIT
)
340 while (sop
>= sops
) {
341 sma
->sem_base
[sop
->sem_num
].semval
-= sop
->sem_op
;
348 /* Go through the pending queue for the indicated semaphore
349 * looking for tasks that can be completed.
351 static void update_queue (struct sem_array
* sma
)
354 struct sem_queue
* q
;
356 q
= sma
->sem_pending
;
358 error
= try_atomic_semop(sma
, q
->sops
, q
->nsops
,
361 /* Does q->sleeper still need to sleep? */
364 remove_from_queue(sma
,q
);
365 q
->status
= IN_WAKEUP
;
367 * Continue scanning. The next operation
368 * that must be checked depends on the type of the
369 * completed operation:
370 * - if the operation modified the array, then
371 * restart from the head of the queue and
372 * check for threads that might be waiting
373 * for semaphore values to become 0.
374 * - if the operation didn't modify the array,
375 * then just continue.
378 n
= sma
->sem_pending
;
381 wake_up_process(q
->sleeper
);
382 /* hands-off: q will disappear immediately after
394 /* The following counts are associated to each semaphore:
395 * semncnt number of tasks waiting on semval being nonzero
396 * semzcnt number of tasks waiting on semval being zero
397 * This model assumes that a task waits on exactly one semaphore.
398 * Since semaphore operations are to be performed atomically, tasks actually
399 * wait on a whole sequence of semaphores simultaneously.
400 * The counts we return here are a rough approximation, but still
401 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
403 static int count_semncnt (struct sem_array
* sma
, ushort semnum
)
406 struct sem_queue
* q
;
409 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
410 struct sembuf
* sops
= q
->sops
;
411 int nsops
= q
->nsops
;
413 for (i
= 0; i
< nsops
; i
++)
414 if (sops
[i
].sem_num
== semnum
415 && (sops
[i
].sem_op
< 0)
416 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
421 static int count_semzcnt (struct sem_array
* sma
, ushort semnum
)
424 struct sem_queue
* q
;
427 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
428 struct sembuf
* sops
= q
->sops
;
429 int nsops
= q
->nsops
;
431 for (i
= 0; i
< nsops
; i
++)
432 if (sops
[i
].sem_num
== semnum
433 && (sops
[i
].sem_op
== 0)
434 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
440 /* Free a semaphore set. freeary() is called with sem_ids.sem down and
441 * the spinlock for this semaphore set hold. sem_ids.sem remains locked
444 static void freeary (struct sem_array
*sma
, int id
)
450 /* Invalidate the existing undo structures for this semaphore set.
451 * (They will be freed without any further action in exit_sem()
452 * or during the next semop.)
454 for (un
= sma
->undo
; un
; un
= un
->id_next
)
457 /* Wake up all pending processes and let them fail with EIDRM. */
458 q
= sma
->sem_pending
;
461 /* lazy remove_from_queue: we are killing the whole queue */
464 q
->status
= IN_WAKEUP
;
465 wake_up_process(q
->sleeper
); /* doesn't sleep */
467 q
->status
= -EIDRM
; /* hands-off q */
471 /* Remove the semaphore set from the ID array*/
475 used_sems
-= sma
->sem_nsems
;
476 size
= sizeof (*sma
) + sma
->sem_nsems
* sizeof (struct sem
);
477 security_sem_free(sma
);
481 static unsigned long copy_semid_to_user(void __user
*buf
, struct semid64_ds
*in
, int version
)
485 return copy_to_user(buf
, in
, sizeof(*in
));
490 ipc64_perm_to_ipc_perm(&in
->sem_perm
, &out
.sem_perm
);
492 out
.sem_otime
= in
->sem_otime
;
493 out
.sem_ctime
= in
->sem_ctime
;
494 out
.sem_nsems
= in
->sem_nsems
;
496 return copy_to_user(buf
, &out
, sizeof(out
));
503 static int semctl_nolock(int semid
, int semnum
, int cmd
, int version
, union semun arg
)
506 struct sem_array
*sma
;
512 struct seminfo seminfo
;
515 err
= security_sem_semctl(NULL
, cmd
);
519 memset(&seminfo
,0,sizeof(seminfo
));
520 seminfo
.semmni
= sc_semmni
;
521 seminfo
.semmns
= sc_semmns
;
522 seminfo
.semmsl
= sc_semmsl
;
523 seminfo
.semopm
= sc_semopm
;
524 seminfo
.semvmx
= SEMVMX
;
525 seminfo
.semmnu
= SEMMNU
;
526 seminfo
.semmap
= SEMMAP
;
527 seminfo
.semume
= SEMUME
;
529 if (cmd
== SEM_INFO
) {
530 seminfo
.semusz
= sem_ids
.in_use
;
531 seminfo
.semaem
= used_sems
;
533 seminfo
.semusz
= SEMUSZ
;
534 seminfo
.semaem
= SEMAEM
;
536 max_id
= sem_ids
.max_id
;
538 if (copy_to_user (arg
.__buf
, &seminfo
, sizeof(struct seminfo
)))
540 return (max_id
< 0) ? 0: max_id
;
544 struct semid64_ds tbuf
;
547 if(semid
>= sem_ids
.entries
->size
)
550 memset(&tbuf
,0,sizeof(tbuf
));
552 sma
= sem_lock(semid
);
557 if (ipcperms (&sma
->sem_perm
, S_IRUGO
))
560 err
= security_sem_semctl(sma
, cmd
);
564 id
= sem_buildid(semid
, sma
->sem_perm
.seq
);
566 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
567 tbuf
.sem_otime
= sma
->sem_otime
;
568 tbuf
.sem_ctime
= sma
->sem_ctime
;
569 tbuf
.sem_nsems
= sma
->sem_nsems
;
571 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
584 static int semctl_main(int semid
, int semnum
, int cmd
, int version
, union semun arg
)
586 struct sem_array
*sma
;
589 ushort fast_sem_io
[SEMMSL_FAST
];
590 ushort
* sem_io
= fast_sem_io
;
593 sma
= sem_lock(semid
);
597 nsems
= sma
->sem_nsems
;
600 if (sem_checkid(sma
,semid
))
604 if (ipcperms (&sma
->sem_perm
, (cmd
==SETVAL
||cmd
==SETALL
)?S_IWUGO
:S_IRUGO
))
607 err
= security_sem_semctl(sma
, cmd
);
615 ushort __user
*array
= arg
.array
;
618 if(nsems
> SEMMSL_FAST
) {
622 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
624 ipc_lock_by_ptr(&sma
->sem_perm
);
630 ipc_lock_by_ptr(&sma
->sem_perm
);
632 if (sma
->sem_perm
.deleted
) {
639 for (i
= 0; i
< sma
->sem_nsems
; i
++)
640 sem_io
[i
] = sma
->sem_base
[i
].semval
;
643 if(copy_to_user(array
, sem_io
, nsems
*sizeof(ushort
)))
655 if(nsems
> SEMMSL_FAST
) {
656 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
658 ipc_lock_by_ptr(&sma
->sem_perm
);
665 if (copy_from_user (sem_io
, arg
.array
, nsems
*sizeof(ushort
))) {
666 ipc_lock_by_ptr(&sma
->sem_perm
);
673 for (i
= 0; i
< nsems
; i
++) {
674 if (sem_io
[i
] > SEMVMX
) {
675 ipc_lock_by_ptr(&sma
->sem_perm
);
682 ipc_lock_by_ptr(&sma
->sem_perm
);
684 if (sma
->sem_perm
.deleted
) {
690 for (i
= 0; i
< nsems
; i
++)
691 sma
->sem_base
[i
].semval
= sem_io
[i
];
692 for (un
= sma
->undo
; un
; un
= un
->id_next
)
693 for (i
= 0; i
< nsems
; i
++)
695 sma
->sem_ctime
= get_seconds();
696 /* maybe some queued-up processes were waiting for this */
703 struct semid64_ds tbuf
;
704 memset(&tbuf
,0,sizeof(tbuf
));
705 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
706 tbuf
.sem_otime
= sma
->sem_otime
;
707 tbuf
.sem_ctime
= sma
->sem_ctime
;
708 tbuf
.sem_nsems
= sma
->sem_nsems
;
710 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
714 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
717 if(semnum
< 0 || semnum
>= nsems
)
720 curr
= &sma
->sem_base
[semnum
];
730 err
= count_semncnt(sma
,semnum
);
733 err
= count_semzcnt(sma
,semnum
);
740 if (val
> SEMVMX
|| val
< 0)
743 for (un
= sma
->undo
; un
; un
= un
->id_next
)
744 un
->semadj
[semnum
] = 0;
746 curr
->sempid
= current
->tgid
;
747 sma
->sem_ctime
= get_seconds();
748 /* maybe some queued-up processes were waiting for this */
757 if(sem_io
!= fast_sem_io
)
758 ipc_free(sem_io
, sizeof(ushort
)*nsems
);
768 static inline unsigned long copy_semid_from_user(struct sem_setbuf
*out
, void __user
*buf
, int version
)
773 struct semid64_ds tbuf
;
775 if(copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
778 out
->uid
= tbuf
.sem_perm
.uid
;
779 out
->gid
= tbuf
.sem_perm
.gid
;
780 out
->mode
= tbuf
.sem_perm
.mode
;
786 struct semid_ds tbuf_old
;
788 if(copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
791 out
->uid
= tbuf_old
.sem_perm
.uid
;
792 out
->gid
= tbuf_old
.sem_perm
.gid
;
793 out
->mode
= tbuf_old
.sem_perm
.mode
;
802 static int semctl_down(int semid
, int semnum
, int cmd
, int version
, union semun arg
)
804 struct sem_array
*sma
;
806 struct sem_setbuf setbuf
;
807 struct kern_ipc_perm
*ipcp
;
810 if(copy_semid_from_user (&setbuf
, arg
.buf
, version
))
812 if ((err
= audit_ipc_perms(0, setbuf
.uid
, setbuf
.gid
, setbuf
.mode
)))
815 sma
= sem_lock(semid
);
819 if (sem_checkid(sma
,semid
)) {
823 ipcp
= &sma
->sem_perm
;
825 if (current
->euid
!= ipcp
->cuid
&&
826 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
)) {
831 err
= security_sem_semctl(sma
, cmd
);
841 ipcp
->uid
= setbuf
.uid
;
842 ipcp
->gid
= setbuf
.gid
;
843 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
)
844 | (setbuf
.mode
& S_IRWXUGO
);
845 sma
->sem_ctime
= get_seconds();
861 asmlinkage
long sys_semctl (int semid
, int semnum
, int cmd
, union semun arg
)
869 version
= ipc_parse_version(&cmd
);
875 err
= semctl_nolock(semid
,semnum
,cmd
,version
,arg
);
885 err
= semctl_main(semid
,semnum
,cmd
,version
,arg
);
890 err
= semctl_down(semid
,semnum
,cmd
,version
,arg
);
898 static inline void lock_semundo(void)
900 struct sem_undo_list
*undo_list
;
902 undo_list
= current
->sysvsem
.undo_list
;
904 spin_lock(&undo_list
->lock
);
907 /* This code has an interaction with copy_semundo().
908 * Consider; two tasks are sharing the undo_list. task1
909 * acquires the undo_list lock in lock_semundo(). If task2 now
910 * exits before task1 releases the lock (by calling
911 * unlock_semundo()), then task1 will never call spin_unlock().
912 * This leave the sem_undo_list in a locked state. If task1 now creats task3
913 * and once again shares the sem_undo_list, the sem_undo_list will still be
914 * locked, and future SEM_UNDO operations will deadlock. This case is
915 * dealt with in copy_semundo() by having it reinitialize the spin lock when
916 * the refcnt goes from 1 to 2.
918 static inline void unlock_semundo(void)
920 struct sem_undo_list
*undo_list
;
922 undo_list
= current
->sysvsem
.undo_list
;
924 spin_unlock(&undo_list
->lock
);
928 /* If the task doesn't already have a undo_list, then allocate one
929 * here. We guarantee there is only one thread using this undo list,
930 * and current is THE ONE
932 * If this allocation and assignment succeeds, but later
933 * portions of this code fail, there is no need to free the sem_undo_list.
934 * Just let it stay associated with the task, and it'll be freed later
937 * This can block, so callers must hold no locks.
939 static inline int get_undo_list(struct sem_undo_list
**undo_listp
)
941 struct sem_undo_list
*undo_list
;
944 undo_list
= current
->sysvsem
.undo_list
;
946 size
= sizeof(struct sem_undo_list
);
947 undo_list
= (struct sem_undo_list
*) kmalloc(size
, GFP_KERNEL
);
948 if (undo_list
== NULL
)
950 memset(undo_list
, 0, size
);
951 spin_lock_init(&undo_list
->lock
);
952 atomic_set(&undo_list
->refcnt
, 1);
953 current
->sysvsem
.undo_list
= undo_list
;
955 *undo_listp
= undo_list
;
959 static struct sem_undo
*lookup_undo(struct sem_undo_list
*ulp
, int semid
)
961 struct sem_undo
**last
, *un
;
963 last
= &ulp
->proc_list
;
979 static struct sem_undo
*find_undo(int semid
)
981 struct sem_array
*sma
;
982 struct sem_undo_list
*ulp
;
983 struct sem_undo
*un
, *new;
987 error
= get_undo_list(&ulp
);
989 return ERR_PTR(error
);
992 un
= lookup_undo(ulp
, semid
);
994 if (likely(un
!=NULL
))
997 /* no undo structure around - allocate one. */
998 sma
= sem_lock(semid
);
999 un
= ERR_PTR(-EINVAL
);
1002 un
= ERR_PTR(-EIDRM
);
1003 if (sem_checkid(sma
,semid
)) {
1007 nsems
= sma
->sem_nsems
;
1008 ipc_rcu_getref(sma
);
1011 new = (struct sem_undo
*) kmalloc(sizeof(struct sem_undo
) + sizeof(short)*nsems
, GFP_KERNEL
);
1013 ipc_lock_by_ptr(&sma
->sem_perm
);
1014 ipc_rcu_putref(sma
);
1016 return ERR_PTR(-ENOMEM
);
1018 memset(new, 0, sizeof(struct sem_undo
) + sizeof(short)*nsems
);
1019 new->semadj
= (short *) &new[1];
1023 un
= lookup_undo(ulp
, semid
);
1027 ipc_lock_by_ptr(&sma
->sem_perm
);
1028 ipc_rcu_putref(sma
);
1032 ipc_lock_by_ptr(&sma
->sem_perm
);
1033 ipc_rcu_putref(sma
);
1034 if (sma
->sem_perm
.deleted
) {
1038 un
= ERR_PTR(-EIDRM
);
1041 new->proc_next
= ulp
->proc_list
;
1042 ulp
->proc_list
= new;
1043 new->id_next
= sma
->undo
;
1052 asmlinkage
long sys_semtimedop(int semid
, struct sembuf __user
*tsops
,
1053 unsigned nsops
, const struct timespec __user
*timeout
)
1055 int error
= -EINVAL
;
1056 struct sem_array
*sma
;
1057 struct sembuf fast_sops
[SEMOPM_FAST
];
1058 struct sembuf
* sops
= fast_sops
, *sop
;
1059 struct sem_undo
*un
;
1060 int undos
= 0, alter
= 0, max
;
1061 struct sem_queue queue
;
1062 unsigned long jiffies_left
= 0;
1064 if (nsops
< 1 || semid
< 0)
1066 if (nsops
> sc_semopm
)
1068 if(nsops
> SEMOPM_FAST
) {
1069 sops
= kmalloc(sizeof(*sops
)*nsops
,GFP_KERNEL
);
1073 if (copy_from_user (sops
, tsops
, nsops
* sizeof(*tsops
))) {
1078 struct timespec _timeout
;
1079 if (copy_from_user(&_timeout
, timeout
, sizeof(*timeout
))) {
1083 if (_timeout
.tv_sec
< 0 || _timeout
.tv_nsec
< 0 ||
1084 _timeout
.tv_nsec
>= 1000000000L) {
1088 jiffies_left
= timespec_to_jiffies(&_timeout
);
1091 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
1092 if (sop
->sem_num
>= max
)
1094 if (sop
->sem_flg
& SEM_UNDO
)
1096 if (sop
->sem_op
!= 0)
1102 un
= find_undo(semid
);
1104 error
= PTR_ERR(un
);
1110 sma
= sem_lock(semid
);
1115 if (sem_checkid(sma
,semid
))
1116 goto out_unlock_free
;
1118 * semid identifies are not unique - find_undo may have
1119 * allocated an undo structure, it was invalidated by an RMID
1120 * and now a new array with received the same id. Check and retry.
1122 if (un
&& un
->semid
== -1) {
1127 if (max
>= sma
->sem_nsems
)
1128 goto out_unlock_free
;
1131 if (ipcperms(&sma
->sem_perm
, alter
? S_IWUGO
: S_IRUGO
))
1132 goto out_unlock_free
;
1134 error
= security_sem_semop(sma
, sops
, nsops
, alter
);
1136 goto out_unlock_free
;
1138 error
= try_atomic_semop (sma
, sops
, nsops
, un
, current
->tgid
);
1140 if (alter
&& error
== 0)
1142 goto out_unlock_free
;
1145 /* We need to sleep on this operation, so we put the current
1146 * task into the pending queue and go to sleep.
1151 queue
.nsops
= nsops
;
1153 queue
.pid
= current
->tgid
;
1155 queue
.alter
= alter
;
1157 append_to_queue(sma
,&queue
);
1159 prepend_to_queue(sma
,&queue
);
1161 queue
.status
= -EINTR
;
1162 queue
.sleeper
= current
;
1163 current
->state
= TASK_INTERRUPTIBLE
;
1167 jiffies_left
= schedule_timeout(jiffies_left
);
1171 error
= queue
.status
;
1172 while(unlikely(error
== IN_WAKEUP
)) {
1174 error
= queue
.status
;
1177 if (error
!= -EINTR
) {
1178 /* fast path: update_queue already obtained all requested
1183 sma
= sem_lock(semid
);
1185 if(queue
.prev
!= NULL
)
1192 * If queue.status != -EINTR we are woken up by another process
1194 error
= queue
.status
;
1195 if (error
!= -EINTR
) {
1196 goto out_unlock_free
;
1200 * If an interrupt occurred we have to clean up the queue
1202 if (timeout
&& jiffies_left
== 0)
1204 remove_from_queue(sma
,&queue
);
1205 goto out_unlock_free
;
1210 if(sops
!= fast_sops
)
1215 asmlinkage
long sys_semop (int semid
, struct sembuf __user
*tsops
, unsigned nsops
)
1217 return sys_semtimedop(semid
, tsops
, nsops
, NULL
);
1220 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1221 * parent and child tasks.
1223 * See the notes above unlock_semundo() regarding the spin_lock_init()
1224 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1225 * because of the reasoning in the comment above unlock_semundo.
1228 int copy_semundo(unsigned long clone_flags
, struct task_struct
*tsk
)
1230 struct sem_undo_list
*undo_list
;
1233 if (clone_flags
& CLONE_SYSVSEM
) {
1234 error
= get_undo_list(&undo_list
);
1237 atomic_inc(&undo_list
->refcnt
);
1238 tsk
->sysvsem
.undo_list
= undo_list
;
1240 tsk
->sysvsem
.undo_list
= NULL
;
1246 * add semadj values to semaphores, free undo structures.
1247 * undo structures are not freed when semaphore arrays are destroyed
1248 * so some of them may be out of date.
1249 * IMPLEMENTATION NOTE: There is some confusion over whether the
1250 * set of adjustments that needs to be done should be done in an atomic
1251 * manner or not. That is, if we are attempting to decrement the semval
1252 * should we queue up and wait until we can do so legally?
1253 * The original implementation attempted to do this (queue and wait).
1254 * The current implementation does not do so. The POSIX standard
1255 * and SVID should be consulted to determine what behavior is mandated.
1257 void exit_sem(struct task_struct
*tsk
)
1259 struct sem_undo_list
*undo_list
;
1260 struct sem_undo
*u
, **up
;
1262 undo_list
= tsk
->sysvsem
.undo_list
;
1266 if (!atomic_dec_and_test(&undo_list
->refcnt
))
1269 /* There's no need to hold the semundo list lock, as current
1270 * is the last task exiting for this undo list.
1272 for (up
= &undo_list
->proc_list
; (u
= *up
); *up
= u
->proc_next
, kfree(u
)) {
1273 struct sem_array
*sma
;
1275 struct sem_undo
*un
, **unp
;
1282 sma
= sem_lock(semid
);
1289 BUG_ON(sem_checkid(sma
,u
->semid
));
1291 /* remove u from the sma->undo list */
1292 for (unp
= &sma
->undo
; (un
= *unp
); unp
= &un
->id_next
) {
1296 printk ("exit_sem undo list error id=%d\n", u
->semid
);
1300 /* perform adjustments registered in u */
1301 nsems
= sma
->sem_nsems
;
1302 for (i
= 0; i
< nsems
; i
++) {
1303 struct sem
* sem
= &sma
->sem_base
[i
];
1305 sem
->semval
+= u
->semadj
[i
];
1307 * Range checks of the new semaphore value,
1308 * not defined by sus:
1309 * - Some unices ignore the undo entirely
1310 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1311 * - some cap the value (e.g. FreeBSD caps
1312 * at 0, but doesn't enforce SEMVMX)
1314 * Linux caps the semaphore value, both at 0
1317 * Manfred <manfred@colorfullife.com>
1319 if (sem
->semval
< 0)
1321 if (sem
->semval
> SEMVMX
)
1322 sem
->semval
= SEMVMX
;
1323 sem
->sempid
= current
->tgid
;
1326 sma
->sem_otime
= get_seconds();
1327 /* maybe some queued-up processes were waiting for this */
1335 #ifdef CONFIG_PROC_FS
1336 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
)
1338 struct sem_array
*sma
= it
;
1340 return seq_printf(s
,
1341 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",