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 <manfred@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>
65 * support for audit of ipc object properties and permission changes
66 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
70 * Pavel Emelianov <xemul@openvz.org>
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>
78 #include <linux/smp_lock.h>
79 #include <linux/security.h>
80 #include <linux/syscalls.h>
81 #include <linux/audit.h>
82 #include <linux/capability.h>
83 #include <linux/seq_file.h>
84 #include <linux/mutex.h>
85 #include <linux/nsproxy.h>
87 #include <asm/uaccess.h>
90 #define sem_ids(ns) (*((ns)->ids[IPC_SEM_IDS]))
92 #define sem_lock(ns, id) ((struct sem_array*)ipc_lock(&sem_ids(ns), id))
93 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
94 #define sem_rmid(ns, id) ((struct sem_array*)ipc_rmid(&sem_ids(ns), id))
95 #define sem_checkid(ns, sma, semid) \
96 ipc_checkid(&sem_ids(ns),&sma->sem_perm,semid)
97 #define sem_buildid(ns, id, seq) \
98 ipc_buildid(&sem_ids(ns), id, seq)
100 static struct ipc_ids init_sem_ids
;
102 static int newary(struct ipc_namespace
*, key_t
, int, int);
103 static void freeary(struct ipc_namespace
*ns
, struct sem_array
*sma
, int id
);
104 #ifdef CONFIG_PROC_FS
105 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
);
108 #define SEMMSL_FAST 256 /* 512 bytes on stack */
109 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
112 * linked list protection:
114 * sem_array.sem_pending{,last},
115 * sem_array.sem_undo: sem_lock() for read/write
116 * sem_undo.proc_next: only "current" is allowed to read/write that field.
120 #define sc_semmsl sem_ctls[0]
121 #define sc_semmns sem_ctls[1]
122 #define sc_semopm sem_ctls[2]
123 #define sc_semmni sem_ctls[3]
125 static void __ipc_init
__sem_init_ns(struct ipc_namespace
*ns
, struct ipc_ids
*ids
)
127 ns
->ids
[IPC_SEM_IDS
] = ids
;
128 ns
->sc_semmsl
= SEMMSL
;
129 ns
->sc_semmns
= SEMMNS
;
130 ns
->sc_semopm
= SEMOPM
;
131 ns
->sc_semmni
= SEMMNI
;
133 ipc_init_ids(ids
, ns
->sc_semmni
);
137 int sem_init_ns(struct ipc_namespace
*ns
)
141 ids
= kmalloc(sizeof(struct ipc_ids
), GFP_KERNEL
);
145 __sem_init_ns(ns
, ids
);
149 void sem_exit_ns(struct ipc_namespace
*ns
)
152 struct sem_array
*sma
;
154 mutex_lock(&sem_ids(ns
).mutex
);
155 for (i
= 0; i
<= sem_ids(ns
).max_id
; i
++) {
156 sma
= sem_lock(ns
, i
);
162 mutex_unlock(&sem_ids(ns
).mutex
);
164 ipc_fini_ids(ns
->ids
[IPC_SEM_IDS
]);
165 kfree(ns
->ids
[IPC_SEM_IDS
]);
166 ns
->ids
[IPC_SEM_IDS
] = NULL
;
170 void __init
sem_init (void)
172 __sem_init_ns(&init_ipc_ns
, &init_sem_ids
);
173 ipc_init_proc_interface("sysvipc/sem",
174 " key semid perms nsems uid gid cuid cgid otime ctime\n",
175 IPC_SEM_IDS
, sysvipc_sem_proc_show
);
179 * Lockless wakeup algorithm:
180 * Without the check/retry algorithm a lockless wakeup is possible:
181 * - queue.status is initialized to -EINTR before blocking.
182 * - wakeup is performed by
183 * * unlinking the queue entry from sma->sem_pending
184 * * setting queue.status to IN_WAKEUP
185 * This is the notification for the blocked thread that a
186 * result value is imminent.
187 * * call wake_up_process
188 * * set queue.status to the final value.
189 * - the previously blocked thread checks queue.status:
190 * * if it's IN_WAKEUP, then it must wait until the value changes
191 * * if it's not -EINTR, then the operation was completed by
192 * update_queue. semtimedop can return queue.status without
193 * performing any operation on the sem array.
194 * * otherwise it must acquire the spinlock and check what's up.
196 * The two-stage algorithm is necessary to protect against the following
198 * - if queue.status is set after wake_up_process, then the woken up idle
199 * thread could race forward and try (and fail) to acquire sma->lock
200 * before update_queue had a chance to set queue.status
201 * - if queue.status is written before wake_up_process and if the
202 * blocked process is woken up by a signal between writing
203 * queue.status and the wake_up_process, then the woken up
204 * process could return from semtimedop and die by calling
205 * sys_exit before wake_up_process is called. Then wake_up_process
206 * will oops, because the task structure is already invalid.
207 * (yes, this happened on s390 with sysv msg).
212 static int newary (struct ipc_namespace
*ns
, key_t key
, int nsems
, int semflg
)
216 struct sem_array
*sma
;
221 if (ns
->used_sems
+ nsems
> ns
->sc_semmns
)
224 size
= sizeof (*sma
) + nsems
* sizeof (struct sem
);
225 sma
= ipc_rcu_alloc(size
);
229 memset (sma
, 0, size
);
231 sma
->sem_perm
.mode
= (semflg
& S_IRWXUGO
);
232 sma
->sem_perm
.key
= key
;
234 sma
->sem_perm
.security
= NULL
;
235 retval
= security_sem_alloc(sma
);
241 id
= ipc_addid(&sem_ids(ns
), &sma
->sem_perm
, ns
->sc_semmni
);
243 security_sem_free(sma
);
247 ns
->used_sems
+= nsems
;
249 sma
->sem_id
= sem_buildid(ns
, id
, sma
->sem_perm
.seq
);
250 sma
->sem_base
= (struct sem
*) &sma
[1];
251 /* sma->sem_pending = NULL; */
252 sma
->sem_pending_last
= &sma
->sem_pending
;
253 /* sma->undo = NULL; */
254 sma
->sem_nsems
= nsems
;
255 sma
->sem_ctime
= get_seconds();
261 asmlinkage
long sys_semget (key_t key
, int nsems
, int semflg
)
263 int id
, err
= -EINVAL
;
264 struct sem_array
*sma
;
265 struct ipc_namespace
*ns
;
267 ns
= current
->nsproxy
->ipc_ns
;
269 if (nsems
< 0 || nsems
> ns
->sc_semmsl
)
271 mutex_lock(&sem_ids(ns
).mutex
);
273 if (key
== IPC_PRIVATE
) {
274 err
= newary(ns
, key
, nsems
, semflg
);
275 } else if ((id
= ipc_findkey(&sem_ids(ns
), key
)) == -1) { /* key not used */
276 if (!(semflg
& IPC_CREAT
))
279 err
= newary(ns
, key
, nsems
, semflg
);
280 } else if (semflg
& IPC_CREAT
&& semflg
& IPC_EXCL
) {
283 sma
= sem_lock(ns
, id
);
285 if (nsems
> sma
->sem_nsems
)
287 else if (ipcperms(&sma
->sem_perm
, semflg
))
290 int semid
= sem_buildid(ns
, id
, sma
->sem_perm
.seq
);
291 err
= security_sem_associate(sma
, semflg
);
298 mutex_unlock(&sem_ids(ns
).mutex
);
302 /* Manage the doubly linked list sma->sem_pending as a FIFO:
303 * insert new queue elements at the tail sma->sem_pending_last.
305 static inline void append_to_queue (struct sem_array
* sma
,
306 struct sem_queue
* q
)
308 *(q
->prev
= sma
->sem_pending_last
) = q
;
309 *(sma
->sem_pending_last
= &q
->next
) = NULL
;
312 static inline void prepend_to_queue (struct sem_array
* sma
,
313 struct sem_queue
* q
)
315 q
->next
= sma
->sem_pending
;
316 *(q
->prev
= &sma
->sem_pending
) = q
;
318 q
->next
->prev
= &q
->next
;
319 else /* sma->sem_pending_last == &sma->sem_pending */
320 sma
->sem_pending_last
= &q
->next
;
323 static inline void remove_from_queue (struct sem_array
* sma
,
324 struct sem_queue
* q
)
326 *(q
->prev
) = q
->next
;
328 q
->next
->prev
= q
->prev
;
329 else /* sma->sem_pending_last == &q->next */
330 sma
->sem_pending_last
= q
->prev
;
331 q
->prev
= NULL
; /* mark as removed */
335 * Determine whether a sequence of semaphore operations would succeed
336 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
339 static int try_atomic_semop (struct sem_array
* sma
, struct sembuf
* sops
,
340 int nsops
, struct sem_undo
*un
, int pid
)
346 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
347 curr
= sma
->sem_base
+ sop
->sem_num
;
348 sem_op
= sop
->sem_op
;
349 result
= curr
->semval
;
351 if (!sem_op
&& result
)
359 if (sop
->sem_flg
& SEM_UNDO
) {
360 int undo
= un
->semadj
[sop
->sem_num
] - sem_op
;
362 * Exceeding the undo range is an error.
364 if (undo
< (-SEMAEM
- 1) || undo
> SEMAEM
)
367 curr
->semval
= result
;
371 while (sop
>= sops
) {
372 sma
->sem_base
[sop
->sem_num
].sempid
= pid
;
373 if (sop
->sem_flg
& SEM_UNDO
)
374 un
->semadj
[sop
->sem_num
] -= sop
->sem_op
;
378 sma
->sem_otime
= get_seconds();
386 if (sop
->sem_flg
& IPC_NOWAIT
)
393 while (sop
>= sops
) {
394 sma
->sem_base
[sop
->sem_num
].semval
-= sop
->sem_op
;
401 /* Go through the pending queue for the indicated semaphore
402 * looking for tasks that can be completed.
404 static void update_queue (struct sem_array
* sma
)
407 struct sem_queue
* q
;
409 q
= sma
->sem_pending
;
411 error
= try_atomic_semop(sma
, q
->sops
, q
->nsops
,
414 /* Does q->sleeper still need to sleep? */
417 remove_from_queue(sma
,q
);
418 q
->status
= IN_WAKEUP
;
420 * Continue scanning. The next operation
421 * that must be checked depends on the type of the
422 * completed operation:
423 * - if the operation modified the array, then
424 * restart from the head of the queue and
425 * check for threads that might be waiting
426 * for semaphore values to become 0.
427 * - if the operation didn't modify the array,
428 * then just continue.
431 n
= sma
->sem_pending
;
434 wake_up_process(q
->sleeper
);
435 /* hands-off: q will disappear immediately after
447 /* The following counts are associated to each semaphore:
448 * semncnt number of tasks waiting on semval being nonzero
449 * semzcnt number of tasks waiting on semval being zero
450 * This model assumes that a task waits on exactly one semaphore.
451 * Since semaphore operations are to be performed atomically, tasks actually
452 * wait on a whole sequence of semaphores simultaneously.
453 * The counts we return here are a rough approximation, but still
454 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
456 static int count_semncnt (struct sem_array
* sma
, ushort semnum
)
459 struct sem_queue
* q
;
462 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
463 struct sembuf
* sops
= q
->sops
;
464 int nsops
= q
->nsops
;
466 for (i
= 0; i
< nsops
; i
++)
467 if (sops
[i
].sem_num
== semnum
468 && (sops
[i
].sem_op
< 0)
469 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
474 static int count_semzcnt (struct sem_array
* sma
, ushort semnum
)
477 struct sem_queue
* q
;
480 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
481 struct sembuf
* sops
= q
->sops
;
482 int nsops
= q
->nsops
;
484 for (i
= 0; i
< nsops
; i
++)
485 if (sops
[i
].sem_num
== semnum
486 && (sops
[i
].sem_op
== 0)
487 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
493 /* Free a semaphore set. freeary() is called with sem_ids.mutex locked and
494 * the spinlock for this semaphore set hold. sem_ids.mutex remains locked
497 static void freeary (struct ipc_namespace
*ns
, struct sem_array
*sma
, int id
)
503 /* Invalidate the existing undo structures for this semaphore set.
504 * (They will be freed without any further action in exit_sem()
505 * or during the next semop.)
507 for (un
= sma
->undo
; un
; un
= un
->id_next
)
510 /* Wake up all pending processes and let them fail with EIDRM. */
511 q
= sma
->sem_pending
;
514 /* lazy remove_from_queue: we are killing the whole queue */
517 q
->status
= IN_WAKEUP
;
518 wake_up_process(q
->sleeper
); /* doesn't sleep */
520 q
->status
= -EIDRM
; /* hands-off q */
524 /* Remove the semaphore set from the ID array*/
525 sma
= sem_rmid(ns
, id
);
528 ns
->used_sems
-= sma
->sem_nsems
;
529 size
= sizeof (*sma
) + sma
->sem_nsems
* sizeof (struct sem
);
530 security_sem_free(sma
);
534 static unsigned long copy_semid_to_user(void __user
*buf
, struct semid64_ds
*in
, int version
)
538 return copy_to_user(buf
, in
, sizeof(*in
));
543 ipc64_perm_to_ipc_perm(&in
->sem_perm
, &out
.sem_perm
);
545 out
.sem_otime
= in
->sem_otime
;
546 out
.sem_ctime
= in
->sem_ctime
;
547 out
.sem_nsems
= in
->sem_nsems
;
549 return copy_to_user(buf
, &out
, sizeof(out
));
556 static int semctl_nolock(struct ipc_namespace
*ns
, int semid
, int semnum
,
557 int cmd
, int version
, union semun arg
)
560 struct sem_array
*sma
;
566 struct seminfo seminfo
;
569 err
= security_sem_semctl(NULL
, cmd
);
573 memset(&seminfo
,0,sizeof(seminfo
));
574 seminfo
.semmni
= ns
->sc_semmni
;
575 seminfo
.semmns
= ns
->sc_semmns
;
576 seminfo
.semmsl
= ns
->sc_semmsl
;
577 seminfo
.semopm
= ns
->sc_semopm
;
578 seminfo
.semvmx
= SEMVMX
;
579 seminfo
.semmnu
= SEMMNU
;
580 seminfo
.semmap
= SEMMAP
;
581 seminfo
.semume
= SEMUME
;
582 mutex_lock(&sem_ids(ns
).mutex
);
583 if (cmd
== SEM_INFO
) {
584 seminfo
.semusz
= sem_ids(ns
).in_use
;
585 seminfo
.semaem
= ns
->used_sems
;
587 seminfo
.semusz
= SEMUSZ
;
588 seminfo
.semaem
= SEMAEM
;
590 max_id
= sem_ids(ns
).max_id
;
591 mutex_unlock(&sem_ids(ns
).mutex
);
592 if (copy_to_user (arg
.__buf
, &seminfo
, sizeof(struct seminfo
)))
594 return (max_id
< 0) ? 0: max_id
;
598 struct semid64_ds tbuf
;
601 if(semid
>= sem_ids(ns
).entries
->size
)
604 memset(&tbuf
,0,sizeof(tbuf
));
606 sma
= sem_lock(ns
, semid
);
611 if (ipcperms (&sma
->sem_perm
, S_IRUGO
))
614 err
= security_sem_semctl(sma
, cmd
);
618 id
= sem_buildid(ns
, semid
, sma
->sem_perm
.seq
);
620 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
621 tbuf
.sem_otime
= sma
->sem_otime
;
622 tbuf
.sem_ctime
= sma
->sem_ctime
;
623 tbuf
.sem_nsems
= sma
->sem_nsems
;
625 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
638 static int semctl_main(struct ipc_namespace
*ns
, int semid
, int semnum
,
639 int cmd
, int version
, union semun arg
)
641 struct sem_array
*sma
;
644 ushort fast_sem_io
[SEMMSL_FAST
];
645 ushort
* sem_io
= fast_sem_io
;
648 sma
= sem_lock(ns
, semid
);
652 nsems
= sma
->sem_nsems
;
655 if (sem_checkid(ns
,sma
,semid
))
659 if (ipcperms (&sma
->sem_perm
, (cmd
==SETVAL
||cmd
==SETALL
)?S_IWUGO
:S_IRUGO
))
662 err
= security_sem_semctl(sma
, cmd
);
670 ushort __user
*array
= arg
.array
;
673 if(nsems
> SEMMSL_FAST
) {
677 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
679 ipc_lock_by_ptr(&sma
->sem_perm
);
685 ipc_lock_by_ptr(&sma
->sem_perm
);
687 if (sma
->sem_perm
.deleted
) {
694 for (i
= 0; i
< sma
->sem_nsems
; i
++)
695 sem_io
[i
] = sma
->sem_base
[i
].semval
;
698 if(copy_to_user(array
, sem_io
, nsems
*sizeof(ushort
)))
710 if(nsems
> SEMMSL_FAST
) {
711 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
713 ipc_lock_by_ptr(&sma
->sem_perm
);
720 if (copy_from_user (sem_io
, arg
.array
, nsems
*sizeof(ushort
))) {
721 ipc_lock_by_ptr(&sma
->sem_perm
);
728 for (i
= 0; i
< nsems
; i
++) {
729 if (sem_io
[i
] > SEMVMX
) {
730 ipc_lock_by_ptr(&sma
->sem_perm
);
737 ipc_lock_by_ptr(&sma
->sem_perm
);
739 if (sma
->sem_perm
.deleted
) {
745 for (i
= 0; i
< nsems
; i
++)
746 sma
->sem_base
[i
].semval
= sem_io
[i
];
747 for (un
= sma
->undo
; un
; un
= un
->id_next
)
748 for (i
= 0; i
< nsems
; i
++)
750 sma
->sem_ctime
= get_seconds();
751 /* maybe some queued-up processes were waiting for this */
758 struct semid64_ds tbuf
;
759 memset(&tbuf
,0,sizeof(tbuf
));
760 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
761 tbuf
.sem_otime
= sma
->sem_otime
;
762 tbuf
.sem_ctime
= sma
->sem_ctime
;
763 tbuf
.sem_nsems
= sma
->sem_nsems
;
765 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
769 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
772 if(semnum
< 0 || semnum
>= nsems
)
775 curr
= &sma
->sem_base
[semnum
];
785 err
= count_semncnt(sma
,semnum
);
788 err
= count_semzcnt(sma
,semnum
);
795 if (val
> SEMVMX
|| val
< 0)
798 for (un
= sma
->undo
; un
; un
= un
->id_next
)
799 un
->semadj
[semnum
] = 0;
801 curr
->sempid
= current
->tgid
;
802 sma
->sem_ctime
= get_seconds();
803 /* maybe some queued-up processes were waiting for this */
812 if(sem_io
!= fast_sem_io
)
813 ipc_free(sem_io
, sizeof(ushort
)*nsems
);
823 static inline unsigned long copy_semid_from_user(struct sem_setbuf
*out
, void __user
*buf
, int version
)
828 struct semid64_ds tbuf
;
830 if(copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
833 out
->uid
= tbuf
.sem_perm
.uid
;
834 out
->gid
= tbuf
.sem_perm
.gid
;
835 out
->mode
= tbuf
.sem_perm
.mode
;
841 struct semid_ds tbuf_old
;
843 if(copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
846 out
->uid
= tbuf_old
.sem_perm
.uid
;
847 out
->gid
= tbuf_old
.sem_perm
.gid
;
848 out
->mode
= tbuf_old
.sem_perm
.mode
;
857 static int semctl_down(struct ipc_namespace
*ns
, int semid
, int semnum
,
858 int cmd
, int version
, union semun arg
)
860 struct sem_array
*sma
;
862 struct sem_setbuf setbuf
;
863 struct kern_ipc_perm
*ipcp
;
866 if(copy_semid_from_user (&setbuf
, arg
.buf
, version
))
869 sma
= sem_lock(ns
, semid
);
873 if (sem_checkid(ns
,sma
,semid
)) {
877 ipcp
= &sma
->sem_perm
;
879 err
= audit_ipc_obj(ipcp
);
883 if (cmd
== IPC_SET
) {
884 err
= audit_ipc_set_perm(0, setbuf
.uid
, setbuf
.gid
, setbuf
.mode
);
888 if (current
->euid
!= ipcp
->cuid
&&
889 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
)) {
894 err
= security_sem_semctl(sma
, cmd
);
900 freeary(ns
, sma
, semid
);
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();
924 asmlinkage
long sys_semctl (int semid
, int semnum
, int cmd
, union semun arg
)
928 struct ipc_namespace
*ns
;
933 version
= ipc_parse_version(&cmd
);
934 ns
= current
->nsproxy
->ipc_ns
;
940 err
= semctl_nolock(ns
,semid
,semnum
,cmd
,version
,arg
);
950 err
= semctl_main(ns
,semid
,semnum
,cmd
,version
,arg
);
954 mutex_lock(&sem_ids(ns
).mutex
);
955 err
= semctl_down(ns
,semid
,semnum
,cmd
,version
,arg
);
956 mutex_unlock(&sem_ids(ns
).mutex
);
963 static inline void lock_semundo(void)
965 struct sem_undo_list
*undo_list
;
967 undo_list
= current
->sysvsem
.undo_list
;
969 spin_lock(&undo_list
->lock
);
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.
983 static inline void unlock_semundo(void)
985 struct sem_undo_list
*undo_list
;
987 undo_list
= current
->sysvsem
.undo_list
;
989 spin_unlock(&undo_list
->lock
);
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
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
1002 * This can block, so callers must hold no locks.
1004 static inline int get_undo_list(struct sem_undo_list
**undo_listp
)
1006 struct sem_undo_list
*undo_list
;
1008 undo_list
= current
->sysvsem
.undo_list
;
1010 undo_list
= kzalloc(sizeof(*undo_list
), GFP_KERNEL
);
1011 if (undo_list
== NULL
)
1013 spin_lock_init(&undo_list
->lock
);
1014 atomic_set(&undo_list
->refcnt
, 1);
1015 current
->sysvsem
.undo_list
= undo_list
;
1017 *undo_listp
= undo_list
;
1021 static struct sem_undo
*lookup_undo(struct sem_undo_list
*ulp
, int semid
)
1023 struct sem_undo
**last
, *un
;
1025 last
= &ulp
->proc_list
;
1028 if(un
->semid
==semid
)
1031 *last
=un
->proc_next
;
1034 last
=&un
->proc_next
;
1041 static struct sem_undo
*find_undo(struct ipc_namespace
*ns
, int semid
)
1043 struct sem_array
*sma
;
1044 struct sem_undo_list
*ulp
;
1045 struct sem_undo
*un
, *new;
1049 error
= get_undo_list(&ulp
);
1051 return ERR_PTR(error
);
1054 un
= lookup_undo(ulp
, semid
);
1056 if (likely(un
!=NULL
))
1059 /* no undo structure around - allocate one. */
1060 sma
= sem_lock(ns
, semid
);
1061 un
= ERR_PTR(-EINVAL
);
1064 un
= ERR_PTR(-EIDRM
);
1065 if (sem_checkid(ns
,sma
,semid
)) {
1069 nsems
= sma
->sem_nsems
;
1070 ipc_rcu_getref(sma
);
1073 new = (struct sem_undo
*) kmalloc(sizeof(struct sem_undo
) + sizeof(short)*nsems
, GFP_KERNEL
);
1075 ipc_lock_by_ptr(&sma
->sem_perm
);
1076 ipc_rcu_putref(sma
);
1078 return ERR_PTR(-ENOMEM
);
1080 memset(new, 0, sizeof(struct sem_undo
) + sizeof(short)*nsems
);
1081 new->semadj
= (short *) &new[1];
1085 un
= lookup_undo(ulp
, semid
);
1089 ipc_lock_by_ptr(&sma
->sem_perm
);
1090 ipc_rcu_putref(sma
);
1094 ipc_lock_by_ptr(&sma
->sem_perm
);
1095 ipc_rcu_putref(sma
);
1096 if (sma
->sem_perm
.deleted
) {
1100 un
= ERR_PTR(-EIDRM
);
1103 new->proc_next
= ulp
->proc_list
;
1104 ulp
->proc_list
= new;
1105 new->id_next
= sma
->undo
;
1114 asmlinkage
long sys_semtimedop(int semid
, struct sembuf __user
*tsops
,
1115 unsigned nsops
, const struct timespec __user
*timeout
)
1117 int error
= -EINVAL
;
1118 struct sem_array
*sma
;
1119 struct sembuf fast_sops
[SEMOPM_FAST
];
1120 struct sembuf
* sops
= fast_sops
, *sop
;
1121 struct sem_undo
*un
;
1122 int undos
= 0, alter
= 0, max
;
1123 struct sem_queue queue
;
1124 unsigned long jiffies_left
= 0;
1125 struct ipc_namespace
*ns
;
1127 ns
= current
->nsproxy
->ipc_ns
;
1129 if (nsops
< 1 || semid
< 0)
1131 if (nsops
> ns
->sc_semopm
)
1133 if(nsops
> SEMOPM_FAST
) {
1134 sops
= kmalloc(sizeof(*sops
)*nsops
,GFP_KERNEL
);
1138 if (copy_from_user (sops
, tsops
, nsops
* sizeof(*tsops
))) {
1143 struct timespec _timeout
;
1144 if (copy_from_user(&_timeout
, timeout
, sizeof(*timeout
))) {
1148 if (_timeout
.tv_sec
< 0 || _timeout
.tv_nsec
< 0 ||
1149 _timeout
.tv_nsec
>= 1000000000L) {
1153 jiffies_left
= timespec_to_jiffies(&_timeout
);
1156 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
1157 if (sop
->sem_num
>= max
)
1159 if (sop
->sem_flg
& SEM_UNDO
)
1161 if (sop
->sem_op
!= 0)
1167 un
= find_undo(ns
, semid
);
1169 error
= PTR_ERR(un
);
1175 sma
= sem_lock(ns
, semid
);
1180 if (sem_checkid(ns
,sma
,semid
))
1181 goto out_unlock_free
;
1183 * semid identifies are not unique - find_undo may have
1184 * allocated an undo structure, it was invalidated by an RMID
1185 * and now a new array with received the same id. Check and retry.
1187 if (un
&& un
->semid
== -1) {
1192 if (max
>= sma
->sem_nsems
)
1193 goto out_unlock_free
;
1196 if (ipcperms(&sma
->sem_perm
, alter
? S_IWUGO
: S_IRUGO
))
1197 goto out_unlock_free
;
1199 error
= security_sem_semop(sma
, sops
, nsops
, alter
);
1201 goto out_unlock_free
;
1203 error
= try_atomic_semop (sma
, sops
, nsops
, un
, current
->tgid
);
1205 if (alter
&& error
== 0)
1207 goto out_unlock_free
;
1210 /* We need to sleep on this operation, so we put the current
1211 * task into the pending queue and go to sleep.
1216 queue
.nsops
= nsops
;
1218 queue
.pid
= current
->tgid
;
1220 queue
.alter
= alter
;
1222 append_to_queue(sma
,&queue
);
1224 prepend_to_queue(sma
,&queue
);
1226 queue
.status
= -EINTR
;
1227 queue
.sleeper
= current
;
1228 current
->state
= TASK_INTERRUPTIBLE
;
1232 jiffies_left
= schedule_timeout(jiffies_left
);
1236 error
= queue
.status
;
1237 while(unlikely(error
== IN_WAKEUP
)) {
1239 error
= queue
.status
;
1242 if (error
!= -EINTR
) {
1243 /* fast path: update_queue already obtained all requested
1248 sma
= sem_lock(ns
, semid
);
1250 BUG_ON(queue
.prev
!= NULL
);
1256 * If queue.status != -EINTR we are woken up by another process
1258 error
= queue
.status
;
1259 if (error
!= -EINTR
) {
1260 goto out_unlock_free
;
1264 * If an interrupt occurred we have to clean up the queue
1266 if (timeout
&& jiffies_left
== 0)
1268 remove_from_queue(sma
,&queue
);
1269 goto out_unlock_free
;
1274 if(sops
!= fast_sops
)
1279 asmlinkage
long sys_semop (int semid
, struct sembuf __user
*tsops
, unsigned nsops
)
1281 return sys_semtimedop(semid
, tsops
, nsops
, NULL
);
1284 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1285 * parent and child tasks.
1287 * See the notes above unlock_semundo() regarding the spin_lock_init()
1288 * in this code. Initialize the undo_list->lock here instead of get_undo_list()
1289 * because of the reasoning in the comment above unlock_semundo.
1292 int copy_semundo(unsigned long clone_flags
, struct task_struct
*tsk
)
1294 struct sem_undo_list
*undo_list
;
1297 if (clone_flags
& CLONE_SYSVSEM
) {
1298 error
= get_undo_list(&undo_list
);
1301 atomic_inc(&undo_list
->refcnt
);
1302 tsk
->sysvsem
.undo_list
= undo_list
;
1304 tsk
->sysvsem
.undo_list
= NULL
;
1310 * add semadj values to semaphores, free undo structures.
1311 * undo structures are not freed when semaphore arrays are destroyed
1312 * so some of them may be out of date.
1313 * IMPLEMENTATION NOTE: There is some confusion over whether the
1314 * set of adjustments that needs to be done should be done in an atomic
1315 * manner or not. That is, if we are attempting to decrement the semval
1316 * should we queue up and wait until we can do so legally?
1317 * The original implementation attempted to do this (queue and wait).
1318 * The current implementation does not do so. The POSIX standard
1319 * and SVID should be consulted to determine what behavior is mandated.
1321 void exit_sem(struct task_struct
*tsk
)
1323 struct sem_undo_list
*undo_list
;
1324 struct sem_undo
*u
, **up
;
1325 struct ipc_namespace
*ns
;
1327 undo_list
= tsk
->sysvsem
.undo_list
;
1331 if (!atomic_dec_and_test(&undo_list
->refcnt
))
1334 ns
= tsk
->nsproxy
->ipc_ns
;
1335 /* There's no need to hold the semundo list lock, as current
1336 * is the last task exiting for this undo list.
1338 for (up
= &undo_list
->proc_list
; (u
= *up
); *up
= u
->proc_next
, kfree(u
)) {
1339 struct sem_array
*sma
;
1341 struct sem_undo
*un
, **unp
;
1348 sma
= sem_lock(ns
, semid
);
1355 BUG_ON(sem_checkid(ns
,sma
,u
->semid
));
1357 /* remove u from the sma->undo list */
1358 for (unp
= &sma
->undo
; (un
= *unp
); unp
= &un
->id_next
) {
1362 printk ("exit_sem undo list error id=%d\n", u
->semid
);
1366 /* perform adjustments registered in u */
1367 nsems
= sma
->sem_nsems
;
1368 for (i
= 0; i
< nsems
; i
++) {
1369 struct sem
* semaphore
= &sma
->sem_base
[i
];
1371 semaphore
->semval
+= u
->semadj
[i
];
1373 * Range checks of the new semaphore value,
1374 * not defined by sus:
1375 * - Some unices ignore the undo entirely
1376 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1377 * - some cap the value (e.g. FreeBSD caps
1378 * at 0, but doesn't enforce SEMVMX)
1380 * Linux caps the semaphore value, both at 0
1383 * Manfred <manfred@colorfullife.com>
1385 if (semaphore
->semval
< 0)
1386 semaphore
->semval
= 0;
1387 if (semaphore
->semval
> SEMVMX
)
1388 semaphore
->semval
= SEMVMX
;
1389 semaphore
->sempid
= current
->tgid
;
1392 sma
->sem_otime
= get_seconds();
1393 /* maybe some queued-up processes were waiting for this */
1401 #ifdef CONFIG_PROC_FS
1402 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
)
1404 struct sem_array
*sma
= it
;
1406 return seq_printf(s
,
1407 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",