From: Bart Van Assche <bvanassche@acm.org>
To: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
Jens Axboe <axboe@kernel.dk>,
linux-block <linux-block@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
Christoph Hellwig <hch@lst.de>,
Damien Le Moal <dlemoal@kernel.org>
Subject: Re: [PATCH] loop: Fix NULL pointer dereference by synchronizing lo_release and loop_queue_rq
Date: Mon, 11 May 2026 08:58:07 -0700 [thread overview]
Message-ID: <c0047a38-3d02-4ce5-88b0-2b7d0b9b69fa@acm.org> (raw)
In-Reply-To: <e33b4060-69d9-4d02-a330-2fbd19249237@I-love.SAKURA.ne.jp>
On 5/11/26 4:43 AM, Tetsuo Handa wrote:
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 0000913f7efc..9be47ce97dab 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -93,6 +93,7 @@ struct loop_cmd {
> static DEFINE_IDR(loop_index_idr);
> static DEFINE_MUTEX(loop_ctl_mutex);
> static DEFINE_MUTEX(loop_validate_mutex);
> +DEFINE_SRCU(loop_io_srcu);
>
> /**
> * loop_global_lock_killable() - take locks for safe loop_validate_file() test
> @@ -1747,8 +1748,19 @@ static void lo_release(struct gendisk *disk)
> need_clear = (lo->lo_state == Lo_rundown);
> mutex_unlock(&lo->lo_mutex);
>
> - if (need_clear)
> + if (need_clear) {
> + /*
> + * Now that loop_queue_rq() sees lo->lo_state != Lo_bound,
> + * wait for already started loop_queue_rq() to complete.
> + */
> + synchronize_srcu(&loop_io_srcu);
> + /*
> + * Now that no more works are scheduled by loop_queue_rq(),
> + * wait for already scheduled works to complete.
> + */
> + drain_workqueue(lo->workqueue);
> __loop_clr_fd(lo);
> + }
> }
There is already a mechanism in the block layer to wait for pending
.queue_rq() calls to complete. Please take a look at
blk_mq_quiesce_queue().
> static void lo_free_disk(struct gendisk *disk)
> @@ -1854,11 +1866,15 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
> struct request *rq = bd->rq;
> struct loop_cmd *cmd = blk_mq_rq_to_pdu(rq);
> struct loop_device *lo = rq->q->queuedata;
> + int idx;
>
> blk_mq_start_request(rq);
>
> - if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound)
> + idx = srcu_read_lock(&loop_io_srcu);
> + if (data_race(READ_ONCE(lo->lo_state)) != Lo_bound) {
> + srcu_read_unlock(&loop_io_srcu, idx);
> return BLK_STS_IOERR;
> + }
>
> switch (req_op(rq)) {
> case REQ_OP_FLUSH:
> @@ -1888,6 +1904,7 @@ static blk_status_t loop_queue_rq(struct blk_mq_hw_ctx *hctx,
> #endif
> loop_queue_work(lo, cmd);
>
> + srcu_read_unlock(&loop_io_srcu, idx);
> return BLK_STS_OK;
> }
Why SRCU instead of RCU? The loop driver doesn't set BLK_MQ_F_BLOCKING
and hence must not sleep inside loop_queue_rq(). Additionally, the block
layer already holds an RCU lock around all loop_queue_rq() calls. From
block/blk-mq.h:
/* run the code block in @dispatch_ops with rcu/srcu read lock held */
#define __blk_mq_run_dispatch_ops(q, check_sleep, dispatch_ops) \
do { \
if ((q)->tag_set->flags & BLK_MQ_F_BLOCKING) { \
struct blk_mq_tag_set *__tag_set = (q)->tag_set; \
int srcu_idx; \
\
might_sleep_if(check_sleep); \
srcu_idx = srcu_read_lock(__tag_set->srcu); \
(dispatch_ops); \
srcu_read_unlock(__tag_set->srcu, srcu_idx); \
} else { \
rcu_read_lock(); \
(dispatch_ops); \
rcu_read_unlock(); \
} \
} while (0)
Thanks,
Bart.
next prev parent reply other threads:[~2026-05-11 15:58 UTC|newest]
Thread overview: 77+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-18 0:02 [syzbot] [block?] general protection fault in lo_rw_aio syzbot
[not found] ` <e1d824ba-9ac7-4fde-a791-32eeadcd4258@I-love.SAKURA.ne.jp>
2026-05-11 11:43 ` [PATCH] loop: Fix NULL pointer dereference by synchronizing lo_release and loop_queue_rq Tetsuo Handa
2026-05-11 15:58 ` Bart Van Assche [this message]
2026-05-11 17:43 ` Tetsuo Handa
2026-05-12 11:46 ` Tetsuo Handa
2026-05-15 1:38 ` [PATCH v2] " Tetsuo Handa
2026-05-19 0:40 ` Andrew Morton
2026-05-19 9:27 ` Tetsuo Handa
2026-05-20 3:06 ` Ming Lei
2026-05-20 6:36 ` Tetsuo Handa
2026-05-20 7:49 ` Ming Lei
2026-05-20 8:20 ` Tetsuo Handa
2026-05-20 8:54 ` Ming Lei
2026-05-25 3:40 ` [PATCH v3] loop: Fix NULL pointer dereference in lo_rw_aio() Tetsuo Handa
2026-05-25 15:19 ` Ming Lei
2026-05-26 0:25 ` Tetsuo Handa
2026-05-27 1:20 ` Ming Lei
2026-05-27 1:35 ` Tetsuo Handa
2026-05-27 3:00 ` Ming Lei
2026-05-27 11:29 ` Tetsuo Handa
2026-05-27 18:11 ` Damien Le Moal
2026-05-28 8:38 ` Christoph Hellwig
2026-05-28 10:16 ` Qu Wenruo
2026-06-01 14:40 ` Christoph Hellwig
2026-06-01 16:29 ` Brian Foster
2026-06-01 22:27 ` Qu Wenruo
2026-06-01 15:29 ` Ming Lei
2026-06-01 21:51 ` Hillf Danton
2026-06-01 22:14 ` Ming Lei
2026-06-01 23:17 ` Hillf Danton
2026-06-01 23:36 ` Ming Lei
2026-06-02 2:02 ` Hillf Danton
2026-05-28 5:43 ` Hillf Danton
2026-05-28 23:00 ` Hillf Danton
2026-05-29 0:14 ` Tetsuo Handa
2026-05-29 7:04 ` Hillf Danton
2026-05-29 22:05 ` Hillf Danton
2026-05-30 23:57 ` Tetsuo Handa
2026-06-07 10:54 ` [PATCH v4] " Tetsuo Handa
2026-06-09 17:50 ` Al Viro
2026-06-13 11:00 ` Tetsuo Handa
2026-06-19 14:33 ` Tetsuo Handa
2026-06-20 7:39 ` Al Viro
2026-06-20 9:42 ` Tetsuo Handa
2026-07-13 3:04 ` Hillf Danton
2026-07-13 11:02 ` Tetsuo Handa
2026-07-14 4:38 ` Hillf Danton
2026-07-16 0:05 ` [PATCH v5] " Tetsuo Handa
2026-08-23 11:17 ` [PATCH v6] " Tetsuo Handa
2026-08-23 15:57 ` Markus Elfring
2026-08-24 22:06 ` Tetsuo Handa
2026-08-24 22:53 ` Bart Van Assche
2026-08-24 23:24 ` Bart Van Assche
2026-08-25 15:13 ` Tetsuo Handa
2026-08-25 22:18 ` Bart Van Assche
2026-08-25 23:28 ` Tetsuo Handa
2026-08-25 23:16 ` Bart Van Assche
2026-08-26 10:37 ` Tetsuo Handa
2026-08-26 17:44 ` Bart Van Assche
2026-08-27 15:30 ` Tetsuo Handa
2026-08-27 17:28 ` Bart Van Assche
2026-08-28 15:53 ` [PATCH v7] " Tetsuo Handa
2026-08-28 16:29 ` Bart Van Assche
2026-08-29 5:17 ` Tetsuo Handa
2026-08-29 5:55 ` Tetsuo Handa
2026-08-31 14:11 ` Tao Cui
2026-08-31 15:49 ` Tetsuo Handa
2026-09-01 13:08 ` Tao Cui
2026-08-31 16:11 ` Bart Van Assche
2026-08-31 16:14 ` Bart Van Assche
2026-09-03 23:15 ` [PATCH v7.1] " Tetsuo Handa
2026-09-03 23:50 ` [PATCH v8] " Tetsuo Handa
2026-09-04 1:07 ` Hillf Danton
2026-09-05 14:25 ` kernel test robot
2026-08-28 15:01 ` [PATCH v3.1] " Tetsuo Handa
2026-07-15 16:01 ` [syzbot] [block?] general protection fault in lo_rw_aio Bart Van Assche
2026-07-15 16:02 ` syzbot
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=c0047a38-3d02-4ce5-88b0-2b7d0b9b69fa@acm.org \
--to=bvanassche@acm.org \
--cc=axboe@kernel.dk \
--cc=dlemoal@kernel.org \
--cc=hch@lst.de \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=penguin-kernel@I-love.SAKURA.ne.jp \
/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
all inboxes | Powered by JetHome®