Linux block layer
 help / color / mirror / Atom feed
From: guzebing <guzebing1612@gmail.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: Guzebing <guzebing1612@gmail.com>,
	linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] block: Make WBT latency writes honor enable state
Date: Sun, 21 Jun 2026 09:40:30 +0800	[thread overview]
Message-ID: <20260621014030.1625306-1-guzebing1612@gmail.com> (raw)

From: Guzebing <guzebing1612@gmail.com>

queue/wbt_lat_usec controls both the stored WBT latency target and the
effective WBT enable state.

The old no-op check skipped updates whenever the converted latency
matched the stored min_lat_nsec. That check ignored whether the current
WBT state already matched the state requested by the write. For a queue
disabled by default, attempting to enable WBT by writing the default
value through sysfs could return success while the enable state was left
unchanged.

Treat a write as a no-op only when both the stored latency and the
effective WBT enabled state already match the converted value.

Signed-off-by: Guzebing <guzebing1612@gmail.com>
---
Background:

The issue can be reproduced on an NVMe namespace when BFQ is available:

  echo bfq > /sys/block/nvme0n1/queue/scheduler
  cat /sys/block/nvme0n1/queue/wbt_lat_usec
  echo 2000 > /sys/block/nvme0n1/queue/wbt_lat_usec
  cat /sys/block/nvme0n1/queue/wbt_lat_usec

After BFQ selects the queue, WBT is disabled by default.  On a
non-rotational NVMe namespace the stored default latency remains
2000000 nsec, while the sysfs file reports 0 because the effective WBT
state is disabled:

  queue/wbt_lat_usec = 0
  debugfs enabled = 3
  debugfs min_lat_nsec = 2000000

Writing the default value succeeds, but the old no-op check skips the
state transition because min_lat_nsec already matches the converted
value:

  echo 2000 > /sys/block/nvme0n1/queue/wbt_lat_usec
  # echo returns success, but:
  queue/wbt_lat_usec = 0
  debugfs enabled = 3
  debugfs min_lat_nsec = 2000000

As a control, writing a non-default value first does work:

  echo 5000 > /sys/block/nvme0n1/queue/wbt_lat_usec
  queue/wbt_lat_usec = 5000
  debugfs enabled = 2
  debugfs min_lat_nsec = 5000000

Writing the default value after that also works, because the stored
latency changes from 5000000 nsec back to 2000000 nsec:

  echo 2000 > /sys/block/nvme0n1/queue/wbt_lat_usec
  queue/wbt_lat_usec = 2000
  debugfs enabled = 2
  debugfs min_lat_nsec = 2000000

With this patch, writing the default value after BFQ default-disables
WBT also re-enables WBT as expected:

  queue/wbt_lat_usec = 2000
  debugfs enabled = 2
  debugfs min_lat_nsec = 2000000

 block/blk-wbt.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/block/blk-wbt.c b/block/blk-wbt.c
index dcc2438ca16dc..953d400fd0137 100644
--- a/block/blk-wbt.c
+++ b/block/blk-wbt.c
@@ -813,6 +813,21 @@ static void wbt_queue_depth_changed(struct rq_qos *rqos)
 	wbt_update_limits(RQWB(rqos));
 }
 
+static bool wbt_set_lat_changed(struct request_queue *q, u64 val)
+{
+	struct rq_qos *rqos = wbt_rq_qos(q);
+	struct rq_wb *rwb;
+
+	if (!rqos)
+		return true;
+
+	rwb = RQWB(rqos);
+	if (rwb->min_lat_nsec != val)
+		return true;
+
+	return rwb_enabled(rwb) != !!val;
+}
+
 static void wbt_exit(struct rq_qos *rqos)
 {
 	struct rq_wb *rwb = RQWB(rqos);
@@ -1005,8 +1020,12 @@ int wbt_set_lat(struct gendisk *disk, s64 val)
 	else if (val >= 0)
 		val *= 1000ULL;
 
-	if (wbt_get_min_lat(q) == val)
+	mutex_lock(&disk->rqos_state_mutex);
+	if (!wbt_set_lat_changed(q, val)) {
+		mutex_unlock(&disk->rqos_state_mutex);
 		goto out;
+	}
+	mutex_unlock(&disk->rqos_state_mutex);
 
 	blk_mq_quiesce_queue(q);
 
-- 
2.20.1

                 reply	other threads:[~2026-06-21  1:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260621014030.1625306-1-guzebing1612@gmail.com \
    --to=guzebing1612@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox