@@ -1683,14 +1683,31 @@ static void ublk_start_cancel(struct ublk_queue *ubq)
1683
1683
ublk_put_disk (disk );
1684
1684
}
1685
1685
1686
- static void ublk_cancel_cmd (struct ublk_queue * ubq , struct ublk_io * io ,
1686
+ static void ublk_cancel_cmd (struct ublk_queue * ubq , unsigned tag ,
1687
1687
unsigned int issue_flags )
1688
1688
{
1689
+ struct ublk_io * io = & ubq -> ios [tag ];
1690
+ struct ublk_device * ub = ubq -> dev ;
1691
+ struct request * req ;
1689
1692
bool done ;
1690
1693
1691
1694
if (!(io -> flags & UBLK_IO_FLAG_ACTIVE ))
1692
1695
return ;
1693
1696
1697
+ /*
1698
+ * Don't try to cancel this command if the request is started for
1699
+ * avoiding race between io_uring_cmd_done() and
1700
+ * io_uring_cmd_complete_in_task().
1701
+ *
1702
+ * Either the started request will be aborted via __ublk_abort_rq(),
1703
+ * then this uring_cmd is canceled next time, or it will be done in
1704
+ * task work function ublk_dispatch_req() because io_uring guarantees
1705
+ * that ublk_dispatch_req() is always called
1706
+ */
1707
+ req = blk_mq_tag_to_rq (ub -> tag_set .tags [ubq -> q_id ], tag );
1708
+ if (req && blk_mq_request_started (req ))
1709
+ return ;
1710
+
1694
1711
spin_lock (& ubq -> cancel_lock );
1695
1712
done = !!(io -> flags & UBLK_IO_FLAG_CANCELED );
1696
1713
if (!done )
@@ -1722,7 +1739,6 @@ static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd *cmd,
1722
1739
struct ublk_uring_cmd_pdu * pdu = ublk_get_uring_cmd_pdu (cmd );
1723
1740
struct ublk_queue * ubq = pdu -> ubq ;
1724
1741
struct task_struct * task ;
1725
- struct ublk_io * io ;
1726
1742
1727
1743
if (WARN_ON_ONCE (!ubq ))
1728
1744
return ;
@@ -1737,9 +1753,8 @@ static void ublk_uring_cmd_cancel_fn(struct io_uring_cmd *cmd,
1737
1753
if (!ubq -> canceling )
1738
1754
ublk_start_cancel (ubq );
1739
1755
1740
- io = & ubq -> ios [pdu -> tag ];
1741
- WARN_ON_ONCE (io -> cmd != cmd );
1742
- ublk_cancel_cmd (ubq , io , issue_flags );
1756
+ WARN_ON_ONCE (ubq -> ios [pdu -> tag ].cmd != cmd );
1757
+ ublk_cancel_cmd (ubq , pdu -> tag , issue_flags );
1743
1758
}
1744
1759
1745
1760
static inline bool ublk_queue_ready (struct ublk_queue * ubq )
@@ -1752,7 +1767,7 @@ static void ublk_cancel_queue(struct ublk_queue *ubq)
1752
1767
int i ;
1753
1768
1754
1769
for (i = 0 ; i < ubq -> q_depth ; i ++ )
1755
- ublk_cancel_cmd (ubq , & ubq -> ios [ i ] , IO_URING_F_UNLOCKED );
1770
+ ublk_cancel_cmd (ubq , i , IO_URING_F_UNLOCKED );
1756
1771
}
1757
1772
1758
1773
/* Cancel all pending commands, must be called after del_gendisk() returns */
0 commit comments