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)
95 static int newary(struct ipc_namespace
*, struct ipc_params
*);
96 static void freeary(struct ipc_namespace
*, struct kern_ipc_perm
*);
98 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
);
101 #define SEMMSL_FAST 256 /* 512 bytes on stack */
102 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
105 * linked list protection:
107 * sem_array.sem_pending{,last},
108 * sem_array.sem_undo: sem_lock() for read/write
109 * sem_undo.proc_next: only "current" is allowed to read/write that field.
113 #define sc_semmsl sem_ctls[0]
114 #define sc_semmns sem_ctls[1]
115 #define sc_semopm sem_ctls[2]
116 #define sc_semmni sem_ctls[3]
118 void sem_init_ns(struct ipc_namespace
*ns
)
120 ns
->sc_semmsl
= SEMMSL
;
121 ns
->sc_semmns
= SEMMNS
;
122 ns
->sc_semopm
= SEMOPM
;
123 ns
->sc_semmni
= SEMMNI
;
125 ipc_init_ids(&ns
->ids
[IPC_SEM_IDS
]);
129 void sem_exit_ns(struct ipc_namespace
*ns
)
131 free_ipcs(ns
, &sem_ids(ns
), freeary
);
135 void __init
sem_init (void)
137 sem_init_ns(&init_ipc_ns
);
138 ipc_init_proc_interface("sysvipc/sem",
139 " key semid perms nsems uid gid cuid cgid otime ctime\n",
140 IPC_SEM_IDS
, sysvipc_sem_proc_show
);
144 * This routine is called in the paths where the rw_mutex is held to protect
145 * access to the idr tree.
147 static inline struct sem_array
*sem_lock_check_down(struct ipc_namespace
*ns
,
150 struct kern_ipc_perm
*ipcp
= ipc_lock_check_down(&sem_ids(ns
), id
);
153 return (struct sem_array
*)ipcp
;
155 return container_of(ipcp
, struct sem_array
, sem_perm
);
159 * sem_lock_(check_) routines are called in the paths where the rw_mutex
162 static inline struct sem_array
*sem_lock(struct ipc_namespace
*ns
, int id
)
164 struct kern_ipc_perm
*ipcp
= ipc_lock(&sem_ids(ns
), id
);
167 return (struct sem_array
*)ipcp
;
169 return container_of(ipcp
, struct sem_array
, sem_perm
);
172 static inline struct sem_array
*sem_lock_check(struct ipc_namespace
*ns
,
175 struct kern_ipc_perm
*ipcp
= ipc_lock_check(&sem_ids(ns
), id
);
178 return (struct sem_array
*)ipcp
;
180 return container_of(ipcp
, struct sem_array
, sem_perm
);
183 static inline void sem_rmid(struct ipc_namespace
*ns
, struct sem_array
*s
)
185 ipc_rmid(&sem_ids(ns
), &s
->sem_perm
);
189 * Lockless wakeup algorithm:
190 * Without the check/retry algorithm a lockless wakeup is possible:
191 * - queue.status is initialized to -EINTR before blocking.
192 * - wakeup is performed by
193 * * unlinking the queue entry from sma->sem_pending
194 * * setting queue.status to IN_WAKEUP
195 * This is the notification for the blocked thread that a
196 * result value is imminent.
197 * * call wake_up_process
198 * * set queue.status to the final value.
199 * - the previously blocked thread checks queue.status:
200 * * if it's IN_WAKEUP, then it must wait until the value changes
201 * * if it's not -EINTR, then the operation was completed by
202 * update_queue. semtimedop can return queue.status without
203 * performing any operation on the sem array.
204 * * otherwise it must acquire the spinlock and check what's up.
206 * The two-stage algorithm is necessary to protect against the following
208 * - if queue.status is set after wake_up_process, then the woken up idle
209 * thread could race forward and try (and fail) to acquire sma->lock
210 * before update_queue had a chance to set queue.status
211 * - if queue.status is written before wake_up_process and if the
212 * blocked process is woken up by a signal between writing
213 * queue.status and the wake_up_process, then the woken up
214 * process could return from semtimedop and die by calling
215 * sys_exit before wake_up_process is called. Then wake_up_process
216 * will oops, because the task structure is already invalid.
217 * (yes, this happened on s390 with sysv msg).
223 * newary - Create a new semaphore set
225 * @params: ptr to the structure that contains key, semflg and nsems
227 * Called with sem_ids.rw_mutex held (as a writer)
230 static int newary(struct ipc_namespace
*ns
, struct ipc_params
*params
)
234 struct sem_array
*sma
;
236 key_t key
= params
->key
;
237 int nsems
= params
->u
.nsems
;
238 int semflg
= params
->flg
;
242 if (ns
->used_sems
+ nsems
> ns
->sc_semmns
)
245 size
= sizeof (*sma
) + nsems
* sizeof (struct sem
);
246 sma
= ipc_rcu_alloc(size
);
250 memset (sma
, 0, size
);
252 sma
->sem_perm
.mode
= (semflg
& S_IRWXUGO
);
253 sma
->sem_perm
.key
= key
;
255 sma
->sem_perm
.security
= NULL
;
256 retval
= security_sem_alloc(sma
);
262 id
= ipc_addid(&sem_ids(ns
), &sma
->sem_perm
, ns
->sc_semmni
);
264 security_sem_free(sma
);
268 ns
->used_sems
+= nsems
;
270 sma
->sem_base
= (struct sem
*) &sma
[1];
271 /* sma->sem_pending = NULL; */
272 sma
->sem_pending_last
= &sma
->sem_pending
;
273 /* sma->undo = NULL; */
274 sma
->sem_nsems
= nsems
;
275 sma
->sem_ctime
= get_seconds();
278 return sma
->sem_perm
.id
;
283 * Called with sem_ids.rw_mutex and ipcp locked.
285 static inline int sem_security(struct kern_ipc_perm
*ipcp
, int semflg
)
287 struct sem_array
*sma
;
289 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
290 return security_sem_associate(sma
, semflg
);
294 * Called with sem_ids.rw_mutex and ipcp locked.
296 static inline int sem_more_checks(struct kern_ipc_perm
*ipcp
,
297 struct ipc_params
*params
)
299 struct sem_array
*sma
;
301 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
302 if (params
->u
.nsems
> sma
->sem_nsems
)
308 asmlinkage
long sys_semget(key_t key
, int nsems
, int semflg
)
310 struct ipc_namespace
*ns
;
311 struct ipc_ops sem_ops
;
312 struct ipc_params sem_params
;
314 ns
= current
->nsproxy
->ipc_ns
;
316 if (nsems
< 0 || nsems
> ns
->sc_semmsl
)
319 sem_ops
.getnew
= newary
;
320 sem_ops
.associate
= sem_security
;
321 sem_ops
.more_checks
= sem_more_checks
;
323 sem_params
.key
= key
;
324 sem_params
.flg
= semflg
;
325 sem_params
.u
.nsems
= nsems
;
327 return ipcget(ns
, &sem_ids(ns
), &sem_ops
, &sem_params
);
330 /* Manage the doubly linked list sma->sem_pending as a FIFO:
331 * insert new queue elements at the tail sma->sem_pending_last.
333 static inline void append_to_queue (struct sem_array
* sma
,
334 struct sem_queue
* q
)
336 *(q
->prev
= sma
->sem_pending_last
) = q
;
337 *(sma
->sem_pending_last
= &q
->next
) = NULL
;
340 static inline void prepend_to_queue (struct sem_array
* sma
,
341 struct sem_queue
* q
)
343 q
->next
= sma
->sem_pending
;
344 *(q
->prev
= &sma
->sem_pending
) = q
;
346 q
->next
->prev
= &q
->next
;
347 else /* sma->sem_pending_last == &sma->sem_pending */
348 sma
->sem_pending_last
= &q
->next
;
351 static inline void remove_from_queue (struct sem_array
* sma
,
352 struct sem_queue
* q
)
354 *(q
->prev
) = q
->next
;
356 q
->next
->prev
= q
->prev
;
357 else /* sma->sem_pending_last == &q->next */
358 sma
->sem_pending_last
= q
->prev
;
359 q
->prev
= NULL
; /* mark as removed */
363 * Determine whether a sequence of semaphore operations would succeed
364 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
367 static int try_atomic_semop (struct sem_array
* sma
, struct sembuf
* sops
,
368 int nsops
, struct sem_undo
*un
, int pid
)
374 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
375 curr
= sma
->sem_base
+ sop
->sem_num
;
376 sem_op
= sop
->sem_op
;
377 result
= curr
->semval
;
379 if (!sem_op
&& result
)
387 if (sop
->sem_flg
& SEM_UNDO
) {
388 int undo
= un
->semadj
[sop
->sem_num
] - sem_op
;
390 * Exceeding the undo range is an error.
392 if (undo
< (-SEMAEM
- 1) || undo
> SEMAEM
)
395 curr
->semval
= result
;
399 while (sop
>= sops
) {
400 sma
->sem_base
[sop
->sem_num
].sempid
= pid
;
401 if (sop
->sem_flg
& SEM_UNDO
)
402 un
->semadj
[sop
->sem_num
] -= sop
->sem_op
;
406 sma
->sem_otime
= get_seconds();
414 if (sop
->sem_flg
& IPC_NOWAIT
)
421 while (sop
>= sops
) {
422 sma
->sem_base
[sop
->sem_num
].semval
-= sop
->sem_op
;
429 /* Go through the pending queue for the indicated semaphore
430 * looking for tasks that can be completed.
432 static void update_queue (struct sem_array
* sma
)
435 struct sem_queue
* q
;
437 q
= sma
->sem_pending
;
439 error
= try_atomic_semop(sma
, q
->sops
, q
->nsops
,
442 /* Does q->sleeper still need to sleep? */
445 remove_from_queue(sma
,q
);
446 q
->status
= IN_WAKEUP
;
448 * Continue scanning. The next operation
449 * that must be checked depends on the type of the
450 * completed operation:
451 * - if the operation modified the array, then
452 * restart from the head of the queue and
453 * check for threads that might be waiting
454 * for semaphore values to become 0.
455 * - if the operation didn't modify the array,
456 * then just continue.
459 n
= sma
->sem_pending
;
462 wake_up_process(q
->sleeper
);
463 /* hands-off: q will disappear immediately after
475 /* The following counts are associated to each semaphore:
476 * semncnt number of tasks waiting on semval being nonzero
477 * semzcnt number of tasks waiting on semval being zero
478 * This model assumes that a task waits on exactly one semaphore.
479 * Since semaphore operations are to be performed atomically, tasks actually
480 * wait on a whole sequence of semaphores simultaneously.
481 * The counts we return here are a rough approximation, but still
482 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
484 static int count_semncnt (struct sem_array
* sma
, ushort semnum
)
487 struct sem_queue
* q
;
490 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
491 struct sembuf
* sops
= q
->sops
;
492 int nsops
= q
->nsops
;
494 for (i
= 0; i
< nsops
; i
++)
495 if (sops
[i
].sem_num
== semnum
496 && (sops
[i
].sem_op
< 0)
497 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
502 static int count_semzcnt (struct sem_array
* sma
, ushort semnum
)
505 struct sem_queue
* q
;
508 for (q
= sma
->sem_pending
; q
; q
= q
->next
) {
509 struct sembuf
* sops
= q
->sops
;
510 int nsops
= q
->nsops
;
512 for (i
= 0; i
< nsops
; i
++)
513 if (sops
[i
].sem_num
== semnum
514 && (sops
[i
].sem_op
== 0)
515 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
521 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
522 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
523 * remains locked on exit.
525 static void freeary(struct ipc_namespace
*ns
, struct kern_ipc_perm
*ipcp
)
529 struct sem_array
*sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
531 /* Invalidate the existing undo structures for this semaphore set.
532 * (They will be freed without any further action in exit_sem()
533 * or during the next semop.)
535 for (un
= sma
->undo
; un
; un
= un
->id_next
)
538 /* Wake up all pending processes and let them fail with EIDRM. */
539 q
= sma
->sem_pending
;
542 /* lazy remove_from_queue: we are killing the whole queue */
545 q
->status
= IN_WAKEUP
;
546 wake_up_process(q
->sleeper
); /* doesn't sleep */
548 q
->status
= -EIDRM
; /* hands-off q */
552 /* Remove the semaphore set from the IDR */
556 ns
->used_sems
-= sma
->sem_nsems
;
557 security_sem_free(sma
);
561 static unsigned long copy_semid_to_user(void __user
*buf
, struct semid64_ds
*in
, int version
)
565 return copy_to_user(buf
, in
, sizeof(*in
));
570 ipc64_perm_to_ipc_perm(&in
->sem_perm
, &out
.sem_perm
);
572 out
.sem_otime
= in
->sem_otime
;
573 out
.sem_ctime
= in
->sem_ctime
;
574 out
.sem_nsems
= in
->sem_nsems
;
576 return copy_to_user(buf
, &out
, sizeof(out
));
583 static int semctl_nolock(struct ipc_namespace
*ns
, int semid
,
584 int cmd
, int version
, union semun arg
)
587 struct sem_array
*sma
;
593 struct seminfo seminfo
;
596 err
= security_sem_semctl(NULL
, cmd
);
600 memset(&seminfo
,0,sizeof(seminfo
));
601 seminfo
.semmni
= ns
->sc_semmni
;
602 seminfo
.semmns
= ns
->sc_semmns
;
603 seminfo
.semmsl
= ns
->sc_semmsl
;
604 seminfo
.semopm
= ns
->sc_semopm
;
605 seminfo
.semvmx
= SEMVMX
;
606 seminfo
.semmnu
= SEMMNU
;
607 seminfo
.semmap
= SEMMAP
;
608 seminfo
.semume
= SEMUME
;
609 down_read(&sem_ids(ns
).rw_mutex
);
610 if (cmd
== SEM_INFO
) {
611 seminfo
.semusz
= sem_ids(ns
).in_use
;
612 seminfo
.semaem
= ns
->used_sems
;
614 seminfo
.semusz
= SEMUSZ
;
615 seminfo
.semaem
= SEMAEM
;
617 max_id
= ipc_get_maxid(&sem_ids(ns
));
618 up_read(&sem_ids(ns
).rw_mutex
);
619 if (copy_to_user (arg
.__buf
, &seminfo
, sizeof(struct seminfo
)))
621 return (max_id
< 0) ? 0: max_id
;
626 struct semid64_ds tbuf
;
629 if (cmd
== SEM_STAT
) {
630 sma
= sem_lock(ns
, semid
);
633 id
= sma
->sem_perm
.id
;
635 sma
= sem_lock_check(ns
, semid
);
642 if (ipcperms (&sma
->sem_perm
, S_IRUGO
))
645 err
= security_sem_semctl(sma
, cmd
);
649 memset(&tbuf
, 0, sizeof(tbuf
));
651 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
652 tbuf
.sem_otime
= sma
->sem_otime
;
653 tbuf
.sem_ctime
= sma
->sem_ctime
;
654 tbuf
.sem_nsems
= sma
->sem_nsems
;
656 if (copy_semid_to_user (arg
.buf
, &tbuf
, version
))
669 static int semctl_main(struct ipc_namespace
*ns
, int semid
, int semnum
,
670 int cmd
, int version
, union semun arg
)
672 struct sem_array
*sma
;
675 ushort fast_sem_io
[SEMMSL_FAST
];
676 ushort
* sem_io
= fast_sem_io
;
679 sma
= sem_lock_check(ns
, semid
);
683 nsems
= sma
->sem_nsems
;
686 if (ipcperms (&sma
->sem_perm
, (cmd
==SETVAL
||cmd
==SETALL
)?S_IWUGO
:S_IRUGO
))
689 err
= security_sem_semctl(sma
, cmd
);
697 ushort __user
*array
= arg
.array
;
700 if(nsems
> SEMMSL_FAST
) {
704 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
706 ipc_lock_by_ptr(&sma
->sem_perm
);
712 ipc_lock_by_ptr(&sma
->sem_perm
);
714 if (sma
->sem_perm
.deleted
) {
721 for (i
= 0; i
< sma
->sem_nsems
; i
++)
722 sem_io
[i
] = sma
->sem_base
[i
].semval
;
725 if(copy_to_user(array
, sem_io
, nsems
*sizeof(ushort
)))
737 if(nsems
> SEMMSL_FAST
) {
738 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
740 ipc_lock_by_ptr(&sma
->sem_perm
);
747 if (copy_from_user (sem_io
, arg
.array
, nsems
*sizeof(ushort
))) {
748 ipc_lock_by_ptr(&sma
->sem_perm
);
755 for (i
= 0; i
< nsems
; i
++) {
756 if (sem_io
[i
] > SEMVMX
) {
757 ipc_lock_by_ptr(&sma
->sem_perm
);
764 ipc_lock_by_ptr(&sma
->sem_perm
);
766 if (sma
->sem_perm
.deleted
) {
772 for (i
= 0; i
< nsems
; i
++)
773 sma
->sem_base
[i
].semval
= sem_io
[i
];
774 for (un
= sma
->undo
; un
; un
= un
->id_next
)
775 for (i
= 0; i
< nsems
; i
++)
777 sma
->sem_ctime
= get_seconds();
778 /* maybe some queued-up processes were waiting for this */
783 /* GETVAL, GETPID, GETNCTN, GETZCNT, SETVAL: fall-through */
786 if(semnum
< 0 || semnum
>= nsems
)
789 curr
= &sma
->sem_base
[semnum
];
799 err
= count_semncnt(sma
,semnum
);
802 err
= count_semzcnt(sma
,semnum
);
809 if (val
> SEMVMX
|| val
< 0)
812 for (un
= sma
->undo
; un
; un
= un
->id_next
)
813 un
->semadj
[semnum
] = 0;
815 curr
->sempid
= task_tgid_vnr(current
);
816 sma
->sem_ctime
= get_seconds();
817 /* maybe some queued-up processes were waiting for this */
826 if(sem_io
!= fast_sem_io
)
827 ipc_free(sem_io
, sizeof(ushort
)*nsems
);
837 static inline unsigned long copy_semid_from_user(struct sem_setbuf
*out
, void __user
*buf
, int version
)
842 struct semid64_ds tbuf
;
844 if(copy_from_user(&tbuf
, buf
, sizeof(tbuf
)))
847 out
->uid
= tbuf
.sem_perm
.uid
;
848 out
->gid
= tbuf
.sem_perm
.gid
;
849 out
->mode
= tbuf
.sem_perm
.mode
;
855 struct semid_ds tbuf_old
;
857 if(copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
860 out
->uid
= tbuf_old
.sem_perm
.uid
;
861 out
->gid
= tbuf_old
.sem_perm
.gid
;
862 out
->mode
= tbuf_old
.sem_perm
.mode
;
871 static int semctl_down(struct ipc_namespace
*ns
, int semid
, int semnum
,
872 int cmd
, int version
, union semun arg
)
874 struct sem_array
*sma
;
876 struct sem_setbuf
uninitialized_var(setbuf
);
877 struct kern_ipc_perm
*ipcp
;
880 if(copy_semid_from_user (&setbuf
, arg
.buf
, version
))
883 sma
= sem_lock_check_down(ns
, semid
);
887 ipcp
= &sma
->sem_perm
;
889 err
= audit_ipc_obj(ipcp
);
893 if (cmd
== IPC_SET
) {
894 err
= audit_ipc_set_perm(0, setbuf
.uid
, setbuf
.gid
, setbuf
.mode
);
898 if (current
->euid
!= ipcp
->cuid
&&
899 current
->euid
!= ipcp
->uid
&& !capable(CAP_SYS_ADMIN
)) {
904 err
= security_sem_semctl(sma
, cmd
);
914 ipcp
->uid
= setbuf
.uid
;
915 ipcp
->gid
= setbuf
.gid
;
916 ipcp
->mode
= (ipcp
->mode
& ~S_IRWXUGO
)
917 | (setbuf
.mode
& S_IRWXUGO
);
918 sma
->sem_ctime
= get_seconds();
934 asmlinkage
long sys_semctl (int semid
, int semnum
, int cmd
, union semun arg
)
938 struct ipc_namespace
*ns
;
943 version
= ipc_parse_version(&cmd
);
944 ns
= current
->nsproxy
->ipc_ns
;
951 err
= semctl_nolock(ns
, semid
, cmd
, version
, arg
);
960 err
= semctl_main(ns
,semid
,semnum
,cmd
,version
,arg
);
964 down_write(&sem_ids(ns
).rw_mutex
);
965 err
= semctl_down(ns
,semid
,semnum
,cmd
,version
,arg
);
966 up_write(&sem_ids(ns
).rw_mutex
);
973 /* If the task doesn't already have a undo_list, then allocate one
974 * here. We guarantee there is only one thread using this undo list,
975 * and current is THE ONE
977 * If this allocation and assignment succeeds, but later
978 * portions of this code fail, there is no need to free the sem_undo_list.
979 * Just let it stay associated with the task, and it'll be freed later
982 * This can block, so callers must hold no locks.
984 static inline int get_undo_list(struct sem_undo_list
**undo_listp
)
986 struct sem_undo_list
*undo_list
;
988 undo_list
= current
->sysvsem
.undo_list
;
990 undo_list
= kzalloc(sizeof(*undo_list
), GFP_KERNEL
);
991 if (undo_list
== NULL
)
993 spin_lock_init(&undo_list
->lock
);
994 atomic_set(&undo_list
->refcnt
, 1);
995 current
->sysvsem
.undo_list
= undo_list
;
997 *undo_listp
= undo_list
;
1001 static struct sem_undo
*lookup_undo(struct sem_undo_list
*ulp
, int semid
)
1003 struct sem_undo
**last
, *un
;
1005 last
= &ulp
->proc_list
;
1008 if(un
->semid
==semid
)
1011 *last
=un
->proc_next
;
1014 last
=&un
->proc_next
;
1021 static struct sem_undo
*find_undo(struct ipc_namespace
*ns
, int semid
)
1023 struct sem_array
*sma
;
1024 struct sem_undo_list
*ulp
;
1025 struct sem_undo
*un
, *new;
1029 error
= get_undo_list(&ulp
);
1031 return ERR_PTR(error
);
1033 spin_lock(&ulp
->lock
);
1034 un
= lookup_undo(ulp
, semid
);
1035 spin_unlock(&ulp
->lock
);
1036 if (likely(un
!=NULL
))
1039 /* no undo structure around - allocate one. */
1040 sma
= sem_lock_check(ns
, semid
);
1042 return ERR_PTR(PTR_ERR(sma
));
1044 nsems
= sma
->sem_nsems
;
1045 ipc_rcu_getref(sma
);
1048 new = kzalloc(sizeof(struct sem_undo
) + sizeof(short)*nsems
, GFP_KERNEL
);
1050 ipc_lock_by_ptr(&sma
->sem_perm
);
1051 ipc_rcu_putref(sma
);
1053 return ERR_PTR(-ENOMEM
);
1055 new->semadj
= (short *) &new[1];
1058 spin_lock(&ulp
->lock
);
1059 un
= lookup_undo(ulp
, semid
);
1061 spin_unlock(&ulp
->lock
);
1063 ipc_lock_by_ptr(&sma
->sem_perm
);
1064 ipc_rcu_putref(sma
);
1068 ipc_lock_by_ptr(&sma
->sem_perm
);
1069 ipc_rcu_putref(sma
);
1070 if (sma
->sem_perm
.deleted
) {
1072 spin_unlock(&ulp
->lock
);
1074 un
= ERR_PTR(-EIDRM
);
1077 new->proc_next
= ulp
->proc_list
;
1078 ulp
->proc_list
= new;
1079 new->id_next
= sma
->undo
;
1083 spin_unlock(&ulp
->lock
);
1088 asmlinkage
long sys_semtimedop(int semid
, struct sembuf __user
*tsops
,
1089 unsigned nsops
, const struct timespec __user
*timeout
)
1091 int error
= -EINVAL
;
1092 struct sem_array
*sma
;
1093 struct sembuf fast_sops
[SEMOPM_FAST
];
1094 struct sembuf
* sops
= fast_sops
, *sop
;
1095 struct sem_undo
*un
;
1096 int undos
= 0, alter
= 0, max
;
1097 struct sem_queue queue
;
1098 unsigned long jiffies_left
= 0;
1099 struct ipc_namespace
*ns
;
1101 ns
= current
->nsproxy
->ipc_ns
;
1103 if (nsops
< 1 || semid
< 0)
1105 if (nsops
> ns
->sc_semopm
)
1107 if(nsops
> SEMOPM_FAST
) {
1108 sops
= kmalloc(sizeof(*sops
)*nsops
,GFP_KERNEL
);
1112 if (copy_from_user (sops
, tsops
, nsops
* sizeof(*tsops
))) {
1117 struct timespec _timeout
;
1118 if (copy_from_user(&_timeout
, timeout
, sizeof(*timeout
))) {
1122 if (_timeout
.tv_sec
< 0 || _timeout
.tv_nsec
< 0 ||
1123 _timeout
.tv_nsec
>= 1000000000L) {
1127 jiffies_left
= timespec_to_jiffies(&_timeout
);
1130 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
1131 if (sop
->sem_num
>= max
)
1133 if (sop
->sem_flg
& SEM_UNDO
)
1135 if (sop
->sem_op
!= 0)
1141 un
= find_undo(ns
, semid
);
1143 error
= PTR_ERR(un
);
1149 sma
= sem_lock_check(ns
, semid
);
1151 error
= PTR_ERR(sma
);
1156 * semid identifiers are not unique - find_undo may have
1157 * allocated an undo structure, it was invalidated by an RMID
1158 * and now a new array with received the same id. Check and retry.
1160 if (un
&& un
->semid
== -1) {
1165 if (max
>= sma
->sem_nsems
)
1166 goto out_unlock_free
;
1169 if (ipcperms(&sma
->sem_perm
, alter
? S_IWUGO
: S_IRUGO
))
1170 goto out_unlock_free
;
1172 error
= security_sem_semop(sma
, sops
, nsops
, alter
);
1174 goto out_unlock_free
;
1176 error
= try_atomic_semop (sma
, sops
, nsops
, un
, task_tgid_vnr(current
));
1178 if (alter
&& error
== 0)
1180 goto out_unlock_free
;
1183 /* We need to sleep on this operation, so we put the current
1184 * task into the pending queue and go to sleep.
1189 queue
.nsops
= nsops
;
1191 queue
.pid
= task_tgid_vnr(current
);
1193 queue
.alter
= alter
;
1195 append_to_queue(sma
,&queue
);
1197 prepend_to_queue(sma
,&queue
);
1199 queue
.status
= -EINTR
;
1200 queue
.sleeper
= current
;
1201 current
->state
= TASK_INTERRUPTIBLE
;
1205 jiffies_left
= schedule_timeout(jiffies_left
);
1209 error
= queue
.status
;
1210 while(unlikely(error
== IN_WAKEUP
)) {
1212 error
= queue
.status
;
1215 if (error
!= -EINTR
) {
1216 /* fast path: update_queue already obtained all requested
1221 sma
= sem_lock(ns
, semid
);
1223 BUG_ON(queue
.prev
!= NULL
);
1229 * If queue.status != -EINTR we are woken up by another process
1231 error
= queue
.status
;
1232 if (error
!= -EINTR
) {
1233 goto out_unlock_free
;
1237 * If an interrupt occurred we have to clean up the queue
1239 if (timeout
&& jiffies_left
== 0)
1241 remove_from_queue(sma
,&queue
);
1242 goto out_unlock_free
;
1247 if(sops
!= fast_sops
)
1252 asmlinkage
long sys_semop (int semid
, struct sembuf __user
*tsops
, unsigned nsops
)
1254 return sys_semtimedop(semid
, tsops
, nsops
, NULL
);
1257 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1258 * parent and child tasks.
1261 int copy_semundo(unsigned long clone_flags
, struct task_struct
*tsk
)
1263 struct sem_undo_list
*undo_list
;
1266 if (clone_flags
& CLONE_SYSVSEM
) {
1267 error
= get_undo_list(&undo_list
);
1270 atomic_inc(&undo_list
->refcnt
);
1271 tsk
->sysvsem
.undo_list
= undo_list
;
1273 tsk
->sysvsem
.undo_list
= NULL
;
1279 * add semadj values to semaphores, free undo structures.
1280 * undo structures are not freed when semaphore arrays are destroyed
1281 * so some of them may be out of date.
1282 * IMPLEMENTATION NOTE: There is some confusion over whether the
1283 * set of adjustments that needs to be done should be done in an atomic
1284 * manner or not. That is, if we are attempting to decrement the semval
1285 * should we queue up and wait until we can do so legally?
1286 * The original implementation attempted to do this (queue and wait).
1287 * The current implementation does not do so. The POSIX standard
1288 * and SVID should be consulted to determine what behavior is mandated.
1290 void exit_sem(struct task_struct
*tsk
)
1292 struct sem_undo_list
*undo_list
;
1293 struct sem_undo
*u
, **up
;
1294 struct ipc_namespace
*ns
;
1296 undo_list
= tsk
->sysvsem
.undo_list
;
1300 if (!atomic_dec_and_test(&undo_list
->refcnt
))
1303 ns
= tsk
->nsproxy
->ipc_ns
;
1304 /* There's no need to hold the semundo list lock, as current
1305 * is the last task exiting for this undo list.
1307 for (up
= &undo_list
->proc_list
; (u
= *up
); *up
= u
->proc_next
, kfree(u
)) {
1308 struct sem_array
*sma
;
1310 struct sem_undo
*un
, **unp
;
1317 sma
= sem_lock(ns
, semid
);
1324 BUG_ON(sem_checkid(sma
, u
->semid
));
1326 /* remove u from the sma->undo list */
1327 for (unp
= &sma
->undo
; (un
= *unp
); unp
= &un
->id_next
) {
1331 printk ("exit_sem undo list error id=%d\n", u
->semid
);
1335 /* perform adjustments registered in u */
1336 nsems
= sma
->sem_nsems
;
1337 for (i
= 0; i
< nsems
; i
++) {
1338 struct sem
* semaphore
= &sma
->sem_base
[i
];
1340 semaphore
->semval
+= u
->semadj
[i
];
1342 * Range checks of the new semaphore value,
1343 * not defined by sus:
1344 * - Some unices ignore the undo entirely
1345 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1346 * - some cap the value (e.g. FreeBSD caps
1347 * at 0, but doesn't enforce SEMVMX)
1349 * Linux caps the semaphore value, both at 0
1352 * Manfred <manfred@colorfullife.com>
1354 if (semaphore
->semval
< 0)
1355 semaphore
->semval
= 0;
1356 if (semaphore
->semval
> SEMVMX
)
1357 semaphore
->semval
= SEMVMX
;
1358 semaphore
->sempid
= task_tgid_vnr(current
);
1361 sma
->sem_otime
= get_seconds();
1362 /* maybe some queued-up processes were waiting for this */
1370 #ifdef CONFIG_PROC_FS
1371 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
)
1373 struct sem_array
*sma
= it
;
1375 return seq_printf(s
,
1376 "%10d %10d %4o %10lu %5u %5u %5u %5u %10lu %10lu\n",
This page took 0.065847 seconds and 5 git commands to generate.