Linux block layer
 help / color / mirror / Atom feed
From: Tao Cui <cui.tao@linux.dev>
To: Jens Axboe <axboe@kernel.dk>
Cc: Tejun Heo <tj@kernel.org>, Josef Bacik <josef@toxicpanda.com>,
	Omar Sandoval <osandov@fb.com>,
	Bart Van Assche <bvanassche@acm.org>,
	linux-block@vger.kernel.org, cgroups@vger.kernel.org,
	linux-kernel@vger.kernel.org, cui.tao@linux.dev,
	Tao Cui <cuitao@kylinos.cn>,
	stable@vger.kernel.org
Subject: [PATCH] block/blk-stat: fix use-after-free on callback timer teardown
Date: Mon, 20 Jul 2026 21:36:22 +0800	[thread overview]
Message-ID: <20260720133622.44949-1-cui.tao@linux.dev> (raw)

From: Tao Cui <cuitao@kylinos.cn>

blk_stat_remove_callback() cancels a callback's timer with
timer_delete_sync(), and its callers free the timer's data right after:
wbt_exit() does blk_stat_remove_callback() then wbt_free(), which
kfree()s struct rq_wb -- the very object the timer callback dereferences
as cb->data.

The blk-stat timer callback invokes the consumer's callback (wb_timer_fn
for WBT), which re-arms the timer via mod_timer() while there is
in-flight IO or an active scale_step.  By the timer API contract,
timer_delete_sync() is only meaningful when the caller guarantees the
timer is not restarted; with no shutdown guard in the callback, the
re-arm can win the race -- the callback re-arms the timer,
timer_delete_sync() returns, the data is freed, and the re-armed timer
later fires and dereferences freed memory.

Use timer_shutdown_sync(), which makes any later mod_timer() a no-op and
guarantees the timer cannot be queued or fire again once it returns.
blk_stat_remove_callback() is the generic teardown path for every
blk-stat callback, so this is safe for all users.

Fixes: 34dbad5d26e2 ("blk-stat: convert to callback-based statistics reporting")
Cc: stable@vger.kernel.org
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 block/blk-stat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/blk-stat.c b/block/blk-stat.c
index d57c2fc6bf06..e7f8a9b0c1d2 100644
--- a/block/blk-stat.c
+++ b/block/blk-stat.c
@@ -161,7 +161,7 @@ void blk_stat_remove_callback(struct request_queue *q, struct blk_stat_callback *cb)
 		blk_queue_flag_clear(QUEUE_FLAG_STATS, q);
 	spin_unlock_irqrestore(&q->stats->lock, flags);

-	timer_delete_sync(&cb->timer);
+	timer_shutdown_sync(&cb->timer);
 }

 static void blk_stat_free_callback_rcu(struct rcu_head *head)
--
2.43.0

                 reply	other threads:[~2026-07-20 13:36 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=20260720133622.44949-1-cui.tao@linux.dev \
    --to=cui.tao@linux.dev \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=cgroups@vger.kernel.org \
    --cc=cuitao@kylinos.cn \
    --cc=josef@toxicpanda.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=osandov@fb.com \
    --cc=stable@vger.kernel.org \
    --cc=tj@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