Skip to content

Commit f56c319

Browse files
htejuntorvalds
authored andcommitted
async: fix __lowest_in_progress()
Commit 083b804 ("async: use workqueue for worker pool") made it possible that async jobs are moved from pending to running out-of-order. While pending async jobs will be queued and dispatched for execution in the same order, nothing guarantees they'll enter "1) move self to the running queue" of async_run_entry_fn() in the same order. Before the conversion, async implemented its own worker pool. An async worker, upon being woken up, fetches the first item from the pending list, which kept the executing lists sorted. The conversion to workqueue was done by adding work_struct to each async_entry and async just schedules the work item. The queueing and dispatching of such work items are still in order but now each worker thread is associated with a specific async_entry and moves that specific async_entry to the executing list. So, depending on which worker reaches that point earlier, which is non-deterministic, we may end up moving an async_entry with larger cookie before one with smaller one. This broke __lowest_in_progress(). running->domain may not be properly sorted and is not guaranteed to contain lower cookies than pending list when not empty. Fix it by ensuring sort-inserting to the running list and always looking at both pending and running when trying to determine the lowest cookie. Over time, the async synchronization implementation became quite messy. We better restructure it such that each async_entry is linked to two lists - one global and one per domain - and not move it when execution starts. There's no reason to distinguish pending and running. They behave the same for synchronization purposes. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: Arjan van de Ven <arjan@linux.intel.com> Cc: stable@vger.kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ed06ef3 commit f56c319

1 file changed

Lines changed: 20 additions & 7 deletions

File tree

‎kernel/async.c‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,27 @@ static atomic_t entry_count;
8686
*/
8787
static async_cookie_t __lowest_in_progress(struct async_domain *running)
8888
{
89+
async_cookie_t first_running = next_cookie; /* infinity value */
90+
async_cookie_t first_pending = next_cookie; /* ditto */
8991
struct async_entry *entry;
9092

93+
/*
94+
* Both running and pending lists are sorted but not disjoint.
95+
* Take the first cookies from both and return the min.
96+
*/
9197
if (!list_empty(&running->domain)) {
9298
entry = list_first_entry(&running->domain, typeof(*entry), list);
93-
return entry->cookie;
99+
first_running = entry->cookie;
94100
}
95101

96-
list_for_each_entry(entry, &async_pending, list)
97-
if (entry->running == running)
98-
return entry->cookie;
102+
list_for_each_entry(entry, &async_pending, list) {
103+
if (entry->running == running) {
104+
first_pending = entry->cookie;
105+
break;
106+
}
107+
}
99108

100-
return next_cookie; /* "infinity" value */
109+
return min(first_running, first_pending);
101110
}
102111

103112
static async_cookie_t lowest_in_progress(struct async_domain *running)
@@ -118,13 +127,17 @@ static void async_run_entry_fn(struct work_struct *work)
118127
{
119128
struct async_entry *entry =
120129
container_of(work, struct async_entry, work);
130+
struct async_entry *pos;
121131
unsigned long flags;
122132
ktime_t uninitialized_var(calltime), delta, rettime;
123133
struct async_domain *running = entry->running;
124134

125-
/* 1) move self to the running queue */
135+
/* 1) move self to the running queue, make sure it stays sorted */
126136
spin_lock_irqsave(&async_lock, flags);
127-
list_move_tail(&entry->list, &running->domain);
137+
list_for_each_entry_reverse(pos, &running->domain, list)
138+
if (entry->cookie < pos->cookie)
139+
break;
140+
list_move_tail(&entry->list, &pos->list);
128141
spin_unlock_irqrestore(&async_lock, flags);
129142

130143
/* 2) run (and print duration) */

0 commit comments

Comments
 (0)