* [PATCHv4] sbitmap: fix batched wait_cnt accounting
@ 2022-09-08 21:51 Keith Busch
2022-09-09 15:18 ` Keith Busch
2022-09-09 15:49 ` Keith Busch
0 siblings, 2 replies; 3+ messages in thread
From: Keith Busch @ 2022-09-08 21:51 UTC (permalink / raw)
To: axboe, linux-block; +Cc: Keith Busch
From: Keith Busch <kbusch@kernel.org>
Batched completions can clear multiple bits, but we're only decrementing
the wait_cnt by one each time. This can cause waiters to never be woken,
stalling IO. Use the batched count instead.
Link: https://bugzilla.kernel.org/show_bug.cgi?id=215679
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
v3->v4:
Merged to the latest block for-6.1/block that contains the dependency
fix that I attempted to implement in v3.
block/blk-mq-tag.c | 2 +-
include/linux/sbitmap.h | 3 ++-
lib/sbitmap.c | 34 ++++++++++++++++++----------------
3 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/block/blk-mq-tag.c b/block/blk-mq-tag.c
index 8e3b36d1cb57..9eb968e14d31 100644
--- a/block/blk-mq-tag.c
+++ b/block/blk-mq-tag.c
@@ -196,7 +196,7 @@ unsigned int blk_mq_get_tag(struct blk_mq_alloc_data *data)
* other allocations on previous queue won't be starved.
*/
if (bt != bt_prev)
- sbitmap_queue_wake_up(bt_prev);
+ sbitmap_queue_wake_up(bt_prev, 1);
ws = bt_wait_ptr(bt, data->hctx);
} while (1);
diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
index 8f5a86e210b9..4d2d5205ab58 100644
--- a/include/linux/sbitmap.h
+++ b/include/linux/sbitmap.h
@@ -575,8 +575,9 @@ void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
* sbitmap_queue_wake_up() - Wake up some of waiters in one waitqueue
* on a &struct sbitmap_queue.
* @sbq: Bitmap queue to wake up.
+ * @nr: Number of bits cleared.
*/
-void sbitmap_queue_wake_up(struct sbitmap_queue *sbq);
+void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr);
/**
* sbitmap_queue_show() - Dump &struct sbitmap_queue information to a &struct
diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index cbfd2e677d87..cd631d2e4737 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -599,24 +599,27 @@ static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq)
return NULL;
}
-static bool __sbq_wake_up(struct sbitmap_queue *sbq)
+static bool __sbq_wake_up(struct sbitmap_queue *sbq, int nr)
{
struct sbq_wait_state *ws;
unsigned int wake_batch;
- int wait_cnt;
+ int wait_cnt, cur;
bool ret;
ws = sbq_wake_ptr(sbq);
- if (!ws)
+ if (!ws || nr <= 0)
return false;
- wait_cnt = atomic_dec_return(&ws->wait_cnt);
- /*
- * For concurrent callers of this, callers should call this function
- * again to wakeup a new batch on a different 'ws'.
- */
- if (wait_cnt < 0)
- return true;
+ cur = atomic_read(&ws->wait_cnt);
+ do {
+ /*
+ * For concurrent callers of this, callers should call this
+ * function again to wakeup a new batch on a different 'ws'.
+ */
+ if (cur <= 0)
+ return true;
+ wait_cnt = cur - nr;
+ } while (!atomic_try_cmpxchg(&ws->wait_cnt, &cur, wait_cnt));
/*
* If we decremented queue without waiters, retry to avoid lost
@@ -638,7 +641,7 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)
* Wake up first in case that concurrent callers decrease wait_cnt
* while waitqueue is empty.
*/
- wake_up_nr(&ws->wait, wake_batch);
+ wake_up_nr(&ws->wait, wake_batch - wait_cnt);
/*
* Pairs with the memory barrier in sbitmap_queue_resize() to
@@ -663,12 +666,11 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)
return ret;
}
-void sbitmap_queue_wake_up(struct sbitmap_queue *sbq)
+void sbitmap_queue_wake_up(struct sbitmap_queue *sbq, int nr)
{
- while (__sbq_wake_up(sbq))
+ while (__sbq_wake_up(sbq, nr))
;
}
-EXPORT_SYMBOL_GPL(sbitmap_queue_wake_up);
static inline void sbitmap_update_cpu_hint(struct sbitmap *sb, int cpu, int tag)
{
@@ -705,7 +707,7 @@ void sbitmap_queue_clear_batch(struct sbitmap_queue *sbq, int offset,
atomic_long_andnot(mask, (atomic_long_t *) addr);
smp_mb__after_atomic();
- sbitmap_queue_wake_up(sbq);
+ sbitmap_queue_wake_up(sbq, nr_tags);
sbitmap_update_cpu_hint(&sbq->sb, raw_smp_processor_id(),
tags[nr_tags - 1] - offset);
}
@@ -733,7 +735,7 @@ void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
* waiter. See the comment on waitqueue_active().
*/
smp_mb__after_atomic();
- sbitmap_queue_wake_up(sbq);
+ sbitmap_queue_wake_up(sbq, 1);
sbitmap_update_cpu_hint(&sbq->sb, cpu, nr);
}
EXPORT_SYMBOL_GPL(sbitmap_queue_clear);
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCHv4] sbitmap: fix batched wait_cnt accounting
2022-09-08 21:51 [PATCHv4] sbitmap: fix batched wait_cnt accounting Keith Busch
@ 2022-09-09 15:18 ` Keith Busch
2022-09-09 15:49 ` Keith Busch
1 sibling, 0 replies; 3+ messages in thread
From: Keith Busch @ 2022-09-09 15:18 UTC (permalink / raw)
To: Keith Busch; +Cc: axboe, linux-block
On Thu, Sep 08, 2022 at 02:51:32PM -0700, Keith Busch wrote:
> From: Keith Busch <kbusch@kernel.org>
>
> Batched completions can clear multiple bits, but we're only decrementing
> the wait_cnt by one each time. This can cause waiters to never be woken,
> stalling IO. Use the batched count instead.
>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=215679
A longer running test finally failed in a similar manner as before. It use to
fail much quicker, but something is still off, so please don't apply or test
this patch yet.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCHv4] sbitmap: fix batched wait_cnt accounting
2022-09-08 21:51 [PATCHv4] sbitmap: fix batched wait_cnt accounting Keith Busch
2022-09-09 15:18 ` Keith Busch
@ 2022-09-09 15:49 ` Keith Busch
1 sibling, 0 replies; 3+ messages in thread
From: Keith Busch @ 2022-09-09 15:49 UTC (permalink / raw)
To: Keith Busch; +Cc: axboe, linux-block
On Thu, Sep 08, 2022 at 02:51:32PM -0700, Keith Busch wrote:
> @@ -638,7 +641,7 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)
> * Wake up first in case that concurrent callers decrease wait_cnt
> * while waitqueue is empty.
> */
> - wake_up_nr(&ws->wait, wake_batch);
> + wake_up_nr(&ws->wait, wake_batch - wait_cnt);
The reason it was failing is this was not spreading the wake up among enough
wait states. I'll have to go back to the same method that patch 1 was doing
with clamping the decrement count..
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-09-09 15:49 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-08 21:51 [PATCHv4] sbitmap: fix batched wait_cnt accounting Keith Busch
2022-09-09 15:18 ` Keith Busch
2022-09-09 15:49 ` Keith Busch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox