Skip to contentSkip to conversation

sbitmap wake fixes

PATCH New Keith Busch 1 participant 3 messages Sep 6, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover sbitmap wake fixes

From: Keith Busch <[email protected]>

changes from v2:

Adding a prep patch that fixes a condition that can leave the wait_cnt at the wrong value.

Changed the number to wake up to account for the number of bits released in previous batches instead of just the current batch.

Keith Busch (2):
sbitmap: check waitqueue_active prior to decrement
sbitmap: fix batched wait_cnt accounting

 block/blk-mq-tag.c      |  2 +-
 include/linux/sbitmap.h |  3 ++-
 lib/sbitmap.c           | 36 ++++++++++++++++++++----------------
 3 files changed, 23 insertions(+), 18 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/2 sbitmap: check waitqueue_active prior to decrement

From: Keith Busch <[email protected]>

The bitmap wait state may have set an active count, but doesn't have an active waiter due to racing with adding it. If that happens, the state's wait_cnt will be set at the wrong value, and could prevent future wakes until the atomic_dec wraps back to 0.

Check the waitqueue_active before decrementing so that we don't need to account for fixing it up after.

Fixes 040b83fcecfb8 ("sbitmap: fix possible io hung due to lost wakeup")
---
 lib/sbitmap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
lib/sbitmap.c +5 -1
index a39b1a877366..ad0670b580d3 100644
@@ -609,12 +609,16 @@ static bool __sbq_wake_up(struct sbitmap_queue *sbq)
 	if (!ws)
 		return false;
 
+	if (!waitqueue_active(&ws->wait))
+		return true;
+
 	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 || !waitqueue_active(&ws->wait))
+	if (wait_cnt < 0)
 		return true;
 
 	if (wait_cnt > 0)
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/2 sbitmap: fix batched wait_cnt accounting

In reply to Keith Busch

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.

---
 block/blk-mq-tag.c      |  2 +-
 include/linux/sbitmap.h |  3 ++-
 lib/sbitmap.c           | 36 ++++++++++++++++++------------------
 3 files changed, 21 insertions(+), 20 deletions(-)
block/blk-mq-tag.c +1 -1
index 8e3b36d1cb57..9eb968e14d31 100644
@@ -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
index 8f5a86e210b9..4d2d5205ab58 100644
@@ -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 -18
index ad0670b580d3..0a9a5d5049d9 100644
@@ -599,27 +599,28 @@ 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;
 
 	if (!waitqueue_active(&ws->wait))
 		return true;
 
-	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 (wait_cnt > 0)
 		return false;
@@ -630,7 +631,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
@@ -655,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)
 {
@@ -697,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);
 }
@@ -725,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