memstick: fixes for mspro_block and ms_block
Subsystems (2)
cover memstick: fixes for mspro_block and ms_block
This series fixes bugs that I found in the memstick core while writing the bcm577x5_ms host driver for the Broadcom BCM57765/57785 card reader.
Some of these bugs were really rare, so I used Claude to automatically fuzz access patterns/events to reproduce some of these issues.
Should I put this with Cc: stable?
Ho Jie Feng (5):
memstick: mspro_block: don't hand a finished request back to the host
memstick: mspro_block: wait for running dispatches in
mspro_block_stop()
memstick: ms_block: don't erase the old block of an unmapped LBA
memstick: ms_block: keep the power off during a full msb_reset()
memstick: ms_block: add the missing newline to the parallel switch
error
drivers/memstick/core/ms_block.c | 11 ++++++++--- drivers/memstick/core/mspro_block.c | 9 ++++++--- 2 files changed, 14 insertions(+), 6 deletions(-)
base-commit: 461294b587f6fa5bb17d5856e4404b67d666086d
Signature · 1 line
2.56.0
PATCH 1/5 memstick: mspro_block: don't hand a finished request back to the host
When a block request ends, mspro_block_complete_req() clears msb->block_req, completes card->mrq_complete and returns 0. Host drivers take a 0 return from memstick_next_req() to mean that *mrq holds a new request, so the host issues card->current_mrq again even though it has already finished.
At the same time, msb->block_req being NULL allows mspro_queue_rq() to pass the check and queue another request, causing a race condition that rewrites card->current_mrq while the host is executing it.
Return -EAGAIN instead, as the function already does when there is no block request.
--- drivers/memstick/core/mspro_block.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/memstick/core/mspro_block.c +2 -0
@@ -706,6 +706,8 @@ static int mspro_block_complete_req(struct memstick_dev *card, int error)__blk_mq_end_request (msb->block_req,errno_to_blk_status (error)); msb->block_req =NULL ; +if (!error) + error = -EAGAIN ; } }else {if (!error)
Signature · 1 line
2.56.0
PATCH 2/5 memstick: mspro_block: wait for running dispatches in mspro_block_stop()
In reply to Ho Jie Fengmspro_block_stop() only calls blk_mq_stop_hw_queues() once it sees that msb->block_req is NULL. That marks the queues stopped so that no new dispatch starts, but does not wait for dispatches that are already running. In this case, a request can start just after mspro_block_stop() has returned, causing a race condition.
Stop the queues before checking msb->block_req, and use blk_mq_wait_quiesce_done() to wait for running ->queue_rq() calls to return. The existing loop then waits for the current request to finish before mspro_block_stop() returns.
--- drivers/memstick/core/mspro_block.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/memstick/core/mspro_block.c +4 -3
@@ -727,12 +727,13 @@ static void mspro_block_stop(struct memstick_dev *card)int rc =0 ;unsigned long flags; +blk_mq_stop_hw_queues (msb->queue); +blk_mq_wait_quiesce_done (&msb->tag_set); +while (1 ) {spin_lock_irqsave (&msb->q_lock, flags); -if (!msb->block_req) { -blk_mq_stop_hw_queues (msb->queue); +if (!msb->block_req) rc =1 ; - }spin_unlock_irqrestore (&msb->q_lock, flags);if (rc)
Signature · 1 line
2.56.0
PATCH 3/5 memstick: ms_block: don't erase the old block of an unmapped LBA
In reply to Ho Jie Fengmsb_update_block() checks for MS_BLOCK_INVALID before it sets the update flag on the old block, but then erases the old block unconditionally. For an LBA without a block, this tries to erase block 65535.
msb_erase_block() then calls msb_mark_block_unused() and sets the bit in erased_blocks_bitmap with pba 65535, so used_blocks_bitmap, free_block_count[] and erased_blocks_bitmap are accessed out of bounds.
UBSAN reported free_block_count[127] on the int[16] array.
Only erase the old block if there is one.
--- drivers/memstick/core/ms_block.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/memstick/core/ms_block.c +4 -2
@@ -1149,8 +1149,10 @@ static int msb_update_block(struct msb_data *msb, u16 lba,if (error) goto out; -dbg_verbose ("block update: erasing the old block" ); -msb_erase_block (msb, pba); +if (pba !=MS_BLOCK_INVALID ) { +dbg_verbose ("block update: erasing the old block" ); +msb_erase_block (msb, pba); + } msb->lba_to_pba_table[lba] = new_pba;return 0 ; }
Signature · 1 line
2.56.0
PATCH 4/5 memstick: ms_block: keep the power off during a full msb_reset()
In reply to Ho Jie FengKeep the power off for long enough so that the card properly power cycles.
--- drivers/memstick/core/ms_block.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/memstick/core/ms_block.c +3 -0
@@ -740,6 +740,9 @@ static int msb_reset(struct msb_data *msb, bool full)msb_invalidate_reg_window (msb); +/* Allow enough time for the card to fully power off */ +msleep (20 ); + error = host->set_param (host,MEMSTICK_POWER ,MEMSTICK_POWER_ON );if (error)
Signature · 1 line
2.56.0
Re: [PATCH 4/5] memstick: ms_block: keep the power off during a full msb_reset()
Quoted 15 lines · Ho Jie Feng
Keep the power off for long enough so that the card properly power cycles.
Fixes 0ab30494bc4f ("memstick: add support for legacy memorysticks")Signed-off-by Ho Jie Feng <[email protected]>--- drivers/memstick/core/ms_block.c | 3 +++ 1 file changed, 3 insertions(+)drivers/memstick/core/ms_block.c +0
-0
drivers/memstick/core/ms_block.c static int msb_reset(struct msb_data *msb, bool full) · line 745
Quoted 6 lines
drivers/memstick/core/ms_block.c +3
-0@@ -740,6 +740,9 @@ static int msb_reset(struct msb_data *msb, bool full)msb_invalidate_reg_window (msb); +/* Allow enough time for the card to fully power off */ +msleep (20 ); +
Why 20ms?
Quoted 6 lines
drivers/memstick/core/ms_block.c +0
-1error = host->set_param (host,MEMSTICK_POWER ,MEMSTICK_POWER_ON );if (error) --2.56.0
Kind regards
Uffe
Re: [PATCH 4/5] memstick: ms_block: keep the power off during a full msb_reset()
Quoted 18 lines · Ulf Hansson
Quoted 16 lines · Ho Jie Feng
Keep the power off for long enough so that the card properly power cycles.
Fixes 0ab30494bc4f ("memstick: add support for legacy memorysticks")Signed-off-by Ho Jie Feng <[email protected]>--- drivers/memstick/core/ms_block.c | 3 +++ 1 file changed, 3 insertions(+)drivers/memstick/core/ms_block.c +0
-0@@ -740,6 +740,9 @@ static int msb_reset(struct msb_data *msb, bool full)
Quoted 7 lines
drivers/memstick/core/ms_block.c static int msb_reset(struct msb_data *msb, bool full) · line 745
Quoted 5 lines
drivers/memstick/core/ms_block.c +3
-0msb_invalidate_reg_window (msb); +/* Allow enough time for the card to fully power off */ +msleep (20 ); +Why 20ms?
It is just a random number I picked. I can change it to mspro_block.c 10ms:
https://elixir.bootlin.com/linux/v7.4-rc2/source/drivers/memstick/core/mspro_block.c#L867
Quoted 9 lines
Quoted 6 lines
drivers/memstick/core/ms_block.c +0
-1error = host->set_param (host,MEMSTICK_POWER ,MEMSTICK_POWER_ON );if (error) --2.56.0
Kind regards
Uffe
Best Regards,
Jie Feng
Re: [PATCH 4/5] memstick: ms_block: keep the power off during a full msb_reset()
Quoted 22 lines · Jie Feng
Quoted 20 lines · Ulf Hansson
Quoted 18 lines · Ho Jie Feng
Keep the power off for long enough so that the card properly power cycles.
Fixes 0ab30494bc4f ("memstick: add support for legacy memorysticks")Signed-off-by Ho Jie Feng <[email protected]>--- drivers/memstick/core/ms_block.c | 3 +++ 1 file changed, 3 insertions(+)drivers/memstick/core/ms_block.c +0
-0@@ -740,6 +740,9 @@ static int msb_reset(struct msb_data *msb, bool full)msb_invalidate_reg_window (msb);
Quoted 7 lines
Quoted 5 lines
drivers/memstick/core/ms_block.c static int msb_reset(struct msb_data *msb, bool full) · line 745
Quoted 3 lines
drivers/memstick/core/ms_block.c +3
-0+/* Allow enough time for the card to fully power off */ +msleep (20 ); +Why 20ms?
It is just a random number I picked. I can change it to
mspro_block.c10ms:
https://elixir.bootlin.com/linux/v7.4-rc2/source/drivers/memstick/core/mspro_block.c#L867
Okay, in that case let's at least make it a define, so it can be used at both places.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/memstick/core/ms_block.c +0
-1error = host->set_param (host,MEMSTICK_POWER ,MEMSTICK_POWER_ON );if (error) --2.56.0
Kind regards
Uffe
PATCH 5/5 memstick: ms_block: add the missing newline to the parallel switch error
In reply to Ho Jie FengAllow printk to finalize the record immediately instead of waiting for the next message with a newline.
--- drivers/memstick/core/ms_block.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/memstick/core/ms_block.c +1 -1
@@ -778,7 +778,7 @@ static int msb_switch_to_parallel(struct msb_data *msb) error =msb_run_state_machine (msb, h_msb_parallel_switch);if (error) { -pr_err ("Switch to parallel failed" ); +pr_err ("Switch to parallel failed\n" ); msb->regs.param.system &= ~MEMSTICK_SYS_PAM ;msb_reset (msb, true);return -EFAULT ;
Signature · 1 line
2.56.0
Quoted 18 lines · Ho Jie Feng
This series fixes bugs that I found in the memstick core while writing the
bcm577x5_mshost driver for the Broadcom BCM57765/57785 card reader.Some of these bugs were really rare, so I used Claude to automatically fuzz access patterns/events to reproduce some of these issues.
Should I put this with Cc: stable?
Ho Jie Feng (5):
memstick:mspro_block: don't hand a finished request back to the host
memstick:mspro_block: wait for running dispatches in
mspro_block_stop()
memstick:ms_block: don't erase the old block of an unmapped LBA
memstick:ms_block: keep the power off during a fullmsb_reset()
memstick:ms_block: add the missing newline to the parallel switch
error
Quoted 9 lines
drivers/memstick/core/ms_block.c | 11 ++++++++--- drivers/memstick/core/mspro_block.c | 9 ++++++--- 2 files changed, 14 insertions(+), 6 deletions(-)base-commit:
461294b587f6fa5bb17d5856e4404b67d666086d--
2.56.0
Patch 1->3 applied for fixes and by adding stable tags, thanks!
Deferring patch 4-5 for a new version.
Kind regards
Uffe