3 * Copyright (C) 1992 Krishna Balasubramanian
4 * Copyright (C) 1995 Eric Schenk, Bruno Haible
6 * /proc/sysvipc/sem support (c) 1999 Dragos Acostachioaie <dragos@iname.com>
8 * SMP-threaded, sysctl's added
9 * (c) 1999 Manfred Spraul <manfred@colorfullife.com>
10 * Enforced range limit on SEM_UNDO
11 * (c) 2001 Red Hat Inc
13 * (c) 2003 Manfred Spraul <manfred@colorfullife.com>
14 * Further wakeup optimizations, documentation
15 * (c) 2010 Manfred Spraul <manfred@colorfullife.com>
17 * support for audit of ipc object properties and permission changes
18 * Dustin Kirkland <dustin.kirkland@us.ibm.com>
22 * Pavel Emelianov <xemul@openvz.org>
24 * Implementation notes: (May 2010)
25 * This file implements System V semaphores.
27 * User space visible behavior:
28 * - FIFO ordering for semop() operations (just FIFO, not starvation
30 * - multiple semaphore operations that alter the same semaphore in
31 * one semop() are handled.
32 * - sem_ctime (time of last semctl()) is updated in the IPC_SET, SETVAL and
34 * - two Linux specific semctl() commands: SEM_STAT, SEM_INFO.
35 * - undo adjustments at process exit are limited to 0..SEMVMX.
36 * - namespace are supported.
37 * - SEMMSL, SEMMNS, SEMOPM and SEMMNI can be configured at runtine by writing
38 * to /proc/sys/kernel/sem.
39 * - statistics about the usage are reported in /proc/sysvipc/sem.
43 * - all global variables are read-mostly.
44 * - semop() calls and semctl(RMID) are synchronized by RCU.
45 * - most operations do write operations (actually: spin_lock calls) to
46 * the per-semaphore array structure.
47 * Thus: Perfect SMP scaling between independent semaphore arrays.
48 * If multiple semaphores in one array are used, then cache line
49 * trashing on the semaphore array spinlock will limit the scaling.
50 * - semncnt and semzcnt are calculated on demand in count_semncnt() and
52 * - the task that performs a successful semop() scans the list of all
53 * sleeping tasks and completes any pending operations that can be fulfilled.
54 * Semaphores are actively given to waiting tasks (necessary for FIFO).
55 * (see update_queue())
56 * - To improve the scalability, the actual wake-up calls are performed after
57 * dropping all locks. (see wake_up_sem_queue_prepare(),
58 * wake_up_sem_queue_do())
59 * - All work is done by the waker, the woken up task does not have to do
60 * anything - not even acquiring a lock or dropping a refcount.
61 * - A woken up task may not even touch the semaphore array anymore, it may
62 * have been destroyed already by a semctl(RMID).
63 * - The synchronizations between wake-ups due to a timeout/signal and a
64 * wake-up due to a completed semaphore operation is achieved by using an
65 * intermediate state (IN_WAKEUP).
66 * - UNDO values are stored in an array (one per process and per
67 * semaphore array, lazily allocated). For backwards compatibility, multiple
68 * modes for the UNDO variables are supported (per process, per thread)
69 * (see copy_semundo, CLONE_SYSVSEM)
70 * - There are two lists of the pending operations: a per-array list
71 * and per-semaphore list (stored in the array). This allows to achieve FIFO
72 * ordering without always scanning all pending operations.
73 * The worst-case behavior is nevertheless O(N^2) for N wakeups.
76 #include <linux/slab.h>
77 #include <linux/spinlock.h>
78 #include <linux/init.h>
79 #include <linux/proc_fs.h>
80 #include <linux/time.h>
81 #include <linux/security.h>
82 #include <linux/syscalls.h>
83 #include <linux/audit.h>
84 #include <linux/capability.h>
85 #include <linux/seq_file.h>
86 #include <linux/rwsem.h>
87 #include <linux/nsproxy.h>
88 #include <linux/ipc_namespace.h>
90 #include <asm/uaccess.h>
93 /* One semaphore structure for each semaphore in the system. */
95 int semval
; /* current value */
96 int sempid
; /* pid of last operation */
97 spinlock_t lock
; /* spinlock for fine-grained semtimedop */
98 struct list_head sem_pending
; /* pending single-sop operations */
101 /* One queue for each sleeping process in the system. */
103 struct list_head list
; /* queue of pending operations */
104 struct task_struct
*sleeper
; /* this process */
105 struct sem_undo
*undo
; /* undo structure */
106 int pid
; /* process id of requesting process */
107 int status
; /* completion status of operation */
108 struct sembuf
*sops
; /* array of pending operations */
109 int nsops
; /* number of operations */
110 int alter
; /* does *sops alter the array? */
113 /* Each task has a list of undo requests. They are executed automatically
114 * when the process exits.
117 struct list_head list_proc
; /* per-process list: *
118 * all undos from one process
120 struct rcu_head rcu
; /* rcu struct for sem_undo */
121 struct sem_undo_list
*ulp
; /* back ptr to sem_undo_list */
122 struct list_head list_id
; /* per semaphore array list:
123 * all undos for one array */
124 int semid
; /* semaphore set identifier */
125 short *semadj
; /* array of adjustments */
126 /* one per semaphore */
129 /* sem_undo_list controls shared access to the list of sem_undo structures
130 * that may be shared among all a CLONE_SYSVSEM task group.
132 struct sem_undo_list
{
135 struct list_head list_proc
;
139 #define sem_ids(ns) ((ns)->ids[IPC_SEM_IDS])
141 #define sem_checkid(sma, semid) ipc_checkid(&sma->sem_perm, semid)
143 static int newary(struct ipc_namespace
*, struct ipc_params
*);
144 static void freeary(struct ipc_namespace
*, struct kern_ipc_perm
*);
145 #ifdef CONFIG_PROC_FS
146 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
);
149 #define SEMMSL_FAST 256 /* 512 bytes on stack */
150 #define SEMOPM_FAST 64 /* ~ 372 bytes on stack */
153 * linked list protection:
155 * sem_array.sem_pending{,last},
156 * sem_array.sem_undo: sem_lock() for read/write
157 * sem_undo.proc_next: only "current" is allowed to read/write that field.
161 #define sc_semmsl sem_ctls[0]
162 #define sc_semmns sem_ctls[1]
163 #define sc_semopm sem_ctls[2]
164 #define sc_semmni sem_ctls[3]
166 void sem_init_ns(struct ipc_namespace
*ns
)
168 ns
->sc_semmsl
= SEMMSL
;
169 ns
->sc_semmns
= SEMMNS
;
170 ns
->sc_semopm
= SEMOPM
;
171 ns
->sc_semmni
= SEMMNI
;
173 ipc_init_ids(&ns
->ids
[IPC_SEM_IDS
]);
177 void sem_exit_ns(struct ipc_namespace
*ns
)
179 free_ipcs(ns
, &sem_ids(ns
), freeary
);
180 idr_destroy(&ns
->ids
[IPC_SEM_IDS
].ipcs_idr
);
184 void __init
sem_init (void)
186 sem_init_ns(&init_ipc_ns
);
187 ipc_init_proc_interface("sysvipc/sem",
188 " key semid perms nsems uid gid cuid cgid otime ctime\n",
189 IPC_SEM_IDS
, sysvipc_sem_proc_show
);
193 * If the request contains only one semaphore operation, and there are
194 * no complex transactions pending, lock only the semaphore involved.
195 * Otherwise, lock the entire semaphore array, since we either have
196 * multiple semaphores in our own semops, or we need to look at
197 * semaphores from other pending complex operations.
199 * Carefully guard against sma->complex_count changing between zero
200 * and non-zero while we are spinning for the lock. The value of
201 * sma->complex_count cannot change while we are holding the lock,
202 * so sem_unlock should be fine.
204 * The global lock path checks that all the local locks have been released,
205 * checking each local lock once. This means that the local lock paths
206 * cannot start their critical sections while the global lock is held.
208 static inline int sem_lock(struct sem_array
*sma
, struct sembuf
*sops
,
213 if (nsops
== 1 && !sma
->complex_count
) {
214 struct sem
*sem
= sma
->sem_base
+ sops
->sem_num
;
216 /* Lock just the semaphore we are interested in. */
217 spin_lock(&sem
->lock
);
220 * If sma->complex_count was set while we were spinning,
221 * we may need to look at things we did not lock here.
223 if (unlikely(sma
->complex_count
)) {
224 spin_unlock(&sem
->lock
);
229 * Another process is holding the global lock on the
230 * sem_array; we cannot enter our critical section,
231 * but have to wait for the global lock to be released.
233 if (unlikely(spin_is_locked(&sma
->sem_perm
.lock
))) {
234 spin_unlock(&sem
->lock
);
235 spin_unlock_wait(&sma
->sem_perm
.lock
);
239 locknum
= sops
->sem_num
;
243 * Lock the semaphore array, and wait for all of the
244 * individual semaphore locks to go away. The code
245 * above ensures no new single-lock holders will enter
246 * their critical section while the array lock is held.
249 spin_lock(&sma
->sem_perm
.lock
);
250 for (i
= 0; i
< sma
->sem_nsems
; i
++) {
251 struct sem
*sem
= sma
->sem_base
+ i
;
252 spin_unlock_wait(&sem
->lock
);
259 static inline void sem_unlock(struct sem_array
*sma
, int locknum
)
262 spin_unlock(&sma
->sem_perm
.lock
);
264 struct sem
*sem
= sma
->sem_base
+ locknum
;
265 spin_unlock(&sem
->lock
);
271 * sem_lock_(check_) routines are called in the paths where the rw_mutex
274 static inline struct sem_array
*sem_obtain_lock(struct ipc_namespace
*ns
,
275 int id
, struct sembuf
*sops
, int nsops
, int *locknum
)
277 struct kern_ipc_perm
*ipcp
;
278 struct sem_array
*sma
;
281 ipcp
= ipc_obtain_object(&sem_ids(ns
), id
);
283 sma
= ERR_CAST(ipcp
);
287 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
288 *locknum
= sem_lock(sma
, sops
, nsops
);
290 /* ipc_rmid() may have already freed the ID while sem_lock
291 * was spinning: verify that the structure is still valid
294 return container_of(ipcp
, struct sem_array
, sem_perm
);
296 sem_unlock(sma
, *locknum
);
297 sma
= ERR_PTR(-EINVAL
);
303 static inline struct sem_array
*sem_obtain_object(struct ipc_namespace
*ns
, int id
)
305 struct kern_ipc_perm
*ipcp
= ipc_obtain_object(&sem_ids(ns
), id
);
308 return ERR_CAST(ipcp
);
310 return container_of(ipcp
, struct sem_array
, sem_perm
);
313 static inline struct sem_array
*sem_obtain_object_check(struct ipc_namespace
*ns
,
316 struct kern_ipc_perm
*ipcp
= ipc_obtain_object_check(&sem_ids(ns
), id
);
319 return ERR_CAST(ipcp
);
321 return container_of(ipcp
, struct sem_array
, sem_perm
);
324 static inline void sem_lock_and_putref(struct sem_array
*sma
)
327 sem_lock(sma
, NULL
, -1);
331 static inline void sem_putref(struct sem_array
*sma
)
333 sem_lock_and_putref(sma
);
337 static inline void sem_rmid(struct ipc_namespace
*ns
, struct sem_array
*s
)
339 ipc_rmid(&sem_ids(ns
), &s
->sem_perm
);
343 * Lockless wakeup algorithm:
344 * Without the check/retry algorithm a lockless wakeup is possible:
345 * - queue.status is initialized to -EINTR before blocking.
346 * - wakeup is performed by
347 * * unlinking the queue entry from sma->sem_pending
348 * * setting queue.status to IN_WAKEUP
349 * This is the notification for the blocked thread that a
350 * result value is imminent.
351 * * call wake_up_process
352 * * set queue.status to the final value.
353 * - the previously blocked thread checks queue.status:
354 * * if it's IN_WAKEUP, then it must wait until the value changes
355 * * if it's not -EINTR, then the operation was completed by
356 * update_queue. semtimedop can return queue.status without
357 * performing any operation on the sem array.
358 * * otherwise it must acquire the spinlock and check what's up.
360 * The two-stage algorithm is necessary to protect against the following
362 * - if queue.status is set after wake_up_process, then the woken up idle
363 * thread could race forward and try (and fail) to acquire sma->lock
364 * before update_queue had a chance to set queue.status
365 * - if queue.status is written before wake_up_process and if the
366 * blocked process is woken up by a signal between writing
367 * queue.status and the wake_up_process, then the woken up
368 * process could return from semtimedop and die by calling
369 * sys_exit before wake_up_process is called. Then wake_up_process
370 * will oops, because the task structure is already invalid.
371 * (yes, this happened on s390 with sysv msg).
377 * newary - Create a new semaphore set
379 * @params: ptr to the structure that contains key, semflg and nsems
381 * Called with sem_ids.rw_mutex held (as a writer)
384 static int newary(struct ipc_namespace
*ns
, struct ipc_params
*params
)
388 struct sem_array
*sma
;
390 key_t key
= params
->key
;
391 int nsems
= params
->u
.nsems
;
392 int semflg
= params
->flg
;
397 if (ns
->used_sems
+ nsems
> ns
->sc_semmns
)
400 size
= sizeof (*sma
) + nsems
* sizeof (struct sem
);
401 sma
= ipc_rcu_alloc(size
);
405 memset (sma
, 0, size
);
407 sma
->sem_perm
.mode
= (semflg
& S_IRWXUGO
);
408 sma
->sem_perm
.key
= key
;
410 sma
->sem_perm
.security
= NULL
;
411 retval
= security_sem_alloc(sma
);
417 id
= ipc_addid(&sem_ids(ns
), &sma
->sem_perm
, ns
->sc_semmni
);
419 security_sem_free(sma
);
423 ns
->used_sems
+= nsems
;
425 sma
->sem_base
= (struct sem
*) &sma
[1];
427 for (i
= 0; i
< nsems
; i
++) {
428 INIT_LIST_HEAD(&sma
->sem_base
[i
].sem_pending
);
429 spin_lock_init(&sma
->sem_base
[i
].lock
);
432 sma
->complex_count
= 0;
433 INIT_LIST_HEAD(&sma
->sem_pending
);
434 INIT_LIST_HEAD(&sma
->list_id
);
435 sma
->sem_nsems
= nsems
;
436 sma
->sem_ctime
= get_seconds();
439 return sma
->sem_perm
.id
;
444 * Called with sem_ids.rw_mutex and ipcp locked.
446 static inline int sem_security(struct kern_ipc_perm
*ipcp
, int semflg
)
448 struct sem_array
*sma
;
450 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
451 return security_sem_associate(sma
, semflg
);
455 * Called with sem_ids.rw_mutex and ipcp locked.
457 static inline int sem_more_checks(struct kern_ipc_perm
*ipcp
,
458 struct ipc_params
*params
)
460 struct sem_array
*sma
;
462 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
463 if (params
->u
.nsems
> sma
->sem_nsems
)
469 SYSCALL_DEFINE3(semget
, key_t
, key
, int, nsems
, int, semflg
)
471 struct ipc_namespace
*ns
;
472 struct ipc_ops sem_ops
;
473 struct ipc_params sem_params
;
475 ns
= current
->nsproxy
->ipc_ns
;
477 if (nsems
< 0 || nsems
> ns
->sc_semmsl
)
480 sem_ops
.getnew
= newary
;
481 sem_ops
.associate
= sem_security
;
482 sem_ops
.more_checks
= sem_more_checks
;
484 sem_params
.key
= key
;
485 sem_params
.flg
= semflg
;
486 sem_params
.u
.nsems
= nsems
;
488 return ipcget(ns
, &sem_ids(ns
), &sem_ops
, &sem_params
);
492 * Determine whether a sequence of semaphore operations would succeed
493 * all at once. Return 0 if yes, 1 if need to sleep, else return error code.
496 static int try_atomic_semop (struct sem_array
* sma
, struct sembuf
* sops
,
497 int nsops
, struct sem_undo
*un
, int pid
)
503 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
504 curr
= sma
->sem_base
+ sop
->sem_num
;
505 sem_op
= sop
->sem_op
;
506 result
= curr
->semval
;
508 if (!sem_op
&& result
)
516 if (sop
->sem_flg
& SEM_UNDO
) {
517 int undo
= un
->semadj
[sop
->sem_num
] - sem_op
;
519 * Exceeding the undo range is an error.
521 if (undo
< (-SEMAEM
- 1) || undo
> SEMAEM
)
524 curr
->semval
= result
;
528 while (sop
>= sops
) {
529 sma
->sem_base
[sop
->sem_num
].sempid
= pid
;
530 if (sop
->sem_flg
& SEM_UNDO
)
531 un
->semadj
[sop
->sem_num
] -= sop
->sem_op
;
542 if (sop
->sem_flg
& IPC_NOWAIT
)
549 while (sop
>= sops
) {
550 sma
->sem_base
[sop
->sem_num
].semval
-= sop
->sem_op
;
557 /** wake_up_sem_queue_prepare(q, error): Prepare wake-up
558 * @q: queue entry that must be signaled
559 * @error: Error value for the signal
561 * Prepare the wake-up of the queue entry q.
563 static void wake_up_sem_queue_prepare(struct list_head
*pt
,
564 struct sem_queue
*q
, int error
)
566 if (list_empty(pt
)) {
568 * Hold preempt off so that we don't get preempted and have the
569 * wakee busy-wait until we're scheduled back on.
573 q
->status
= IN_WAKEUP
;
576 list_add_tail(&q
->list
, pt
);
580 * wake_up_sem_queue_do(pt) - do the actual wake-up
581 * @pt: list of tasks to be woken up
583 * Do the actual wake-up.
584 * The function is called without any locks held, thus the semaphore array
585 * could be destroyed already and the tasks can disappear as soon as the
586 * status is set to the actual return code.
588 static void wake_up_sem_queue_do(struct list_head
*pt
)
590 struct sem_queue
*q
, *t
;
593 did_something
= !list_empty(pt
);
594 list_for_each_entry_safe(q
, t
, pt
, list
) {
595 wake_up_process(q
->sleeper
);
596 /* q can disappear immediately after writing q->status. */
604 static void unlink_queue(struct sem_array
*sma
, struct sem_queue
*q
)
608 sma
->complex_count
--;
611 /** check_restart(sma, q)
612 * @sma: semaphore array
613 * @q: the operation that just completed
615 * update_queue is O(N^2) when it restarts scanning the whole queue of
616 * waiting operations. Therefore this function checks if the restart is
617 * really necessary. It is called after a previously waiting operation
620 static int check_restart(struct sem_array
*sma
, struct sem_queue
*q
)
625 /* if the operation didn't modify the array, then no restart */
629 /* pending complex operations are too difficult to analyse */
630 if (sma
->complex_count
)
633 /* we were a sleeping complex operation. Too difficult */
637 curr
= sma
->sem_base
+ q
->sops
[0].sem_num
;
639 /* No-one waits on this queue */
640 if (list_empty(&curr
->sem_pending
))
643 /* the new semaphore value */
645 /* It is impossible that someone waits for the new value:
646 * - q is a previously sleeping simple operation that
647 * altered the array. It must be a decrement, because
648 * simple increments never sleep.
649 * - The value is not 0, thus wait-for-zero won't proceed.
650 * - If there are older (higher priority) decrements
651 * in the queue, then they have observed the original
652 * semval value and couldn't proceed. The operation
653 * decremented to value - thus they won't proceed either.
655 BUG_ON(q
->sops
[0].sem_op
>= 0);
659 * semval is 0. Check if there are wait-for-zero semops.
660 * They must be the first entries in the per-semaphore queue
662 h
= list_first_entry(&curr
->sem_pending
, struct sem_queue
, list
);
663 BUG_ON(h
->nsops
!= 1);
664 BUG_ON(h
->sops
[0].sem_num
!= q
->sops
[0].sem_num
);
666 /* Yes, there is a wait-for-zero semop. Restart */
667 if (h
->sops
[0].sem_op
== 0)
670 /* Again - no-one is waiting for the new value. */
676 * update_queue(sma, semnum): Look for tasks that can be completed.
677 * @sma: semaphore array.
678 * @semnum: semaphore that was modified.
679 * @pt: list head for the tasks that must be woken up.
681 * update_queue must be called after a semaphore in a semaphore array
682 * was modified. If multiple semaphores were modified, update_queue must
683 * be called with semnum = -1, as well as with the number of each modified
685 * The tasks that must be woken up are added to @pt. The return code
686 * is stored in q->pid.
687 * The function return 1 if at least one semop was completed successfully.
689 static int update_queue(struct sem_array
*sma
, int semnum
, struct list_head
*pt
)
692 struct list_head
*walk
;
693 struct list_head
*pending_list
;
694 int semop_completed
= 0;
697 pending_list
= &sma
->sem_pending
;
699 pending_list
= &sma
->sem_base
[semnum
].sem_pending
;
702 walk
= pending_list
->next
;
703 while (walk
!= pending_list
) {
706 q
= container_of(walk
, struct sem_queue
, list
);
709 /* If we are scanning the single sop, per-semaphore list of
710 * one semaphore and that semaphore is 0, then it is not
711 * necessary to scan the "alter" entries: simple increments
712 * that affect only one entry succeed immediately and cannot
713 * be in the per semaphore pending queue, and decrements
714 * cannot be successful if the value is already 0.
716 if (semnum
!= -1 && sma
->sem_base
[semnum
].semval
== 0 &&
720 error
= try_atomic_semop(sma
, q
->sops
, q
->nsops
,
723 /* Does q->sleeper still need to sleep? */
727 unlink_queue(sma
, q
);
733 restart
= check_restart(sma
, q
);
736 wake_up_sem_queue_prepare(pt
, q
, error
);
740 return semop_completed
;
744 * do_smart_update(sma, sops, nsops, otime, pt) - optimized update_queue
745 * @sma: semaphore array
746 * @sops: operations that were performed
747 * @nsops: number of operations
748 * @otime: force setting otime
749 * @pt: list head of the tasks that must be woken up.
751 * do_smart_update() does the required called to update_queue, based on the
752 * actual changes that were performed on the semaphore array.
753 * Note that the function does not do the actual wake-up: the caller is
754 * responsible for calling wake_up_sem_queue_do(@pt).
755 * It is safe to perform this call after dropping all locks.
757 static void do_smart_update(struct sem_array
*sma
, struct sembuf
*sops
, int nsops
,
758 int otime
, struct list_head
*pt
)
762 if (sma
->complex_count
|| sops
== NULL
) {
763 if (update_queue(sma
, -1, pt
))
768 /* No semops; something special is going on. */
769 for (i
= 0; i
< sma
->sem_nsems
; i
++) {
770 if (update_queue(sma
, i
, pt
))
776 /* Check the semaphores that were modified. */
777 for (i
= 0; i
< nsops
; i
++) {
778 if (sops
[i
].sem_op
> 0 ||
779 (sops
[i
].sem_op
< 0 &&
780 sma
->sem_base
[sops
[i
].sem_num
].semval
== 0))
781 if (update_queue(sma
, sops
[i
].sem_num
, pt
))
786 sma
->sem_otime
= get_seconds();
790 /* The following counts are associated to each semaphore:
791 * semncnt number of tasks waiting on semval being nonzero
792 * semzcnt number of tasks waiting on semval being zero
793 * This model assumes that a task waits on exactly one semaphore.
794 * Since semaphore operations are to be performed atomically, tasks actually
795 * wait on a whole sequence of semaphores simultaneously.
796 * The counts we return here are a rough approximation, but still
797 * warrant that semncnt+semzcnt>0 if the task is on the pending queue.
799 static int count_semncnt (struct sem_array
* sma
, ushort semnum
)
802 struct sem_queue
* q
;
805 list_for_each_entry(q
, &sma
->sem_pending
, list
) {
806 struct sembuf
* sops
= q
->sops
;
807 int nsops
= q
->nsops
;
809 for (i
= 0; i
< nsops
; i
++)
810 if (sops
[i
].sem_num
== semnum
811 && (sops
[i
].sem_op
< 0)
812 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
818 static int count_semzcnt (struct sem_array
* sma
, ushort semnum
)
821 struct sem_queue
* q
;
824 list_for_each_entry(q
, &sma
->sem_pending
, list
) {
825 struct sembuf
* sops
= q
->sops
;
826 int nsops
= q
->nsops
;
828 for (i
= 0; i
< nsops
; i
++)
829 if (sops
[i
].sem_num
== semnum
830 && (sops
[i
].sem_op
== 0)
831 && !(sops
[i
].sem_flg
& IPC_NOWAIT
))
837 /* Free a semaphore set. freeary() is called with sem_ids.rw_mutex locked
838 * as a writer and the spinlock for this semaphore set hold. sem_ids.rw_mutex
839 * remains locked on exit.
841 static void freeary(struct ipc_namespace
*ns
, struct kern_ipc_perm
*ipcp
)
843 struct sem_undo
*un
, *tu
;
844 struct sem_queue
*q
, *tq
;
845 struct sem_array
*sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
846 struct list_head tasks
;
849 /* Free the existing undo structures for this semaphore set. */
850 assert_spin_locked(&sma
->sem_perm
.lock
);
851 list_for_each_entry_safe(un
, tu
, &sma
->list_id
, list_id
) {
852 list_del(&un
->list_id
);
853 spin_lock(&un
->ulp
->lock
);
855 list_del_rcu(&un
->list_proc
);
856 spin_unlock(&un
->ulp
->lock
);
860 /* Wake up all pending processes and let them fail with EIDRM. */
861 INIT_LIST_HEAD(&tasks
);
862 list_for_each_entry_safe(q
, tq
, &sma
->sem_pending
, list
) {
863 unlink_queue(sma
, q
);
864 wake_up_sem_queue_prepare(&tasks
, q
, -EIDRM
);
866 for (i
= 0; i
< sma
->sem_nsems
; i
++) {
867 struct sem
*sem
= sma
->sem_base
+ i
;
868 list_for_each_entry_safe(q
, tq
, &sem
->sem_pending
, list
) {
869 unlink_queue(sma
, q
);
870 wake_up_sem_queue_prepare(&tasks
, q
, -EIDRM
);
874 /* Remove the semaphore set from the IDR */
878 wake_up_sem_queue_do(&tasks
);
879 ns
->used_sems
-= sma
->sem_nsems
;
880 security_sem_free(sma
);
884 static unsigned long copy_semid_to_user(void __user
*buf
, struct semid64_ds
*in
, int version
)
888 return copy_to_user(buf
, in
, sizeof(*in
));
893 memset(&out
, 0, sizeof(out
));
895 ipc64_perm_to_ipc_perm(&in
->sem_perm
, &out
.sem_perm
);
897 out
.sem_otime
= in
->sem_otime
;
898 out
.sem_ctime
= in
->sem_ctime
;
899 out
.sem_nsems
= in
->sem_nsems
;
901 return copy_to_user(buf
, &out
, sizeof(out
));
908 static int semctl_nolock(struct ipc_namespace
*ns
, int semid
,
909 int cmd
, int version
, void __user
*p
)
912 struct sem_array
*sma
;
918 struct seminfo seminfo
;
921 err
= security_sem_semctl(NULL
, cmd
);
925 memset(&seminfo
,0,sizeof(seminfo
));
926 seminfo
.semmni
= ns
->sc_semmni
;
927 seminfo
.semmns
= ns
->sc_semmns
;
928 seminfo
.semmsl
= ns
->sc_semmsl
;
929 seminfo
.semopm
= ns
->sc_semopm
;
930 seminfo
.semvmx
= SEMVMX
;
931 seminfo
.semmnu
= SEMMNU
;
932 seminfo
.semmap
= SEMMAP
;
933 seminfo
.semume
= SEMUME
;
934 down_read(&sem_ids(ns
).rw_mutex
);
935 if (cmd
== SEM_INFO
) {
936 seminfo
.semusz
= sem_ids(ns
).in_use
;
937 seminfo
.semaem
= ns
->used_sems
;
939 seminfo
.semusz
= SEMUSZ
;
940 seminfo
.semaem
= SEMAEM
;
942 max_id
= ipc_get_maxid(&sem_ids(ns
));
943 up_read(&sem_ids(ns
).rw_mutex
);
944 if (copy_to_user(p
, &seminfo
, sizeof(struct seminfo
)))
946 return (max_id
< 0) ? 0: max_id
;
951 struct semid64_ds tbuf
;
954 memset(&tbuf
, 0, sizeof(tbuf
));
956 if (cmd
== SEM_STAT
) {
958 sma
= sem_obtain_object(ns
, semid
);
963 id
= sma
->sem_perm
.id
;
966 sma
= sem_obtain_object_check(ns
, semid
);
974 if (ipcperms(ns
, &sma
->sem_perm
, S_IRUGO
))
977 err
= security_sem_semctl(sma
, cmd
);
981 kernel_to_ipc64_perm(&sma
->sem_perm
, &tbuf
.sem_perm
);
982 tbuf
.sem_otime
= sma
->sem_otime
;
983 tbuf
.sem_ctime
= sma
->sem_ctime
;
984 tbuf
.sem_nsems
= sma
->sem_nsems
;
986 if (copy_semid_to_user(p
, &tbuf
, version
))
998 static int semctl_setval(struct ipc_namespace
*ns
, int semid
, int semnum
,
1001 struct sem_undo
*un
;
1002 struct sem_array
*sma
;
1005 struct list_head tasks
;
1007 #if defined(CONFIG_64BIT) && defined(__BIG_ENDIAN)
1008 /* big-endian 64bit */
1011 /* 32bit or little-endian 64bit */
1015 if (val
> SEMVMX
|| val
< 0)
1018 INIT_LIST_HEAD(&tasks
);
1021 sma
= sem_obtain_object_check(ns
, semid
);
1024 return PTR_ERR(sma
);
1027 if (semnum
< 0 || semnum
>= sma
->sem_nsems
) {
1033 if (ipcperms(ns
, &sma
->sem_perm
, S_IWUGO
)) {
1038 err
= security_sem_semctl(sma
, SETVAL
);
1044 sem_lock(sma
, NULL
, -1);
1046 curr
= &sma
->sem_base
[semnum
];
1048 assert_spin_locked(&sma
->sem_perm
.lock
);
1049 list_for_each_entry(un
, &sma
->list_id
, list_id
)
1050 un
->semadj
[semnum
] = 0;
1053 curr
->sempid
= task_tgid_vnr(current
);
1054 sma
->sem_ctime
= get_seconds();
1055 /* maybe some queued-up processes were waiting for this */
1056 do_smart_update(sma
, NULL
, 0, 0, &tasks
);
1057 sem_unlock(sma
, -1);
1058 wake_up_sem_queue_do(&tasks
);
1062 static int semctl_main(struct ipc_namespace
*ns
, int semid
, int semnum
,
1063 int cmd
, void __user
*p
)
1065 struct sem_array
*sma
;
1068 ushort fast_sem_io
[SEMMSL_FAST
];
1069 ushort
* sem_io
= fast_sem_io
;
1070 struct list_head tasks
;
1072 INIT_LIST_HEAD(&tasks
);
1075 sma
= sem_obtain_object_check(ns
, semid
);
1078 return PTR_ERR(sma
);
1081 nsems
= sma
->sem_nsems
;
1084 if (ipcperms(ns
, &sma
->sem_perm
,
1085 cmd
== SETALL
? S_IWUGO
: S_IRUGO
)) {
1090 err
= security_sem_semctl(sma
, cmd
);
1100 ushort __user
*array
= p
;
1103 sem_lock(sma
, NULL
, -1);
1104 if(nsems
> SEMMSL_FAST
) {
1105 if (!ipc_rcu_getref(sma
)) {
1106 sem_unlock(sma
, -1);
1110 sem_unlock(sma
, -1);
1111 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
1112 if(sem_io
== NULL
) {
1117 sem_lock_and_putref(sma
);
1118 if (sma
->sem_perm
.deleted
) {
1119 sem_unlock(sma
, -1);
1124 for (i
= 0; i
< sma
->sem_nsems
; i
++)
1125 sem_io
[i
] = sma
->sem_base
[i
].semval
;
1126 sem_unlock(sma
, -1);
1128 if(copy_to_user(array
, sem_io
, nsems
*sizeof(ushort
)))
1135 struct sem_undo
*un
;
1137 if (!ipc_rcu_getref(sma
)) {
1143 if(nsems
> SEMMSL_FAST
) {
1144 sem_io
= ipc_alloc(sizeof(ushort
)*nsems
);
1145 if(sem_io
== NULL
) {
1151 if (copy_from_user (sem_io
, p
, nsems
*sizeof(ushort
))) {
1157 for (i
= 0; i
< nsems
; i
++) {
1158 if (sem_io
[i
] > SEMVMX
) {
1164 sem_lock_and_putref(sma
);
1165 if (sma
->sem_perm
.deleted
) {
1166 sem_unlock(sma
, -1);
1171 for (i
= 0; i
< nsems
; i
++)
1172 sma
->sem_base
[i
].semval
= sem_io
[i
];
1174 assert_spin_locked(&sma
->sem_perm
.lock
);
1175 list_for_each_entry(un
, &sma
->list_id
, list_id
) {
1176 for (i
= 0; i
< nsems
; i
++)
1179 sma
->sem_ctime
= get_seconds();
1180 /* maybe some queued-up processes were waiting for this */
1181 do_smart_update(sma
, NULL
, 0, 0, &tasks
);
1185 /* GETVAL, GETPID, GETNCTN, GETZCNT: fall-through */
1188 if (semnum
< 0 || semnum
>= nsems
) {
1193 sem_lock(sma
, NULL
, -1);
1194 curr
= &sma
->sem_base
[semnum
];
1204 err
= count_semncnt(sma
,semnum
);
1207 err
= count_semzcnt(sma
,semnum
);
1212 sem_unlock(sma
, -1);
1214 wake_up_sem_queue_do(&tasks
);
1216 if(sem_io
!= fast_sem_io
)
1217 ipc_free(sem_io
, sizeof(ushort
)*nsems
);
1221 static inline unsigned long
1222 copy_semid_from_user(struct semid64_ds
*out
, void __user
*buf
, int version
)
1226 if (copy_from_user(out
, buf
, sizeof(*out
)))
1231 struct semid_ds tbuf_old
;
1233 if(copy_from_user(&tbuf_old
, buf
, sizeof(tbuf_old
)))
1236 out
->sem_perm
.uid
= tbuf_old
.sem_perm
.uid
;
1237 out
->sem_perm
.gid
= tbuf_old
.sem_perm
.gid
;
1238 out
->sem_perm
.mode
= tbuf_old
.sem_perm
.mode
;
1248 * This function handles some semctl commands which require the rw_mutex
1249 * to be held in write mode.
1250 * NOTE: no locks must be held, the rw_mutex is taken inside this function.
1252 static int semctl_down(struct ipc_namespace
*ns
, int semid
,
1253 int cmd
, int version
, void __user
*p
)
1255 struct sem_array
*sma
;
1257 struct semid64_ds semid64
;
1258 struct kern_ipc_perm
*ipcp
;
1260 if(cmd
== IPC_SET
) {
1261 if (copy_semid_from_user(&semid64
, p
, version
))
1265 ipcp
= ipcctl_pre_down_nolock(ns
, &sem_ids(ns
), semid
, cmd
,
1266 &semid64
.sem_perm
, 0);
1268 return PTR_ERR(ipcp
);
1270 sma
= container_of(ipcp
, struct sem_array
, sem_perm
);
1272 err
= security_sem_semctl(sma
, cmd
);
1280 sem_lock(sma
, NULL
, -1);
1284 sem_lock(sma
, NULL
, -1);
1285 err
= ipc_update_perm(&semid64
.sem_perm
, ipcp
);
1288 sma
->sem_ctime
= get_seconds();
1297 sem_unlock(sma
, -1);
1299 up_write(&sem_ids(ns
).rw_mutex
);
1303 SYSCALL_DEFINE4(semctl
, int, semid
, int, semnum
, int, cmd
, unsigned long, arg
)
1306 struct ipc_namespace
*ns
;
1307 void __user
*p
= (void __user
*)arg
;
1312 version
= ipc_parse_version(&cmd
);
1313 ns
= current
->nsproxy
->ipc_ns
;
1320 return semctl_nolock(ns
, semid
, cmd
, version
, p
);
1327 return semctl_main(ns
, semid
, semnum
, cmd
, p
);
1329 return semctl_setval(ns
, semid
, semnum
, arg
);
1332 return semctl_down(ns
, semid
, cmd
, version
, p
);
1338 /* If the task doesn't already have a undo_list, then allocate one
1339 * here. We guarantee there is only one thread using this undo list,
1340 * and current is THE ONE
1342 * If this allocation and assignment succeeds, but later
1343 * portions of this code fail, there is no need to free the sem_undo_list.
1344 * Just let it stay associated with the task, and it'll be freed later
1347 * This can block, so callers must hold no locks.
1349 static inline int get_undo_list(struct sem_undo_list
**undo_listp
)
1351 struct sem_undo_list
*undo_list
;
1353 undo_list
= current
->sysvsem
.undo_list
;
1355 undo_list
= kzalloc(sizeof(*undo_list
), GFP_KERNEL
);
1356 if (undo_list
== NULL
)
1358 spin_lock_init(&undo_list
->lock
);
1359 atomic_set(&undo_list
->refcnt
, 1);
1360 INIT_LIST_HEAD(&undo_list
->list_proc
);
1362 current
->sysvsem
.undo_list
= undo_list
;
1364 *undo_listp
= undo_list
;
1368 static struct sem_undo
*__lookup_undo(struct sem_undo_list
*ulp
, int semid
)
1370 struct sem_undo
*un
;
1372 list_for_each_entry_rcu(un
, &ulp
->list_proc
, list_proc
) {
1373 if (un
->semid
== semid
)
1379 static struct sem_undo
*lookup_undo(struct sem_undo_list
*ulp
, int semid
)
1381 struct sem_undo
*un
;
1383 assert_spin_locked(&ulp
->lock
);
1385 un
= __lookup_undo(ulp
, semid
);
1387 list_del_rcu(&un
->list_proc
);
1388 list_add_rcu(&un
->list_proc
, &ulp
->list_proc
);
1394 * find_alloc_undo - Lookup (and if not present create) undo array
1396 * @semid: semaphore array id
1398 * The function looks up (and if not present creates) the undo structure.
1399 * The size of the undo structure depends on the size of the semaphore
1400 * array, thus the alloc path is not that straightforward.
1401 * Lifetime-rules: sem_undo is rcu-protected, on success, the function
1402 * performs a rcu_read_lock().
1404 static struct sem_undo
*find_alloc_undo(struct ipc_namespace
*ns
, int semid
)
1406 struct sem_array
*sma
;
1407 struct sem_undo_list
*ulp
;
1408 struct sem_undo
*un
, *new;
1411 error
= get_undo_list(&ulp
);
1413 return ERR_PTR(error
);
1416 spin_lock(&ulp
->lock
);
1417 un
= lookup_undo(ulp
, semid
);
1418 spin_unlock(&ulp
->lock
);
1419 if (likely(un
!=NULL
))
1422 /* no undo structure around - allocate one. */
1423 /* step 1: figure out the size of the semaphore array */
1424 sma
= sem_obtain_object_check(ns
, semid
);
1427 return ERR_CAST(sma
);
1430 nsems
= sma
->sem_nsems
;
1431 if (!ipc_rcu_getref(sma
)) {
1433 un
= ERR_PTR(-EIDRM
);
1438 /* step 2: allocate new undo structure */
1439 new = kzalloc(sizeof(struct sem_undo
) + sizeof(short)*nsems
, GFP_KERNEL
);
1442 return ERR_PTR(-ENOMEM
);
1445 /* step 3: Acquire the lock on semaphore array */
1446 sem_lock_and_putref(sma
);
1447 if (sma
->sem_perm
.deleted
) {
1448 sem_unlock(sma
, -1);
1450 un
= ERR_PTR(-EIDRM
);
1453 spin_lock(&ulp
->lock
);
1456 * step 4: check for races: did someone else allocate the undo struct?
1458 un
= lookup_undo(ulp
, semid
);
1463 /* step 5: initialize & link new undo structure */
1464 new->semadj
= (short *) &new[1];
1467 assert_spin_locked(&ulp
->lock
);
1468 list_add_rcu(&new->list_proc
, &ulp
->list_proc
);
1469 assert_spin_locked(&sma
->sem_perm
.lock
);
1470 list_add(&new->list_id
, &sma
->list_id
);
1474 spin_unlock(&ulp
->lock
);
1476 sem_unlock(sma
, -1);
1483 * get_queue_result - Retrieve the result code from sem_queue
1484 * @q: Pointer to queue structure
1486 * Retrieve the return code from the pending queue. If IN_WAKEUP is found in
1487 * q->status, then we must loop until the value is replaced with the final
1488 * value: This may happen if a task is woken up by an unrelated event (e.g.
1489 * signal) and in parallel the task is woken up by another task because it got
1490 * the requested semaphores.
1492 * The function can be called with or without holding the semaphore spinlock.
1494 static int get_queue_result(struct sem_queue
*q
)
1499 while (unlikely(error
== IN_WAKEUP
)) {
1508 SYSCALL_DEFINE4(semtimedop
, int, semid
, struct sembuf __user
*, tsops
,
1509 unsigned, nsops
, const struct timespec __user
*, timeout
)
1511 int error
= -EINVAL
;
1512 struct sem_array
*sma
;
1513 struct sembuf fast_sops
[SEMOPM_FAST
];
1514 struct sembuf
* sops
= fast_sops
, *sop
;
1515 struct sem_undo
*un
;
1516 int undos
= 0, alter
= 0, max
, locknum
;
1517 struct sem_queue queue
;
1518 unsigned long jiffies_left
= 0;
1519 struct ipc_namespace
*ns
;
1520 struct list_head tasks
;
1522 ns
= current
->nsproxy
->ipc_ns
;
1524 if (nsops
< 1 || semid
< 0)
1526 if (nsops
> ns
->sc_semopm
)
1528 if(nsops
> SEMOPM_FAST
) {
1529 sops
= kmalloc(sizeof(*sops
)*nsops
,GFP_KERNEL
);
1533 if (copy_from_user (sops
, tsops
, nsops
* sizeof(*tsops
))) {
1538 struct timespec _timeout
;
1539 if (copy_from_user(&_timeout
, timeout
, sizeof(*timeout
))) {
1543 if (_timeout
.tv_sec
< 0 || _timeout
.tv_nsec
< 0 ||
1544 _timeout
.tv_nsec
>= 1000000000L) {
1548 jiffies_left
= timespec_to_jiffies(&_timeout
);
1551 for (sop
= sops
; sop
< sops
+ nsops
; sop
++) {
1552 if (sop
->sem_num
>= max
)
1554 if (sop
->sem_flg
& SEM_UNDO
)
1556 if (sop
->sem_op
!= 0)
1560 INIT_LIST_HEAD(&tasks
);
1563 /* On success, find_alloc_undo takes the rcu_read_lock */
1564 un
= find_alloc_undo(ns
, semid
);
1566 error
= PTR_ERR(un
);
1574 sma
= sem_obtain_object_check(ns
, semid
);
1577 error
= PTR_ERR(sma
);
1582 if (max
>= sma
->sem_nsems
) {
1588 if (ipcperms(ns
, &sma
->sem_perm
, alter
? S_IWUGO
: S_IRUGO
)) {
1593 error
= security_sem_semop(sma
, sops
, nsops
, alter
);
1600 * semid identifiers are not unique - find_alloc_undo may have
1601 * allocated an undo structure, it was invalidated by an RMID
1602 * and now a new array with received the same id. Check and fail.
1603 * This case can be detected checking un->semid. The existence of
1604 * "un" itself is guaranteed by rcu.
1607 locknum
= sem_lock(sma
, sops
, nsops
);
1608 if (un
&& un
->semid
== -1)
1609 goto out_unlock_free
;
1611 error
= try_atomic_semop (sma
, sops
, nsops
, un
, task_tgid_vnr(current
));
1613 if (alter
&& error
== 0)
1614 do_smart_update(sma
, sops
, nsops
, 1, &tasks
);
1616 goto out_unlock_free
;
1619 /* We need to sleep on this operation, so we put the current
1620 * task into the pending queue and go to sleep.
1624 queue
.nsops
= nsops
;
1626 queue
.pid
= task_tgid_vnr(current
);
1627 queue
.alter
= alter
;
1631 curr
= &sma
->sem_base
[sops
->sem_num
];
1634 list_add_tail(&queue
.list
, &curr
->sem_pending
);
1636 list_add(&queue
.list
, &curr
->sem_pending
);
1639 list_add_tail(&queue
.list
, &sma
->sem_pending
);
1641 list_add(&queue
.list
, &sma
->sem_pending
);
1642 sma
->complex_count
++;
1645 queue
.status
= -EINTR
;
1646 queue
.sleeper
= current
;
1649 current
->state
= TASK_INTERRUPTIBLE
;
1650 sem_unlock(sma
, locknum
);
1653 jiffies_left
= schedule_timeout(jiffies_left
);
1657 error
= get_queue_result(&queue
);
1659 if (error
!= -EINTR
) {
1660 /* fast path: update_queue already obtained all requested
1662 * Perform a smp_mb(): User space could assume that semop()
1663 * is a memory barrier: Without the mb(), the cpu could
1664 * speculatively read in user space stale data that was
1665 * overwritten by the previous owner of the semaphore.
1672 sma
= sem_obtain_lock(ns
, semid
, sops
, nsops
, &locknum
);
1675 * Wait until it's guaranteed that no wakeup_sem_queue_do() is ongoing.
1677 error
= get_queue_result(&queue
);
1680 * Array removed? If yes, leave without sem_unlock().
1688 * If queue.status != -EINTR we are woken up by another process.
1689 * Leave without unlink_queue(), but with sem_unlock().
1692 if (error
!= -EINTR
) {
1693 goto out_unlock_free
;
1697 * If an interrupt occurred we have to clean up the queue
1699 if (timeout
&& jiffies_left
== 0)
1703 * If the wakeup was spurious, just retry
1705 if (error
== -EINTR
&& !signal_pending(current
))
1708 unlink_queue(sma
, &queue
);
1711 sem_unlock(sma
, locknum
);
1713 wake_up_sem_queue_do(&tasks
);
1715 if(sops
!= fast_sops
)
1720 SYSCALL_DEFINE3(semop
, int, semid
, struct sembuf __user
*, tsops
,
1723 return sys_semtimedop(semid
, tsops
, nsops
, NULL
);
1726 /* If CLONE_SYSVSEM is set, establish sharing of SEM_UNDO state between
1727 * parent and child tasks.
1730 int copy_semundo(unsigned long clone_flags
, struct task_struct
*tsk
)
1732 struct sem_undo_list
*undo_list
;
1735 if (clone_flags
& CLONE_SYSVSEM
) {
1736 error
= get_undo_list(&undo_list
);
1739 atomic_inc(&undo_list
->refcnt
);
1740 tsk
->sysvsem
.undo_list
= undo_list
;
1742 tsk
->sysvsem
.undo_list
= NULL
;
1748 * add semadj values to semaphores, free undo structures.
1749 * undo structures are not freed when semaphore arrays are destroyed
1750 * so some of them may be out of date.
1751 * IMPLEMENTATION NOTE: There is some confusion over whether the
1752 * set of adjustments that needs to be done should be done in an atomic
1753 * manner or not. That is, if we are attempting to decrement the semval
1754 * should we queue up and wait until we can do so legally?
1755 * The original implementation attempted to do this (queue and wait).
1756 * The current implementation does not do so. The POSIX standard
1757 * and SVID should be consulted to determine what behavior is mandated.
1759 void exit_sem(struct task_struct
*tsk
)
1761 struct sem_undo_list
*ulp
;
1763 ulp
= tsk
->sysvsem
.undo_list
;
1766 tsk
->sysvsem
.undo_list
= NULL
;
1768 if (!atomic_dec_and_test(&ulp
->refcnt
))
1772 struct sem_array
*sma
;
1773 struct sem_undo
*un
;
1774 struct list_head tasks
;
1778 un
= list_entry_rcu(ulp
->list_proc
.next
,
1779 struct sem_undo
, list_proc
);
1780 if (&un
->list_proc
== &ulp
->list_proc
)
1790 sma
= sem_obtain_object_check(tsk
->nsproxy
->ipc_ns
, un
->semid
);
1791 /* exit_sem raced with IPC_RMID, nothing to do */
1797 sem_lock(sma
, NULL
, -1);
1798 un
= __lookup_undo(ulp
, semid
);
1800 /* exit_sem raced with IPC_RMID+semget() that created
1801 * exactly the same semid. Nothing to do.
1803 sem_unlock(sma
, -1);
1807 /* remove un from the linked lists */
1808 assert_spin_locked(&sma
->sem_perm
.lock
);
1809 list_del(&un
->list_id
);
1811 spin_lock(&ulp
->lock
);
1812 list_del_rcu(&un
->list_proc
);
1813 spin_unlock(&ulp
->lock
);
1815 /* perform adjustments registered in un */
1816 for (i
= 0; i
< sma
->sem_nsems
; i
++) {
1817 struct sem
* semaphore
= &sma
->sem_base
[i
];
1818 if (un
->semadj
[i
]) {
1819 semaphore
->semval
+= un
->semadj
[i
];
1821 * Range checks of the new semaphore value,
1822 * not defined by sus:
1823 * - Some unices ignore the undo entirely
1824 * (e.g. HP UX 11i 11.22, Tru64 V5.1)
1825 * - some cap the value (e.g. FreeBSD caps
1826 * at 0, but doesn't enforce SEMVMX)
1828 * Linux caps the semaphore value, both at 0
1831 * Manfred <manfred@colorfullife.com>
1833 if (semaphore
->semval
< 0)
1834 semaphore
->semval
= 0;
1835 if (semaphore
->semval
> SEMVMX
)
1836 semaphore
->semval
= SEMVMX
;
1837 semaphore
->sempid
= task_tgid_vnr(current
);
1840 /* maybe some queued-up processes were waiting for this */
1841 INIT_LIST_HEAD(&tasks
);
1842 do_smart_update(sma
, NULL
, 0, 1, &tasks
);
1843 sem_unlock(sma
, -1);
1844 wake_up_sem_queue_do(&tasks
);
1851 #ifdef CONFIG_PROC_FS
1852 static int sysvipc_sem_proc_show(struct seq_file
*s
, void *it
)
1854 struct user_namespace
*user_ns
= seq_user_ns(s
);
1855 struct sem_array
*sma
= it
;
1857 return seq_printf(s
,
1858 "%10d %10d %4o %10u %5u %5u %5u %5u %10lu %10lu\n",
1863 from_kuid_munged(user_ns
, sma
->sem_perm
.uid
),
1864 from_kgid_munged(user_ns
, sma
->sem_perm
.gid
),
1865 from_kuid_munged(user_ns
, sma
->sem_perm
.cuid
),
1866 from_kgid_munged(user_ns
, sma
->sem_perm
.cgid
),
This page took 0.100025 seconds and 6 git commands to generate.