[PATCH -next RFC v2 1/8] sbitmap: record the number of waiters for each waitqueue

From: Yu Kuai
Date: Fri Apr 08 2022 - 03:24:54 EST


Add a counter in struct sbq_wait_stat to recored how many threads are
waiting on the waitqueue, this will be used in later patches to make
sure 8 waitqueues are balanced. Such counter will also be shown in
debugfs so that user can see if waitqueues are balanced.

Signed-off-by: Yu Kuai <yukuai3@xxxxxxxxxx>
---
include/linux/sbitmap.h | 5 +++++
lib/sbitmap.c | 7 +++++--
2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
index 8f5a86e210b9..8a64271d0696 100644
--- a/include/linux/sbitmap.h
+++ b/include/linux/sbitmap.h
@@ -91,6 +91,11 @@ struct sbq_wait_state {
*/
atomic_t wait_cnt;

+ /**
+ * @waiters_cnt: Number of active waiters
+ */
+ atomic_t waiters_cnt;
+
/**
* @wait: Wait queue.
*/
diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index ae4fd4de9ebe..393f2b71647a 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -444,6 +444,7 @@ int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
init_waitqueue_head(&sbq->ws[i].wait);
atomic_set(&sbq->ws[i].wait_cnt, sbq->wake_batch);
+ atomic_set(&sbq->ws[i].waiters_cnt, 0);
}

return 0;
@@ -759,9 +760,9 @@ void sbitmap_queue_show(struct sbitmap_queue *sbq, struct seq_file *m)
for (i = 0; i < SBQ_WAIT_QUEUES; i++) {
struct sbq_wait_state *ws = &sbq->ws[i];

- seq_printf(m, "\t{.wait_cnt=%d, .wait=%s},\n",
+ seq_printf(m, "\t{.wait_cnt=%d, .waiters_cnt=%d},\n",
atomic_read(&ws->wait_cnt),
- waitqueue_active(&ws->wait) ? "active" : "inactive");
+ atomic_read(&ws->waiters_cnt));
}
seq_puts(m, "}\n");

@@ -798,6 +799,7 @@ void sbitmap_prepare_to_wait(struct sbitmap_queue *sbq,
{
if (!sbq_wait->sbq) {
atomic_inc(&sbq->ws_active);
+ atomic_inc(&ws->waiters_cnt);
sbq_wait->sbq = sbq;
}
prepare_to_wait_exclusive(&ws->wait, &sbq_wait->wait, state);
@@ -810,6 +812,7 @@ void sbitmap_finish_wait(struct sbitmap_queue *sbq, struct sbq_wait_state *ws,
finish_wait(&ws->wait, &sbq_wait->wait);
if (sbq_wait->sbq) {
atomic_dec(&sbq->ws_active);
+ atomic_dec(&ws->waiters_cnt);
sbq_wait->sbq = NULL;
}
}
--
2.31.1