sbitmap: fix batched wait_cnt accounting
Subsystems (2)
PATCH v3 1/1 sbitmap: fix batched wait_cnt accounting
From: Keith Busch <[email protected]>
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.
---v2->v3:
Simplify try_cmpxchg loop by using the updated 'old' value instead of re-reading it (Pankaj)
Don't cap the wakeup count to "wait_batch" if more bits were cleared. This simplifies handling batched wakeups since it doesn't need to repeatedly wake the smaller number in a loop.
block/blk-mq-tag.c | 2 +- include/linux/sbitmap.h | 3 ++- lib/sbitmap.c | 31 +++++++++++++++++-------------- 3 files changed, 20 insertions(+), 16 deletions(-)
block/blk-mq-tag.c +1 -1
@@ -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 );
include/linux/sbitmap.h +2 -1
@@ -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
lib/sbitmap.c +17 -14
@@ -600,34 +600,38 @@ 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 wake_batch, wait_cnt, cur; ws =sbq_wake_ptr (sbq); -if (!ws) +if (!ws || !nr)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); + cur =atomic_read (&ws->wait_cnt); +do { +if (cur <=0 ) +return true; + wait_cnt = cur - nr; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, 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 || !waitqueue_active (&ws->wait)) +if (!waitqueue_active (&ws->wait))return true;if (wait_cnt >0 )return false; - wake_batch =READ_ONCE (sbq->wake_batch); -/* * 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,max (wake_batch, nr));/* * Pairs with the memory barrier in sbitmap_queue_resize() to
@@ -652,12 +656,11 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)return false; } -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) {
@@ -694,7 +697,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); }
@@ -722,7 +725,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);
Signature · 1 line
2.30.2
Quoted 8 lines · Keith Busch
From: Keith Busch <[email protected]>
Batched completions can clear multiple bits, but we're only decrementing the
wait_cntby one each time. This can cause waiters to never be woken, stalling IO. Use the batched count instead.[...]
Applied, thanks!
[1/1] sbitmap: fix batched wait_cnt accounting
commit: 16ede66973c84f890c03584f79158dd5b2d725f5
Best regards,
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerQuoted 8 lines · Jens Axboe
Quoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Batched completions can clear multiple bits, but we're only decrementing the
wait_cntby one each time. This can cause waiters to never be woken, stalling IO. Use the batched count instead.
Quoted 6 lines
Quoted 1 line
[...]
Applied, thanks!
[1/1] sbitmap: fix batched
wait_cntaccountingcommit: 16ede66973c84f890c03584f79158dd5b2d725f5
This is causing CPU stalls for me running make -j256 with the source hosted on an ATA device with QD=32. It's not running with a scheduler. It just goes spammy on most/all CPUs so hard to get a real trace out of it, but it looks like we're just looping forever off sbitmap_queue_wake_up().
I'm going to revert this one for now until we can investigate what is going on here.
Signature · 1 line
Jens Axboe
Quoted 16 lines · Jens Axboe
Quoted 8 lines · Jens Axboe
Quoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Batched completions can clear multiple bits, but we're only decrementing the
wait_cntby one each time. This can cause waiters to never be woken, stalling IO. Use the batched count instead.Quoted 6 lines
Quoted 1 line
[...]
Applied, thanks!
[1/1] sbitmap: fix batched
wait_cntaccountingcommit: 16ede66973c84f890c03584f79158dd5b2d725f5
Quoted 8 lines
This is causing CPU stalls for me running make -j256 with the source hosted on an ATA device with QD=32. It's not running with a scheduler. It just goes spammy on most/all CPUs so hard to get a real trace out of it, but it looks like we're just looping forever off
sbitmap_queue_wake_up().I'm going to revert this one for now until we can investigate what is going on here.
I was able to reproduce this without much trouble. I think it needs to restore the wait_cnt if we're racing with wait_active. I think the problem even exists without this patch ([1]), but you'd be unlikely to hit it decrementing wait_cnt just one at a time when the wait_batch is > 1. The diff on top of this patch should fix it:
---
- if (!waitqueue_active(&ws->wait))
+ if (!waitqueue_active(&ws->wait)) {
+ atomic_add(nr, &ws->wait_cnt); return true;
+ }
--
[1] https://lore.kernel.org/linux-block/[email protected]/T/#t