sbitmap: fix batched wait_cnt accounting
Subsystems (2)
PATCH v2 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.
---v1->v2:
Use atomic_try_cmpxchg instead of direct subtraction to prevent a race between two batched completions.
Remove 'wait_cnt < 0' condition since it's not possible anymore.
Repeat wake up while waiters exist and more bits are unaccounted.
block/blk-mq-tag.c | 2 +- include/linux/sbitmap.h | 3 ++- lib/sbitmap.c | 32 ++++++++++++++++++-------------- 3 files changed, 21 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 +18 -14
@@ -600,28 +600,33 @@ 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, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); +do { + cur =atomic_read (&ws->wait_cnt); + sub =min3 (wake_batch, *nr, cur); + wait_cnt = cur - sub; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, wait_cnt)); + + *nr -= sub; +/* * 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); +return *nr >0 ;/* * Wake up first in case that concurrent callers decrease wait_cnt
@@ -649,15 +654,14 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)sbq_index_atomic_inc (&sbq->wake_index);atomic_set (&ws->wait_cnt, wake_batch); -return false; +return *nr >0 ; } -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 +698,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 +726,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 56 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.Signed-off-by Keith Busch <[email protected]>---
v1->v2:Use
atomic_try_cmpxchginstead of direct subtraction to prevent a race between two batched completions.Remove '
wait_cnt< 0' condition since it's not possible anymore.Repeat wake up while waiters exist and more bits are unaccounted.
block/blk-mq-tag.c | 2 +- include/linux/sbitmap.h | 3 ++- lib/sbitmap.c | 32 ++++++++++++++++++-------------- 3 files changed, 21 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 +0
-0
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 603
Quoted 5 lines
lib/sbitmap.c +1
-1@@ -600,28 +600,33 @@ 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)
Why is '*nr' a pointer? It's always used as a value, so what does promoting it to a point buys you?
Quoted 65 lines
lib/sbitmap.c +16
-12{struct sbq_wait_state *ws; -unsigned int wake_batch; -int wait_cnt; +int wake_batch, wait_cnt, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); +do { + cur =atomic_read (&ws->wait_cnt); + sub =min3 (wake_batch, *nr, cur); + wait_cnt = cur - sub; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, wait_cnt)); + + *nr -= sub; +/* * 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); +return *nr >0 ;/* * Wake up first in case that concurrent callers decrease wait_cnt @@ -649,15 +654,14 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)sbq_index_atomic_inc (&sbq->wake_index);atomic_set (&ws->wait_cnt, wake_batch); -return false; +return *nr >0 ; } -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 +698,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); }
lib/sbitmap.c void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr, · line 732
Quoted 8 lines
lib/sbitmap.c +1
-1@@ -722,7 +726,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);
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Quoted 7 lines · Keith Busch
lib/sbitmap.c +1
-3-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;
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 614
Quoted 10 lines
lib/sbitmap.c +4
-1+int wake_batch, wait_cnt, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); + do { + cur = atomic_read(&ws->wait_cnt);
I think the above statement is not needed if we use atomic_try_cmpxchg as the old value is updated in that function itself.
https://docs.kernel.org/staging/index.html?highlight=atomic_try_cmpxchg#atomic-types
Quoted 7 lines
lib/sbitmap.c +6
-0+ sub =min3 (wake_batch, *nr, cur); + wait_cnt = cur - sub; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, wait_cnt)); + + *nr -= sub; +/*
Signature · 1 line
Pankaj Raghav
Quoted 14 lines · Pankaj Raghav
Quoted 13 lines · Keith Busch
lib/sbitmap.c +2
-3-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, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false;
Quoted 8 lines
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 614
Quoted 4 lines
lib/sbitmap.c +3
-1- wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); + do { + cur = atomic_read(&ws->wait_cnt);I think the above statement is not needed if we use
atomic_try_cmpxchgas the old value is updated in that function itself.
https://docs.kernel.org/staging/index.html?highlight=atomic_try_cmpxchg#atomic-types
I mean after moving the cur = atomic_read(..) above the do statement:
wake_batch = READ_ONCE(sbq->wake_batch);
cur = atomic_read(&ws->wait_cnt);
do {
sub = min3(wake_batch, *nr, cur);
wait_cnt = cur - sub;
} while (!atomic_try_cmpxchg(&ws->wait_cnt, &cur, wait_cnt));Signature · 1 line
Pankaj Raghav
Quoted 4 lines · Hannes Reinecke
Quoted 1 line
+static bool
__sbq_wake_up(struct sbitmap_queue *sbq, int *nr)Why is '*nr' a pointer? It's always used as a value, so what does promoting it to a point buys you?
Not quite, see below:
Quoted 9 lines
Quoted 9 lines
{ struct sbq_wait_state *ws;lib/sbitmap.c +1
-3-unsigned int wake_batch; -int wait_cnt; +int wake_batch, wait_cnt, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt);
Quoted 8 lines
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 619
Quoted 8 lines
lib/sbitmap.c +8
-0+ wake_batch =READ_ONCE (sbq->wake_batch); +do { + cur =atomic_read (&ws->wait_cnt); + sub =min3 (wake_batch, *nr, cur); + wait_cnt = cur - sub; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, wait_cnt)); + + *nr -= sub;
Dereferenced here to account for how many bits were considered for this wake cycle.
Quoted 24 lines · Pankaj Raghav
Quoted 14 lines · Pankaj Raghav
Quoted 13 lines · Keith Busch
lib/sbitmap.c +2
-3-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, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false;Quoted 8 lines
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 614
Quoted 4 lines
lib/sbitmap.c +3
-1- wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); + do { + cur = atomic_read(&ws->wait_cnt);I think the above statement is not needed if we use
atomic_try_cmpxchgas the old value is updated in that function itself.
https://docs.kernel.org/staging/index.html?highlight=atomic_try_cmpxchg#atomic-types
Quoted 8 lines
I mean after moving the cur =
atomic_read(..)above the do statement:
wake_batch=READ_ONCE(sbq->wake_batch);
cur =atomic_read(&ws->wait_cnt);do { sub = min3(wake_batch, *nr, cur); wait_cnt = cur - sub; } while (!atomic_try_cmpxchg(&ws->wait_cnt, &cur, wait_cnt));
Yeah, that's a good change. I'll fix it up.
Quoted 20 lines · Keith Busch
Quoted 4 lines · Hannes Reinecke
Quoted 1 line
+static bool
__sbq_wake_up(struct sbitmap_queue *sbq, int *nr)Why is '*nr' a pointer? It's always used as a value, so what does promoting it to a point buys you?
Not quite, see below:
Quoted 12 lines
Quoted 12 lines
{ struct sbq_wait_state *ws;lib/sbitmap.c +4
-3-unsigned int wake_batch; -int wait_cnt; +int wake_batch, wait_cnt, sub, cur; ws =sbq_wake_ptr (sbq);if (!ws)return false; - wait_cnt =atomic_dec_return (&ws->wait_cnt); + wake_batch =READ_ONCE (sbq->wake_batch); + do { + cur = atomic_read(&ws->wait_cnt);
Quoted 8 lines
Quoted 5 lines
lib/sbitmap.c static struct sbq_wait_state *sbq_wake_ptr(struct sbitmap_queue *sbq) · line 619
Quoted 5 lines
lib/sbitmap.c +5
-0+ sub =min3 (wake_batch, *nr, cur); + wait_cnt = cur - sub; + }while (!atomic_try_cmpxchg (&ws->wait_cnt, &cur, wait_cnt)); + + *nr -= sub;Dereferenced here to account for how many bits were considered for this wake cycle.
Actually, I think we can just get rid of this accounting. Instead of calling wake_up_nr() with wake_batch multiple times, we can call wake_up_nr() with max(wake_batch, nr) just once, then all bits are accounted for in a single go. The 'wake_batch' value is really just the lowest amount of tags to accumulate before waking tag waiters; having more should be fine.