dm: make sure to wait for all dispatched requests in __dm_suspend()
RFC PATCH 1/1 dm: make sure to wait for all dispatched requests in __dm_suspend()
In a recent kernel dump analysis, we found that the kernel crashed because dm_rq_target_io tio->ti was pointing to invalid memory in dm_end_request(), in a situation where multipathd was doing map reloads because of a storage failover. The map of the respective mapped_device had been replaced by a different struct dm_table.
We obverved this with a 5.3.18 distro kernel, but the code in question hasn't change much since then. Basically, we were only missing b4459b11e840 ("dm rq: don't queue request to blk-mq during DM suspend"), which doesn't guarantee that the race I'm thinking of (see below) can't happen.
When a map is resumed after a table reload, the live table is swapped, and the tio->ti member of any live request becomes stale. __dm_resume() avoids this by quiescing the queue and calling dm_wait_for_completion(), which waits until blk_mq_queue_inflight() doesn't report any in-flight requests.
However, blk_mq_queue_inflight() counts only "started" requests. So, if a request is dispatched before the queue was quiesced, but dm_wait_for_completion() doesn't observe MQ_RQ_IN_FLIGHT for this request because of memory ordering effects, __dm_suspend() may finish successfully, even though there is an active request, and the resume code path may proceed through dm_swap_table() and dm_table_destroy(). The request that sneaked through will then carry an invalid dm_target reference in tio->ti. This is how I believe the above crash came to pass.
This patch tries to fix this by setting the "started" status of the request inside a SRCU read side critical section. In __dm_suspend(), synchronize_srcu(&md->io_barrier) is called after setting DMF_BLOCK_IO_FOR_SUSPEND. The read side critical section is executed completely, with all its memory effects, either before or after the synchronize_srcu(). In the first case, dm_wait_for_completion() will observe the in-flight status. Otherwise, dm_mq_queue_rq will see DMF_BLOCK_IO_FOR_SUSPEND and give up. In both cases, there will be no stale reference in tio->target when the request finishes.
--- drivers/md/dm-rq.c | 51 ++++++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 18 deletions(-)
drivers/md/dm-rq.c +33 -18
@@ -481,6 +481,10 @@ static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,struct dm_rq_target_io *tio =blk_mq_rq_to_pdu (rq);struct mapped_device *md = tio->md;struct dm_target *ti = md->immutable_target; +struct dm_table *map; +int srcu_idx; + + map =dm_get_live_table (md, &srcu_idx);/* * blk-mq's unquiesce may come from outside events, such as
@@ -488,25 +492,31 @@ static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx,* come during suspend, so simply ask for blk-mq to requeue it. */ if (unlikely (test_bit (DMF_BLOCK_IO_FOR_SUSPEND , &md->flags))) -return BLK_STS_RESOURCE ; + goto out_put_table; + +/* +* If this request got dispatched before the queue was stopped in +* __dm_suspend(), make sure dm_wait_for_completion() will +* see this request as in flight. Otherwise this request may be actually +* serviced while the table is swapped, and when it finishes, tio->ti +* may reference a stale target. +* As we're in a read-side critical section here, the synchronize_srcu() +* in __dm_suspend() guarantees that if we haven't seen +* DMF_BLOCK_IO_FOR_SUSPEND here, __dm_suspend() will observe the +* in flight status. +*/ +dm_start_request (md, rq);if (unlikely (!ti)) { -int srcu_idx; -struct dm_table *map; - - map =dm_get_live_table (md, &srcu_idx); -if (unlikely (!map)) { -dm_put_live_table (md, srcu_idx); -return BLK_STS_RESOURCE ; - } +if (unlikely (!map)) + goto out_put_table; ti =dm_table_find_target (map,0 ); -dm_put_live_table (md, srcu_idx); } +dm_put_live_table (md, srcu_idx);if (ti->type->busy && ti->type->busy (ti)) -return BLK_STS_RESOURCE ; + goto out_requeue; -dm_start_request (md, rq);/* Init tio using md established in .init_request */ init_tio (tio, rq, md);
@@ -517,14 +527,19 @@ static blk_status_t dm_mq_queue_rq(struct blk_mq_hw_ctx *hctx, tio->ti = ti;/* Direct call is fine since .queue_rq allows allocations */ -if (map_request (tio) ==DM_MAPIO_REQUEUE ) { -/* Undo dm_start_request() before requeuing */ -rq_end_stats (md, rq); -rq_completed (md); -return BLK_STS_RESOURCE ; - } +if (map_request (tio) ==DM_MAPIO_REQUEUE ) + goto out_requeue;return BLK_STS_OK ; + +out_put_table: +dm_put_live_table (md, srcu_idx); + +out_requeue: +/* Undo dm_start_request() before requeuing */ +rq_end_stats (md, rq); +rq_completed (md); +return BLK_STS_RESOURCE ; }static const struct blk_mq_ops dm_mq_ops = {
Signature · 1 line
2.43.2
Hello Martin,
Quoted 17 lines · Martin Wilck
In a recent kernel dump analysis, we found that the kernel crashed because
dm_rq_target_iotio->tiwas pointing to invalid memory indm_end_request(), in a situation where multipathd was doing map reloads because of a storage failover. The map of the respectivemapped_devicehad been replaced by a different structdm_table.We obverved this with a 5.3.18 distro kernel, but the code in question hasn't change much since then. Basically, we were only missing
b4459b11e840("dm rq: don't queue request to blk-mq during DM suspend"), which doesn't guarantee that the race I'm thinking of (see below) can't happen.When a map is resumed after a table reload, the live table is swapped, and the
tio->timember of any live request becomes stale.__dm_resume()avoids this by quiescing the queue and callingdm_wait_for_completion(), which waits untilblk_mq_queue_inflight()doesn't report any in-flight requests.
Quoted 4 lines
However,
blk_mq_queue_inflight()counts only "started" requests. So, if a request is dispatched before the queue was quiesced, butdm_wait_for_completion()doesn't observeMQ_RQ_IN_FLIGHTfor this request because of memory ordering effects,__dm_suspend()may finish successfully,
Can you explain a bit about the exact memory order which causes MQ_RQ_IN_FLIGHT not observed?
blk-mq quiesce includes synchronize_rcu() which drains all in-flight dispatch, so after blk_mq_quiesce_queue() returns,
if blk_mq_queue_inflight() returns 0, it does mean there isn't any active inflight requests.
If there is bug in this pattern, I guess more drivers may have such 'risk'.
BTW, what is the underlying disks in your dm-mpath setting?
Thanks,
Ming
Hello Ming,
Quoted 43 lines · Ming Lei
Hello Martin,
Quoted 28 lines · Martin Wilck
In a recent kernel dump analysis, we found that the kernel crashed because
dm_rq_target_iotio->tiwas pointing to invalid memory indm_end_request(),
in a situation where multipathd was doing map reloads because of a storage
failover. The map of the respectivemapped_devicehad been replaced by a
different structdm_table.We obverved this with a 5.3.18 distro kernel, but the code in question
hasn't change much since then. Basically, we were only missingb4459b11e840("dm rq: don't queue request to blk-mq during DM suspend"),
which doesn't guarantee that the race I'm thinking of (see below) can't
happen.When a map is resumed after a table reload, the live table is swapped, and
thetio->timember of any live request becomes stale.__dm_resume()avoids
this by quiescing the queue and callingdm_wait_for_completion(), which
waits untilblk_mq_queue_inflight()doesn't report any in-flight requests.Quoted 7 lines
However,
blk_mq_queue_inflight()counts only "started" requests. So, if a
request is dispatched before the queue was quiesced, butdm_wait_for_completion()doesn't observeMQ_RQ_IN_FLIGHTfor this request
because of memory ordering effects,__dm_suspend()may finish successfully,Can you explain a bit about the exact memory order which causes
MQ_RQ_IN_FLIGHT
not observed?
Quoted 7 lines
blk-mq quiesce includes
synchronize_rcu()which drains all in-flight dispatch, so afterblk_mq_quiesce_queue()returns,
ifblk_mq_queue_inflight()returns 0, it does mean there isn't any active inflight requests.If there is bug in this pattern, I guess more drivers may have such 'risk'.
What we know for sure is that there was a bad dm_target reference in (struct dm_rq_target_io *tio)->ti:
crash> struct -x dm_rq_target_io c00000245ca90128 struct dm_rq_target_io {
md = 0xc0000031c66a4000,
ti = 0xc0080000020d0080 <fscache_object_list_lock+665632>,
crash> struct -x dm_target 0xc0080000020d0080 struct dm_target struct: invalid kernel virtual address:c0080000020d0080 type: "gdb_readmem_callback"
The question is how this could have come to pass. It can only happen if tio->ti had been set before the map was reloaded.
My theory is that the IO had been dispatched before the queue had been quiesced, like this:
Task A Task B
(dispatching IO) (executing a DM_SUSPEND ioctl to
resume after DM_TABLE_LOAD)
do_resume()
dm_suspend()
__dm_suspend()
dm_mq_queue_rq()
struct dm_target *ti =
md->immutable_target;
dm_stop_queue()
blk_mq_quiesce_queue()
/*
* At this point, the queue is quiesced, but task A
* has alreadyentered dm_mq_queue_rq()
*/
dm_wait_for_completion()
blk_mq_queue_inflight()
/*
* blk_mq_queue_inflight() doesn't see Task A's
* request because it isn't started yet
*/
set_bit(dmf_suspended_flag)
dm_start_request(md, rq); dm_swap_table()
__bind()
md->immutable_target = ...
dm_target_destroy()
/* the previous md->immutable_target is freed */
init_tio(tio, rq, md);
/* the stale ti pointer is assigned to tio->ti */ tio->ti = ti;
dm_mq_queue_rq() contains no synchronization code if md->immutable_target is set, so I think that this can happen, even though it looks unlikely. With b4459b11e840 (which was not applied in the customer kernel), there would be a set_bit(DMF_BLOCK_IO_FOR_SUSPEND) statement before dm_stop_queue(), but IMO that the above would still be possible.
If this can't happen, I have no more ideas how the observed situation came to pass. The customer who sent us the core claims that he has seen this multiple times already (but we have only this single core dump).
Quoted 1 line
BTW, what is the underlying disks in your dm-mpath setting?
It was ibmvfc.
Thanks for looking into this,
Martin
Mike Snitzer
maintainerQuoted 109 lines · Martin Wilck
Hello Ming,
Quoted 43 lines · Ming Lei
Hello Martin,
Quoted 28 lines · Martin Wilck
In a recent kernel dump analysis, we found that the kernel crashed because
dm_rq_target_iotio->tiwas pointing to invalid memory indm_end_request(),
in a situation where multipathd was doing map reloads because of a storage
failover. The map of the respectivemapped_devicehad been replaced by a
different structdm_table.We obverved this with a 5.3.18 distro kernel, but the code in question
hasn't change much since then. Basically, we were only missingb4459b11e840("dm rq: don't queue request to blk-mq during DM suspend"),
which doesn't guarantee that the race I'm thinking of (see below) can't
happen.When a map is resumed after a table reload, the live table is swapped, and
thetio->timember of any live request becomes stale.__dm_resume()avoids
this by quiescing the queue and callingdm_wait_for_completion(), which
waits untilblk_mq_queue_inflight()doesn't report any in-flight requests.Quoted 7 lines
However,
blk_mq_queue_inflight()counts only "started" requests. So, if a
request is dispatched before the queue was quiesced, butdm_wait_for_completion()doesn't observeMQ_RQ_IN_FLIGHTfor this request
because of memory ordering effects,__dm_suspend()may finish successfully,Can you explain a bit about the exact memory order which causes
MQ_RQ_IN_FLIGHT
not observed?Quoted 7 lines
blk-mq quiesce includes
synchronize_rcu()which drains all in-flight dispatch, so afterblk_mq_quiesce_queue()returns,
ifblk_mq_queue_inflight()returns 0, it does mean there isn't any active inflight requests.If there is bug in this pattern, I guess more drivers may have such 'risk'.
What we know for sure is that there was a bad
dm_targetreference in (structdm_rq_target_io*tio)->ti:crash> struct -x
dm_rq_target_ioc00000245ca90128structdm_rq_target_io{
md =0xc0000031c66a4000,
ti =0xc0080000020d0080<fscache_object_list_lock+665632>,crash> struct -x
dm_target0xc0080000020d0080structdm_targetstruct: invalid kernel virtual address:c0080000020d0080type: "gdb_readmem_callback"The question is how this could have come to pass. It can only happen if
tio->tihad been set before the map was reloaded.
My theory is that the IO had been dispatched before the queue had been quiesced, like this:Task A Task B
(dispatching IO) (executing aDM_SUSPENDioctl toresume after DM_TABLE_LOAD) do_resume() dm_suspend() __dm_suspend() dm_mq_queue_rq() struct dm_target *ti = md->immutable_target; dm_stop_queue() blk_mq_quiesce_queue() /* * At this point, the queue is quiesced, but task A * has alreadyentered dm_mq_queue_rq() */ dm_wait_for_completion() blk_mq_queue_inflight() /* * blk_mq_queue_inflight() doesn't see Task A's * request because it isn't started yet */ set_bit(dmf_suspended_flag) dm_start_request(md, rq); dm_swap_table() __bind() md->immutable_target = ... dm_target_destroy() /* the previous md->immutable_target is freed */ init_tio(tio, rq, md); /* the stale ti pointer is assigned to tio->ti */
tio->ti= ti;
dm_mq_queue_rq()contains no synchronization code ifmd->immutable_targetis set, so I think that this can happen, even though it looks unlikely. Withb4459b11e840(which was not applied in the customer kernel), there would be aset_bit(DMF_BLOCK_IO_FOR_SUSPEND)statement beforedm_stop_queue(), but IMO that the above would still be possible.
Quoted 4 lines
If this can't happen, I have no more ideas how the observed situation came to pass. The customer who sent us the core claims that he has seen this multiple times already (but we have only this single core dump).
While I appreciate you making us aware of this crash, I'm really not interested in going down the rabbit hole of reasoning through fairly complex concerns unless you have gotten your customer a kernel with latest fixes (e.g. commit b4459b11e840) and they _still_ crash.
Has that happened?
NOTE: There is also 85067747cf98 ("dm: do not use waitqueue for request-based DM") but you probably have it since you said other than missing the changes from commit b4459b11e840 that your dm-rq.c was same.
Mike
Mike,
Quoted 7 lines · Mike Snitzer
While I appreciate you making us aware of this crash, I'm really not interested in going down the rabbit hole of reasoning through fairly complex concerns unless you have gotten your customer a kernel with latest fixes (e.g. commit
b4459b11e840) and they _still_ crash.Has that happened?
Not yet; I wanted to bring up the issue upstream first. But what you're saying makes sense. We will ask the customer to test with a kernel that has b4459b11e840 added.
Quoted 4 lines
NOTE: There is also
85067747cf98("dm: do not use waitqueue for request-based DM") but you probably have it since you said other than missing the changes from commitb4459b11e840that yourdm-rq.cwas same.
This commit was already contained in the kernel that the customer had.
Thanks,
Martin
Quoted 73 lines · Martin Wilck
Hello Ming,
Quoted 43 lines · Ming Lei
Hello Martin,
Quoted 28 lines · Martin Wilck
In a recent kernel dump analysis, we found that the kernel crashed because
dm_rq_target_iotio->tiwas pointing to invalid memory indm_end_request(),
in a situation where multipathd was doing map reloads because of a storage
failover. The map of the respectivemapped_devicehad been replaced by a
different structdm_table.We obverved this with a 5.3.18 distro kernel, but the code in question
hasn't change much since then. Basically, we were only missingb4459b11e840("dm rq: don't queue request to blk-mq during DM suspend"),
which doesn't guarantee that the race I'm thinking of (see below) can't
happen.When a map is resumed after a table reload, the live table is swapped, and
thetio->timember of any live request becomes stale.__dm_resume()avoids
this by quiescing the queue and callingdm_wait_for_completion(), which
waits untilblk_mq_queue_inflight()doesn't report any in-flight requests.Quoted 7 lines
However,
blk_mq_queue_inflight()counts only "started" requests. So, if a
request is dispatched before the queue was quiesced, butdm_wait_for_completion()doesn't observeMQ_RQ_IN_FLIGHTfor this request
because of memory ordering effects,__dm_suspend()may finish successfully,Can you explain a bit about the exact memory order which causes
MQ_RQ_IN_FLIGHT
not observed?Quoted 7 lines
blk-mq quiesce includes
synchronize_rcu()which drains all in-flight dispatch, so afterblk_mq_quiesce_queue()returns,
ifblk_mq_queue_inflight()returns 0, it does mean there isn't any active inflight requests.If there is bug in this pattern, I guess more drivers may have such 'risk'.
What we know for sure is that there was a bad
dm_targetreference in (structdm_rq_target_io*tio)->ti:crash> struct -x
dm_rq_target_ioc00000245ca90128structdm_rq_target_io{
md =0xc0000031c66a4000,
ti =0xc0080000020d0080<fscache_object_list_lock+665632>,crash> struct -x
dm_target0xc0080000020d0080structdm_targetstruct: invalid kernel virtual address:c0080000020d0080type: "gdb_readmem_callback"The question is how this could have come to pass. It can only happen if
tio->tihad been set before the map was reloaded.
My theory is that the IO had been dispatched before the queue had been quiesced, like this:Task A Task B
(dispatching IO) (executing aDM_SUSPENDioctl to
Quoted 13 lines
resume after DM_TABLE_LOAD)
do_resume()
dm_suspend()
__dm_suspend()
dm_mq_queue_rq()
struct dm_target *ti =
md->immutable_target;
dm_stop_queue()
blk_mq_quiesce_queue()
/*
* At this point, the queue is quiesced, but task A
* has alreadyentered dm_mq_queue_rq()
*/That shouldn't happen, blk_mq_quiesce_queue() drains all pending dm_mq_queue_rq() and prevents new dm_mq_queue_rq() from being called.
Quoted 23 lines
dm_wait_for_completion()
blk_mq_queue_inflight()
/*
*blk_mq_queue_inflight()doesn't see Task A's
* request because it isn't started yet
*/
set_bit(dmf_suspended_flag)
dm_start_request(md, rq);dm_swap_table()__bind() md->immutable_target = ... dm_target_destroy() /* the previous md->immutable_target is freed */ init_tio(tio, rq, md); /* the stale ti pointer is assigned to tio->ti */
tio->ti= ti;
dm_mq_queue_rq()contains no synchronization code ifmd->immutable_targetis set, so I think that this can happen, even though it looks unlikely. Withb4459b11e840(which was not applied in the customer kernel), there would be aset_bit(DMF_BLOCK_IO_FOR_SUSPEND)statement beforedm_stop_queue(), but IMO that the above would still be possible.
Quoted 4 lines
If this can't happen, I have no more ideas how the observed situation came to pass. The customer who sent us the core claims that he has seen this multiple times already (but we have only this single core dump).
Your kernel is v5.3, which is too old, so as Mike suggested, please try b4459b11e840 ("dm rq: don't queue request to blk-mq during DM suspend") given blk-mq has too many changes here.
IMO, this issue should happen in upstream kernel.
Thanks,
Ming
Quoted 40 lines · Ming Lei
Quoted 30 lines · Martin Wilck
What we know for sure is that there was a bad
dm_targetreference in
(structdm_rq_target_io*tio)->ti:crash> struct -x
dm_rq_target_ioc00000245ca90128structdm_rq_target_io{
md =0xc0000031c66a4000,
ti =0xc0080000020d0080<fscache_object_list_lock+665632>,crash> struct -x dm_target
0xc0080000020d0080structdm_targetstruct: invalid kernel virtual address:
c0080000020d0080 type: "gdb_readmem_callback"The question is how this could have come to pass. It can only happen
iftio->tihad been set before the map was reloaded.
My theory is that the IO had been dispatched before the queue had been
quiesced, like this:Task A Task B (dispatching IO) (executing a
DM_SUSPENDioctl to
resume afterDM_TABLE_LOAD)do_resume()dm_suspend()__dm_suspend()dm_mq_queue_rq()
structdm_target*ti =Quoted 8 lines
md->immutable_target;
dm_stop_queue()blk_mq_quiesce_queue()
/*
* At this point, the queue is quiesced, but task A
* has alreadyentereddm_mq_queue_rq()
*/
Quoted 3 lines
That shouldn't happen,
blk_mq_quiesce_queue()drains all pendingdm_mq_queue_rq()and prevents newdm_mq_queue_rq()from being called.
Thanks for pointing this out. I'd been missing the fact that the synchronization is achieved by the rcu_read_lock() in __blk_mq_run_dispatch_ops(), which guards invocations of the request dispatching code against the synchronize_rcu() in blk_mq_wait_quiesce_done(). In our old kernel it was still in hctx_lock(), but with the same effect.
This means that don't see any more how our dm_target reference could have pointed to freed memory. For now, we'll follow Mike's advice.
Thanks a lot,
Martin