]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - ipc/sem.c
0b45a4d383c6ae662845f5e26c6bc19f25c10a22
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/security.h>
79 #include <linux/syscalls.h>
80 #include <linux/audit.h>
81 #include <linux/capability.h>
82 #include <linux/seq_file.h>
83 #include <linux/rwsem.h>
84 #include <linux/nsproxy.h>
85 #include <linux/ipc_namespace.h>
87 #include <asm/uaccess.h>
90 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
92 #define sem_unlock(sma) ipc_unlock(&(sma)->sem_perm)
93 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
94 #define sem_buildid(id, seq) ipc_buildid(id, seq)
96 static int newary(struct ipc_namespace
*, struct ipc_params
*);
97 static void freeary(struct ipc_namespace
*, struct kern_ipc_perm
*);
99 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
);
102 #define SEMMSL_FAST 256 /* 512 bytes on stack */
103 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
106 * linked list protection:
108 * sem_array.sem_pending{,last},
109 * sem_array.sem_undo: sem_lock() for read/write
110 * sem_undo.proc_next: only "current" is allowed to read/write that field.
114 #define sc_semmsl sem_ctls[0]
115 #define sc_semmns sem_ctls[1]
116 #define sc_semopm sem_ctls[2]
117 #define sc_semmni sem_ctls[3]
119 void sem_init_ns(struct ipc_namespace
*ns
)
121 ns
->sc_semmsl
= SEMMSL
;
122 ns
->sc_semmns
= SEMMNS
;
123 ns
->sc_semopm
= SEMOPM
;
124 ns
->sc_semmni
= SEMMNI
;
126 ipc_init_ids(&ns
->ids
[IPC_SEM_IDS
]);
130 void sem_exit_ns(struct ipc_namespace
*ns
)
132 free_ipcs(ns
, &sem_ids(ns
), freeary
);
136 void __init
sem_init (void)
138 sem_init_ns(&init_ipc_ns
);
139 ipc_init_proc_interface("sysvipc/sem",
140 " key semid perms nsems uid gid cuid cgid otime ctime\n",
141 IPC_SEM_IDS
, sysvipc_sem_proc_show
);
145 * This routine is called in the paths where the rw_mutex is held to protect
146 * access to the idr tree.
148 static inline struct sem_array
*sem_lock_check_down(struct ipc_namespace
*ns
,
151 struct kern_ipc_perm
*ipcp
= ipc_lock_check_down(&sem_ids(ns
), id
);
154 return (struct sem_array
*)ipcp
;
156 return container_of(ipcp
, struct sem_array
, sem_perm
);
160 * sem_lock_(check_) routines are called in the paths where the rw_mutex
163 static inline struct sem_array
*sem_lock(struct ipc_namespace
*ns
, int id
)
165 struct kern_ipc_perm
*ipcp
= ipc_lock(&sem_ids(ns
), id
);
168 return (struct sem_array
*)ipcp
;
170 return container_of(ipcp
, struct sem_array
, sem_perm
);
173 static inline struct sem_array
*sem_lock_check(struct ipc_namespace
*ns
,
176 struct kern_ipc_perm
*ipcp
= ipc_lock_check(&sem_ids(ns
), id
);
179 return (struct sem_array
*)ipcp
;
181 return container_of(ipcp
, struct sem_array
, sem_perm
);
184 static inline void sem_rmid(struct ipc_namespace
*ns
, struct sem_array
*s
)
186 ipc_rmid(&sem_ids(ns
), &s
->sem_perm
);
190 * Lockless wakeup algorithm:
191 * Without the check/retry algorithm a lockless wakeup is possible:
192 * - queue.status is initialized to -EINTR before blocking.
193 * - wakeup is performed by
194 * * unlinking the queue entry from sma->sem_pending
195 * * setting queue.status to IN_WAKEUP
196 * This is the notification for the blocked thread that a
197 * result value is imminent.
198 * * call wake_up_process
199 * * set queue.status to the final value.
200 * - the previously blocked thread checks queue.status:
201 * * if it's IN_WAKEUP, then it must wait until the value changes
202 * * if it's not -EINTR, then the operation was completed by
203 * update_queue. semtimedop can return queue.status without
204 * performing any operation on the sem array.
205 * * otherwise it must acquire the spinlock and check what's up.
207 * The two-stage algorithm is necessary to protect against the following
209 * - if queue.status is set after wake_up_process, then the woken up idle
210 * thread could race forward and try (and fail) to acquire sma->lock
211 * before update_queue had a chance to set queue.status
212 * - if queue.status is written before wake_up_process and if the
213 * blocked process is woken up by a signal between writing
214 * queue.status and the wake_up_process, then the woken up
215 * process could return from semtimedop and die by calling
216 * sys_exit before wake_up_process is called. Then wake_up_process
217 * will oops, because the task structure is already invalid.
218 * (yes, this happened on s390 with sysv msg).
224 * newary - Create a new semaphore set
226 * @params: ptr to the structure that contains key, semflg and nsems
228 * Called with sem_ids.rw_mutex held (as a writer)
231 static int newary(struct ipc_namespace
*ns
, struct ipc_params
*params
)
235 struct sem_array
*sma
;
237 key_t key
= params
->key
;
238 int nsems
= params
->u
.nsems
;
239 int semflg
= params
->flg
;
243 if (ns
->used_sems
+ nsems
> ns
->sc_semmns
)
246 size
= sizeof (*sma
) + nsems
* sizeof (struct sem
);
247 sma
= ipc_rcu_alloc(size
);
251 memset (sma
, 0, size
);
253 sma
->sem_perm
.mode
= (semflg
& S_IRWXUGO
);
254 sma
->sem_perm
.key
= key
;
256 sma
->sem_perm
.security
= NULL
;
257 retval
= security_sem_alloc(sma
);
263 id
= ipc_addid(&sem_ids(ns
), &sma
->sem_perm
, ns
->sc_semmni
);
265 security_sem_free(sma
);
269 ns
->used_sems
+= nsems
;
271 sma
->sem_perm
.id
= sem_buildid(id
, sma
->sem_perm
.seq
);
272 sma
->sem_base
= (struct sem
*) &sma
[1];
273 /* sma->sem_pending = NULL; */
274 sma
->sem_pending_last
= &sma
->sem_pending
;
275 /* sma->undo = NULL; */
276 sma
->sem_nsems
= nsems
;
277 sma
->sem_ctime
= get_seconds();
280 return sma
->sem_perm
.id
;
285 * Called with sem_ids.rw_mutex and ipcp locked.
287 static inline int sem_security(struct kern_ipc_perm
*ipcp
, int semflg
)
289 struct sem_array
*sma
;
291 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
292 return security_sem_associate(sma
, semflg
);
296 * Called with sem_ids.rw_mutex and ipcp locked.
298 static inline int sem_more_checks(struct kern_ipc_perm
*ipcp
,
299 struct ipc_params
*params
)
301 struct sem_array
*sma
;
303 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
304 if (params
->u
.nsems
> sma
->sem_nsems
)
310 asmlinkage
long sys_semget(key_t key
, int nsems
, int semflg
)
312 struct ipc_namespace
*ns
;
313 struct ipc_ops sem_ops
;
314 struct ipc_params sem_params
;
316 ns
= current
->nsproxy
->ipc_ns
;
318 if (nsems
< 0 || nsems
> ns
->sc_semmsl
)
321 sem_ops
.getnew
= newary
;
322 sem_ops
.associate
= sem_security
;
323 sem_ops
.more_checks
= sem_more_checks
;
325 sem_params
.key
= key
;
326 sem_params
.flg
= semflg
;
327 sem_params
.u
.nsems
= nsems
;
329 return ipcget(ns
, &sem_ids(ns
), &sem_ops
, &sem_params
);
332 /* Manage the doubly linked list sma->sem_pending as a FIFO:
333 * insert new queue elements at the tail sma->sem_pending_last.
335 static inline void append_to_queue (struct sem_array
* sma
,
336 struct sem_queue
* q
)
338 *(q
->prev
= sma
->sem_pending_last
) = q
;
339 *(sma
->sem_pending_last
= &q
->next
) = NULL
;
342 static inline void prepend_to_queue (struct sem_array
* sma
,
343 struct sem_queue
* q
)
345 q
->next
= sma
->sem_pending
;
346 *(q
->prev
= &sma
->sem_pending
) = q
;
348 q
->next
->prev
= &q
->next
;
349 else /* sma->sem_pending_last == &sma->sem_pending */
350 sma
->sem_pending_last
= &q
->next
;
353 static inline void remove_from_queue (struct sem_array
* sma
,
354 struct sem_queue
* q
)
356 *(q
->prev
) = q
->next
;
358 q
->next
->prev
= q
->prev
;
359 else /* sma->sem_pending_last == &q->next */
360 sma
->sem_pending_last
= q
->prev
;
361 q
->prev
= NULL
; /* mark as removed */
365 * Determine whether a sequence of semaphore operations would succeed
366 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
369 static int try_atomic_semop (struct sem_array
* sma
, struct sembuf
* sops
,
370 int nsops
, struct sem_undo
*un
, int pid
)
376 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
377 curr
= sma
->sem_base
+ sop
->sem_num
;
378 sem_op
= sop
->sem_op
;
379 result
= curr
->semval
;
381 if (!sem_op
&& result
)
389 if (sop
->sem_flg
& SEM_UNDO
) {
390 int undo
= un
->semadj
[sop
->sem_num
] - sem_op
;
392 * Exceeding the undo range is an error.
394 if (undo
< (-SEMAEM
- 1) || undo
> SEMAEM
)
397 curr
->semval
= result
;
401 while (sop
>= sops
) {
402 sma
->sem_base
[sop
->sem_num
].sempid
= pid
;
403 if (sop
->sem_flg
& SEM_UNDO
)
404 un
->semadj
[sop
->sem_num
] -= sop
->sem_op
;
408 sma
->sem_otime
= get_seconds();
416 if (sop
->sem_flg
& IPC_NOWAIT
)
423 while (sop
>= sops
) {
424 sma
->sem_base
[sop
->sem_num
].semval
-= sop
->sem_op
;
431 /* Go through the pending queue for the indicated semaphore
432 * looking for tasks that can be completed.
434 static void update_queue (struct sem_array
* sma
)
437 struct sem_queue
* q
;
439 q
= sma
->sem_pending
;
441 error
= try_atomic_semop(sma
, q
->sops
, q
->nsops
,
444 /* Does q->sleeper still need to sleep? */
447 remove_from_queue(sma
,q
);
448 q
->status
= IN_WAKEUP
;
450 * Continue scanning. The next operation
451 * that must be checked depends on the type of the
452 * completed operation:
453 * - if the operation modified the array, then
454 * restart from the head of the queue and
455 * check for threads that might be waiting
456 * for semaphore values to become 0.
457 * - if the operation didn't modify the array,
458 * then just continue.
461 n
= sma
->sem_pending
;
464 wake_up_process(q
->sleeper
);
465 /* hands-off: q will disappear immediately after
477 /* The following counts are associated to each semaphore:
478 * semncnt number of tasks waiting on semval being nonzero
479 * semzcnt number of tasks waiting on semval being zero
480 * This model assumes that a task waits on exactly one semaphore.
481 * Since semaphore operations are to be performed atomically, tasks actually
482 * wait on a whole sequence of semaphores simultaneously.
483 * The counts we return here are a rough approximation, but still
484 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
486 static int count_semncnt (struct sem_array
* sma
, ushort semnum
)
489 struct sem_queue
* q
;
492 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
493 struct sembuf
* sops
= q
->sops
;
494 int nsops
= q
->nsops
;
496 for (i
= 0; i
< nsops
; i
++)
497 if (sops
[i
].sem_num
== semnum
498 && (sops
[i
].sem_op
< 0)
499 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
504 static int count_semzcnt (struct sem_array
* sma
, ushort semnum
)
507 struct sem_queue
* q
;
510 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
511 struct sembuf
* sops
= q
->sops
;
512 int nsops
= q
->nsops
;
514 for (i
= 0; i
< nsops
; i
++)
515 if (sops
[i
].sem_num
== semnum
516 && (sops
[i
].sem_op
== 0)
517 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
523 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
524 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
525 * remains locked on exit.
527 static void freeary(struct ipc_namespace
*ns
, struct kern_ipc_perm
*ipcp
)
531 struct sem_array
*sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
533 /* Invalidate the existing undo structures for this semaphore set.
534 * (They will be freed without any further action in exit_sem()
535 * or during the next semop.)
537 for (un
= sma
->undo
; un
; un
= un
->id_next
)
540 /* Wake up all pending processes and let them fail with EIDRM. */
541 q
= sma
->sem_pending
;
544 /* lazy remove_from_queue: we are killing the whole queue */
547 q
->status
= IN_WAKEUP
;
548 wake_up_process(q
->sleeper
); /* doesn't sleep */
550 q
->status
= -EIDRM
; /* hands-off q */
554 /* Remove the semaphore set from the IDR */
558 ns
->used_sems
-= sma
->sem_nsems
;
559 security_sem_free(sma
);
563 static unsigned long copy_semid_to_user(void __user
*buf
, struct semid64_ds
*in
, int version
)
567 return copy_to_user(buf
, in
, sizeof(*in
));
572 ipc64_perm_to_ipc_perm(&in
->sem_perm
, &out
.sem_perm
);
574 out
.sem_otime
= in
->sem_otime
;
575 out
.sem_ctime
= in
->sem_ctime
;
576 out
.sem_nsems
= in
->sem_nsems
;
578 return copy_to_user(buf
, &out
, sizeof(out
));
585 static int semctl_nolock(struct ipc_namespace
*ns
, int semid
,
586 int cmd
, int version
, union semun arg
)
589 struct sem_array
*sma
;
595 struct seminfo seminfo
;
598 err
= security_sem_semctl(NULL
, cmd
);
602 memset(&seminfo
,0,sizeof(seminfo
));
603 seminfo
.semmni
= ns
->sc_semmni
;
604 seminfo
.semmns
= ns
->sc_semmns
;
605 seminfo
.semmsl
= ns
->sc_semmsl
;
606 seminfo
.semopm
= ns
->sc_semopm
;
607 seminfo
.semvmx
= SEMVMX
;
608 seminfo
.semmnu
= SEMMNU
;
609 seminfo
.semmap
= SEMMAP
;
610 seminfo
.semume
= SEMUME
;
611 down_read(&sem_ids(ns
).rw_mutex
);
612 if (cmd
== SEM_INFO
) {
613 seminfo
.semusz
= sem_ids(ns
).in_use
;
614 seminfo
.semaem
= ns
->used_sems
;
616 seminfo
.semusz
= SEMUSZ
;
617 seminfo
.semaem
= SEMAEM
;
619 max_id
= ipc_get_maxid(&sem_ids(ns
));
620 up_read(&sem_ids(ns
).rw_mutex
);
621 if (copy_to_user (arg
.__buf
, &seminfo
, sizeof(struct seminfo
)))
623 return (max_id
< 0) ? 0: max_id
;
628 struct semid64_ds tbuf
;
631 if (cmd
== SEM_STAT
) {
632 sma
= sem_lock(ns
, semid
);
635 id
= sma
->sem_perm
.id
;
637 sma
= sem_lock_check(ns
, semid
);
644 if (ipcperms (&sma
->sem_perm
, S_IRUGO
))
647 err
= security_sem_semctl(sma
, cmd
);
651 memset(&tbuf
, 0, sizeof(tbuf
));
653 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
654 tbuf
.sem_otime
= sma
->sem_otime
;
655 tbuf
.sem_ctime
= sma
->sem_ctime
;
656 tbuf
.sem_nsems
= sma
->sem_nsems
;
658 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
671 static int semctl_main(struct ipc_namespace
*ns
, int semid
, int semnum
,
672 int cmd
, int version
, union semun arg
)
674 struct sem_array
*sma
;
677 ushort fast_sem_io
[SEMMSL_FAST
];
678 ushort
* sem_io
= fast_sem_io
;
681 sma
= sem_lock_check(ns
, semid
);
685 nsems
= sma
->sem_nsems
;
688 if (ipcperms (&sma
->sem_perm
, (cmd
==SETVAL
||cmd
==SETALL
)?S_IWUGO
:S_IRUGO
))
691 err
= security_sem_semctl(sma
, cmd
);
699 ushort __user
*array
= arg
.array
;
702 if(nsems
> SEMMSL_FAST
) {
706 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
708 ipc_lock_by_ptr(&sma
->sem_perm
);
714 ipc_lock_by_ptr(&sma
->sem_perm
);
716 if (sma
->sem_perm
.deleted
) {
723 for (i
= 0; i
< sma
->sem_nsems
; i
++)
724 sem_io
[i
] = sma
->sem_base
[i
].semval
;
727 if(copy_to_user(array
, sem_io
, nsems
*sizeof(ushort
)))
739 if(nsems
> SEMMSL_FAST
) {
740 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
742 ipc_lock_by_ptr(&sma
->sem_perm
);
749 if (copy_from_user (sem_io
, arg
.array
, nsems
*sizeof(ushort
))) {
750 ipc_lock_by_ptr(&sma
->sem_perm
);
757 for (i
= 0; i
< nsems
; i
++) {
758 if (sem_io
[i
] > SEMVMX
) {
759 ipc_lock_by_ptr(&sma
->sem_perm
);
766 ipc_lock_by_ptr(&sma
->sem_perm
);
768 if (sma
->sem_perm
.deleted
) {
774 for (i
= 0; i
< nsems
; i
++)
775 sma
->sem_base
[i
].semval
= sem_io
[i
];
776 for (un
= sma
->undo
; un
; un
= un
->id_next
)
777 for (i
= 0; i
< nsems
; i
++)
779 sma
->sem_ctime
= get_seconds();
780 /* maybe some queued-up processes were waiting for this */
785 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
788 if(semnum
< 0 || semnum
>= nsems
)
791 curr
= &sma
->sem_base
[semnum
];
801 err
= count_semncnt(sma
,semnum
);
804 err
= count_semzcnt(sma
,semnum
);
811 if (val
> SEMVMX
|| val
< 0)
814 for (un
= sma
->undo
; un
; un
= un
->id_next
)
815 un
->semadj
[semnum
] = 0;
817 curr
->sempid
= task_tgid_vnr(current
);
818 sma
->sem_ctime
= get_seconds();
819 /* maybe some queued-up processes were waiting for this */
828 if(sem_io
!= fast_sem_io
)
829 ipc_free(sem_io
, sizeof(ushort
)*nsems
);
839 static inline unsigned long copy_semid_from_user(struct sem_setbuf
*out
, void __user
*buf
, int version
)
844 struct semid64_ds tbuf
;
846 if(copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
849 out
->uid
= tbuf
.sem_perm
.uid
;
850 out
->gid
= tbuf
.sem_perm
.gid
;
851 out
->mode
= tbuf
.sem_perm
.mode
;
857 struct semid_ds tbuf_old
;
859 if(copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
862 out
->uid
= tbuf_old
.sem_perm
.uid
;
863 out
->gid
= tbuf_old
.sem_perm
.gid
;
864 out
->mode
= tbuf_old
.sem_perm
.mode
;
873 static int semctl_down(struct ipc_namespace
*ns
, int semid
, int semnum
,
874 int cmd
, int version
, union semun arg
)
876 struct sem_array
*sma
;
878 struct sem_setbuf
uninitialized_var(setbuf
);
879 struct kern_ipc_perm
*ipcp
;
882 if(copy_semid_from_user (&setbuf
, arg
.buf
, version
))
885 sma
= sem_lock_check_down(ns
, semid
);
889 ipcp
= &sma
->sem_perm
;
891 err
= audit_ipc_obj(ipcp
);
895 if (cmd
== IPC_SET
) {
896 err
= audit_ipc_set_perm(0, setbuf
.uid
, setbuf
.gid
, setbuf
.mode
);
900 if (current
->euid
!= ipcp
->cuid
&&
901 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
)) {
906 err
= security_sem_semctl(sma
, cmd
);
916 ipcp
->uid
= setbuf
.uid
;
917 ipcp
->gid
= setbuf
.gid
;
918 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
)
919 | (setbuf
.mode
& S_IRWXUGO
);
920 sma
->sem_ctime
= get_seconds();
936 asmlinkage
long sys_semctl (int semid
, int semnum
, int cmd
, union semun arg
)
940 struct ipc_namespace
*ns
;
945 version
= ipc_parse_version(&cmd
);
946 ns
= current
->nsproxy
->ipc_ns
;
953 err
= semctl_nolock(ns
, semid
, cmd
, version
, arg
);
962 err
= semctl_main(ns
,semid
,semnum
,cmd
,version
,arg
);
966 down_write(&sem_ids(ns
).rw_mutex
);
967 err
= semctl_down(ns
,semid
,semnum
,cmd
,version
,arg
);
968 up_write(&sem_ids(ns
).rw_mutex
);
975 /* If the task doesn't already have a undo_list, then allocate one
976 * here. We guarantee there is only one thread using this undo list,
977 * and current is THE ONE
979 * If this allocation and assignment succeeds, but later
980 * portions of this code fail, there is no need to free the sem_undo_list.
981 * Just let it stay associated with the task, and it'll be freed later
984 * This can block, so callers must hold no locks.
986 static inline int get_undo_list(struct sem_undo_list
**undo_listp
)
988 struct sem_undo_list
*undo_list
;
990 undo_list
= current
->sysvsem
.undo_list
;
992 undo_list
= kzalloc(sizeof(*undo_list
), GFP_KERNEL
);
993 if (undo_list
== NULL
)
995 spin_lock_init(&undo_list
->lock
);
996 atomic_set(&undo_list
->refcnt
, 1);
997 current
->sysvsem
.undo_list
= undo_list
;
999 *undo_listp
= undo_list
;
1003 static struct sem_undo
*lookup_undo(struct sem_undo_list
*ulp
, int semid
)
1005 struct sem_undo
**last
, *un
;
1007 last
= &ulp
->proc_list
;
1010 if(un
->semid
==semid
)
1013 *last
=un
->proc_next
;
1016 last
=&un
->proc_next
;
1023 static struct sem_undo
*find_undo(struct ipc_namespace
*ns
, int semid
)
1025 struct sem_array
*sma
;
1026 struct sem_undo_list
*ulp
;
1027 struct sem_undo
*un
, *new;
1031 error
= get_undo_list(&ulp
);
1033 return ERR_PTR(error
);
1035 spin_lock(&ulp
->lock
);
1036 un
= lookup_undo(ulp
, semid
);
1037 spin_unlock(&ulp
->lock
);
1038 if (likely(un
!=NULL
))
1041 /* no undo structure around - allocate one. */
1042 sma
= sem_lock_check(ns
, semid
);
1044 return ERR_PTR(PTR_ERR(sma
));
1046 nsems
= sma
->sem_nsems
;
1047 ipc_rcu_getref(sma
);
1050 new = kzalloc(sizeof(struct sem_undo
) + sizeof(short)*nsems
, GFP_KERNEL
);
1052 ipc_lock_by_ptr(&sma
->sem_perm
);
1053 ipc_rcu_putref(sma
);
1055 return ERR_PTR(-ENOMEM
);
1057 new->semadj
= (short *) &new[1];
1060 spin_lock(&ulp
->lock
);
1061 un
= lookup_undo(ulp
, semid
);
1063 spin_unlock(&ulp
->lock
);
1065 ipc_lock_by_ptr(&sma
->sem_perm
);
1066 ipc_rcu_putref(sma
);
1070 ipc_lock_by_ptr(&sma
->sem_perm
);
1071 ipc_rcu_putref(sma
);
1072 if (sma
->sem_perm
.deleted
) {
1074 spin_unlock(&ulp
->lock
);
1076 un
= ERR_PTR(-EIDRM
);
1079 new->proc_next
= ulp
->proc_list
;
1080 ulp
->proc_list
= new;
1081 new->id_next
= sma
->undo
;
1085 spin_unlock(&ulp
->lock
);
1090 asmlinkage
long sys_semtimedop(int semid
, struct sembuf __user
*tsops
,
1091 unsigned nsops
, const struct timespec __user
*timeout
)
1093 int error
= -EINVAL
;
1094 struct sem_array
*sma
;
1095 struct sembuf fast_sops
[SEMOPM_FAST
];
1096 struct sembuf
* sops
= fast_sops
, *sop
;
1097 struct sem_undo
*un
;
1098 int undos
= 0, alter
= 0, max
;
1099 struct sem_queue queue
;
1100 unsigned long jiffies_left
= 0;
1101 struct ipc_namespace
*ns
;
1103 ns
= current
->nsproxy
->ipc_ns
;
1105 if (nsops
< 1 || semid
< 0)
1107 if (nsops
> ns
->sc_semopm
)
1109 if(nsops
> SEMOPM_FAST
) {
1110 sops
= kmalloc(sizeof(*sops
)*nsops
,GFP_KERNEL
);
1114 if (copy_from_user (sops
, tsops
, nsops
* sizeof(*tsops
))) {
1119 struct timespec _timeout
;
1120 if (copy_from_user(&_timeout
, timeout
, sizeof(*timeout
))) {
1124 if (_timeout
.tv_sec
< 0 || _timeout
.tv_nsec
< 0 ||
1125 _timeout
.tv_nsec
>= 1000000000L) {
1129 jiffies_left
= timespec_to_jiffies(&_timeout
);
1132 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
1133 if (sop
->sem_num
>= max
)
1135 if (sop
->sem_flg
& SEM_UNDO
)
1137 if (sop
->sem_op
!= 0)
1143 un
= find_undo(ns
, semid
);
1145 error
= PTR_ERR(un
);
1151 sma
= sem_lock_check(ns
, semid
);
1153 error
= PTR_ERR(sma
);
1158 * semid identifiers are not unique - find_undo may have
1159 * allocated an undo structure, it was invalidated by an RMID
1160 * and now a new array with received the same id. Check and retry.
1162 if (un
&& un
->semid
== -1) {
1167 if (max
>= sma
->sem_nsems
)
1168 goto out_unlock_free
;
1171 if (ipcperms(&sma
->sem_perm
, alter
? S_IWUGO
: S_IRUGO
))
1172 goto out_unlock_free
;
1174 error
= security_sem_semop(sma
, sops
, nsops
, alter
);
1176 goto out_unlock_free
;
1178 error
= try_atomic_semop (sma
, sops
, nsops
, un
, task_tgid_vnr(current
));
1180 if (alter
&& error
== 0)
1182 goto out_unlock_free
;
1185 /* We need to sleep on this operation, so we put the current
1186 * task into the pending queue and go to sleep.
1191 queue
.nsops
= nsops
;
1193 queue
.pid
= task_tgid_vnr(current
);
1195 queue
.alter
= alter
;
1197 append_to_queue(sma
,&queue
);
1199 prepend_to_queue(sma
,&queue
);
1201 queue
.status
= -EINTR
;
1202 queue
.sleeper
= current
;
1203 current
->state
= TASK_INTERRUPTIBLE
;
1207 jiffies_left
= schedule_timeout(jiffies_left
);
1211 error
= queue
.status
;
1212 while(unlikely(error
== IN_WAKEUP
)) {
1214 error
= queue
.status
;
1217 if (error
!= -EINTR
) {
1218 /* fast path: update_queue already obtained all requested
1223 sma
= sem_lock(ns
, semid
);
1225 BUG_ON(queue
.prev
!= NULL
);
1231 * If queue.status != -EINTR we are woken up by another process
1233 error
= queue
.status
;
1234 if (error
!= -EINTR
) {
1235 goto out_unlock_free
;
1239 * If an interrupt occurred we have to clean up the queue
1241 if (timeout
&& jiffies_left
== 0)
1243 remove_from_queue(sma
,&queue
);
1244 goto out_unlock_free
;
1249 if(sops
!= fast_sops
)
1254 asmlinkage
long sys_semop (int semid
, struct sembuf __user
*tsops
, unsigned nsops
)
1256 return sys_semtimedop(semid
, tsops
, nsops
, NULL
);
1259 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1260 * parent and child tasks.
1263 int copy_semundo(unsigned long clone_flags
, struct task_struct
*tsk
)
1265 struct sem_undo_list
*undo_list
;
1268 if (clone_flags
& CLONE_SYSVSEM
) {
1269 error
= get_undo_list(&undo_list
);
1272 atomic_inc(&undo_list
->refcnt
);
1273 tsk
->sysvsem
.undo_list
= undo_list
;
1275 tsk
->sysvsem
.undo_list
= NULL
;
1281 * add semadj values to semaphores, free undo structures.
1282 * undo structures are not freed when semaphore arrays are destroyed
1283 * so some of them may be out of date.
1284 * IMPLEMENTATION NOTE: There is some confusion over whether the
1285 * set of adjustments that needs to be done should be done in an atomic
1286 * manner or not. That is, if we are attempting to decrement the semval
1287 * should we queue up and wait until we can do so legally?
1288 * The original implementation attempted to do this (queue and wait).
1289 * The current implementation does not do so. The POSIX standard
1290 * and SVID should be consulted to determine what behavior is mandated.
1292 void exit_sem(struct task_struct
*tsk
)
1294 struct sem_undo_list
*undo_list
;
1295 struct sem_undo
*u
, **up
;
1296 struct ipc_namespace
*ns
;
1298 undo_list
= tsk
->sysvsem
.undo_list
;
1302 if (!atomic_dec_and_test(&undo_list
->refcnt
))
1305 ns
= tsk
->nsproxy
->ipc_ns
;
1306 /* There's no need to hold the semundo list lock, as current
1307 * is the last task exiting for this undo list.
1309 for (up
= &undo_list
->proc_list
; (u
= *up
); *up
= u
->proc_next
, kfree(u
)) {
1310 struct sem_array
*sma
;
1312 struct sem_undo
*un
, **unp
;
1319 sma
= sem_lock(ns
, semid
);
1326 BUG_ON(sem_checkid(sma
, u
->semid
));
1328 /* remove u from the sma->undo list */
1329 for (unp
= &sma
->undo
; (un
= *unp
); unp
= &un
->id_next
) {
1333 printk ("exit_sem undo list error id=%d\n", u
->semid
);
1337 /* perform adjustments registered in u */
1338 nsems
= sma
->sem_nsems
;
1339 for (i
= 0; i
< nsems
; i
++) {
1340 struct sem
* semaphore
= &sma
->sem_base
[i
];
1342 semaphore
->semval
+= u
->semadj
[i
];
1344 * Range checks of the new semaphore value,
1345 * not defined by sus:
1346 * - Some unices ignore the undo entirely
1347 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1348 * - some cap the value (e.g. FreeBSD caps
1349 * at 0, but doesn't enforce SEMVMX)
1351 * Linux caps the semaphore value, both at 0
1354 * Manfred <manfred@colorfullife.com>
1356 if (semaphore
->semval
< 0)
1357 semaphore
->semval
= 0;
1358 if (semaphore
->semval
> SEMVMX
)
1359 semaphore
->semval
= SEMVMX
;
1360 semaphore
->sempid
= task_tgid_vnr(current
);
1363 sma
->sem_otime
= get_seconds();
1364 /* maybe some queued-up processes were waiting for this */
1372 #ifdef CONFIG_PROC_FS
1373 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
)
1375 struct sem_array
*sma
= it
;
1377 return seq_printf(s
,
1378 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",