Skip to content

Commit f269f40

Browse files
manfred-colorfutorvalds
authored andcommitted
ipc/sem.c: always use only one queue for alter operations
There are two places that can contain alter operations: - the global queue: sma->pending_alter - the per-semaphore queues: sma->sem_base[].pending_alter. Since one of the queues must be processed first, this causes an odd priorization of the wakeups: complex operations have priority over simple ops. The patch restores the behavior of linux <=3.0.9: The longest waiting operation has the highest priority. This is done by using only one queue: - if there are complex ops, then sma->pending_alter is used. - otherwise, the per-semaphore queues are used. As a side effect, do_smart_update_queue() becomes much simpler: no more goto logic. Signed-off-by: Manfred Spraul <manfred@colorfullife.com> Cc: Rik van Riel <riel@redhat.com> Cc: Davidlohr Bueso <davidlohr.bueso@hp.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1a82e9e commit f269f40

1 file changed

Lines changed: 88 additions & 40 deletions

File tree

‎ipc/sem.c‎

Lines changed: 88 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,53 @@ void __init sem_init (void)
192192
IPC_SEM_IDS, sysvipc_sem_proc_show);
193193
}
194194

195+
/**
196+
* unmerge_queues - unmerge queues, if possible.
197+
* @sma: semaphore array
198+
*
199+
* The function unmerges the wait queues if complex_count is 0.
200+
* It must be called prior to dropping the global semaphore array lock.
201+
*/
202+
static void unmerge_queues(struct sem_array *sma)
203+
{
204+
struct sem_queue *q, *tq;
205+
206+
/* complex operations still around? */
207+
if (sma->complex_count)
208+
return;
209+
/*
210+
* We will switch back to simple mode.
211+
* Move all pending operation back into the per-semaphore
212+
* queues.
213+
*/
214+
list_for_each_entry_safe(q, tq, &sma->pending_alter, list) {
215+
struct sem *curr;
216+
curr = &sma->sem_base[q->sops[0].sem_num];
217+
218+
list_add_tail(&q->list, &curr->pending_alter);
219+
}
220+
INIT_LIST_HEAD(&sma->pending_alter);
221+
}
222+
223+
/**
224+
* merge_queues - Merge single semop queues into global queue
225+
* @sma: semaphore array
226+
*
227+
* This function merges all per-semaphore queues into the global queue.
228+
* It is necessary to achieve FIFO ordering for the pending single-sop
229+
* operations when a multi-semop operation must sleep.
230+
* Only the alter operations must be moved, the const operations can stay.
231+
*/
232+
static void merge_queues(struct sem_array *sma)
233+
{
234+
int i;
235+
for (i = 0; i < sma->sem_nsems; i++) {
236+
struct sem *sem = sma->sem_base + i;
237+
238+
list_splice_init(&sem->pending_alter, &sma->pending_alter);
239+
}
240+
}
241+
195242
/*
196243
* If the request contains only one semaphore operation, and there are
197244
* no complex transactions pending, lock only the semaphore involved.
@@ -262,6 +309,7 @@ static inline int sem_lock(struct sem_array *sma, struct sembuf *sops,
262309
static inline void sem_unlock(struct sem_array *sma, int locknum)
263310
{
264311
if (locknum == -1) {
312+
unmerge_queues(sma);
265313
ipc_unlock_object(&sma->sem_perm);
266314
} else {
267315
struct sem *sem = sma->sem_base + locknum;
@@ -831,49 +879,38 @@ static void do_smart_update(struct sem_array *sma, struct sembuf *sops, int nsop
831879
int otime, struct list_head *pt)
832880
{
833881
int i;
834-
int progress;
835882

836883
otime |= do_smart_wakeup_zero(sma, sops, nsops, pt);
837884

838-
progress = 1;
839-
retry_global:
840-
if (sma->complex_count) {
841-
if (update_queue(sma, -1, pt)) {
842-
progress = 1;
843-
otime = 1;
844-
sops = NULL;
845-
}
846-
}
847-
if (!progress)
848-
goto done;
849-
850-
if (!sops) {
851-
/* No semops; something special is going on. */
852-
for (i = 0; i < sma->sem_nsems; i++) {
853-
if (update_queue(sma, i, pt)) {
854-
otime = 1;
855-
progress = 1;
885+
if (!list_empty(&sma->pending_alter)) {
886+
/* semaphore array uses the global queue - just process it. */
887+
otime |= update_queue(sma, -1, pt);
888+
} else {
889+
if (!sops) {
890+
/*
891+
* No sops, thus the modified semaphores are not
892+
* known. Check all.
893+
*/
894+
for (i = 0; i < sma->sem_nsems; i++)
895+
otime |= update_queue(sma, i, pt);
896+
} else {
897+
/*
898+
* Check the semaphores that were increased:
899+
* - No complex ops, thus all sleeping ops are
900+
* decrease.
901+
* - if we decreased the value, then any sleeping
902+
* semaphore ops wont be able to run: If the
903+
* previous value was too small, then the new
904+
* value will be too small, too.
905+
*/
906+
for (i = 0; i < nsops; i++) {
907+
if (sops[i].sem_op > 0) {
908+
otime |= update_queue(sma,
909+
sops[i].sem_num, pt);
910+
}
856911
}
857912
}
858-
goto done_checkretry;
859-
}
860-
861-
/* Check the semaphores that were modified. */
862-
for (i = 0; i < nsops; i++) {
863-
if (sops[i].sem_op > 0 ||
864-
(sops[i].sem_op < 0 &&
865-
sma->sem_base[sops[i].sem_num].semval == 0))
866-
if (update_queue(sma, sops[i].sem_num, pt)) {
867-
otime = 1;
868-
progress = 1;
869-
}
870-
}
871-
done_checkretry:
872-
if (progress) {
873-
progress = 0;
874-
goto retry_global;
875913
}
876-
done:
877914
if (otime)
878915
sma->sem_otime = get_seconds();
879916
}
@@ -1747,11 +1784,22 @@ SYSCALL_DEFINE4(semtimedop, int, semid, struct sembuf __user *, tsops,
17471784
struct sem *curr;
17481785
curr = &sma->sem_base[sops->sem_num];
17491786

1750-
if (alter)
1751-
list_add_tail(&queue.list, &curr->pending_alter);
1752-
else
1787+
if (alter) {
1788+
if (sma->complex_count) {
1789+
list_add_tail(&queue.list,
1790+
&sma->pending_alter);
1791+
} else {
1792+
1793+
list_add_tail(&queue.list,
1794+
&curr->pending_alter);
1795+
}
1796+
} else {
17531797
list_add_tail(&queue.list, &curr->pending_const);
1798+
}
17541799
} else {
1800+
if (!sma->complex_count)
1801+
merge_queues(sma);
1802+
17551803
if (alter)
17561804
list_add_tail(&queue.list, &sma->pending_alter);
17571805
else

0 commit comments

Comments
 (0)