Skip to content

Commit 10d77a8

Browse files
ps-ushankaraxboe
authored andcommitted
ublk: introduce and use ublk_set_canceling helper
For performance reasons (minimizing the number of cache lines accessed in the hot path), we store the "canceling" state redundantly - there is one flag in the device, which can be considered the source of truth, and per-queue copies of that flag. This redundancy can cause confusion, and opens the door to bugs where the state is set inconsistently. Try to guard against these bugs by introducing a ublk_set_canceling helper which is the sole mutator of both the per-device and per-queue canceling state. This helper always sets the state consistently. Use the helper in all places where we need to modify the canceling state. No functional changes are expected. Signed-off-by: Uday Shankar <[email protected]> Reviewed-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 2fa9c93 commit 10d77a8

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,27 @@ static void ublk_put_disk(struct gendisk *disk)
15631563
put_device(disk_to_dev(disk));
15641564
}
15651565

1566+
/*
1567+
* Use this function to ensure that ->canceling is consistently set for
1568+
* the device and all queues. Do not set these flags directly.
1569+
*
1570+
* Caller must ensure that:
1571+
* - cancel_mutex is held. This ensures that there is no concurrent
1572+
* access to ub->canceling and no concurrent writes to ubq->canceling.
1573+
* - there are no concurrent reads of ubq->canceling from the queue_rq
1574+
* path. This can be done by quiescing the queue, or through other
1575+
* means.
1576+
*/
1577+
static void ublk_set_canceling(struct ublk_device *ub, bool canceling)
1578+
__must_hold(&ub->cancel_mutex)
1579+
{
1580+
int i;
1581+
1582+
ub->canceling = canceling;
1583+
for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1584+
ublk_get_queue(ub, i)->canceling = canceling;
1585+
}
1586+
15661587
static int ublk_ch_release(struct inode *inode, struct file *filp)
15671588
{
15681589
struct ublk_device *ub = filp->private_data;
@@ -1591,13 +1612,11 @@ static int ublk_ch_release(struct inode *inode, struct file *filp)
15911612
* All requests may be inflight, so ->canceling may not be set, set
15921613
* it now.
15931614
*/
1594-
ub->canceling = true;
1595-
for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
1596-
struct ublk_queue *ubq = ublk_get_queue(ub, i);
1597-
1598-
ubq->canceling = true;
1599-
ublk_abort_queue(ub, ubq);
1600-
}
1615+
mutex_lock(&ub->cancel_mutex);
1616+
ublk_set_canceling(ub, true);
1617+
for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1618+
ublk_abort_queue(ub, ublk_get_queue(ub, i));
1619+
mutex_unlock(&ub->cancel_mutex);
16011620
blk_mq_kick_requeue_list(disk->queue);
16021621

16031622
/*
@@ -1723,7 +1742,6 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
17231742
static void ublk_start_cancel(struct ublk_device *ub)
17241743
{
17251744
struct gendisk *disk = ublk_get_disk(ub);
1726-
int i;
17271745

17281746
/* Our disk has been dead */
17291747
if (!disk)
@@ -1740,9 +1758,7 @@ static void ublk_start_cancel(struct ublk_device *ub)
17401758
* touch completed uring_cmd
17411759
*/
17421760
blk_mq_quiesce_queue(disk->queue);
1743-
ub->canceling = true;
1744-
for (i = 0; i < ub->dev_info.nr_hw_queues; i++)
1745-
ublk_get_queue(ub, i)->canceling = true;
1761+
ublk_set_canceling(ub, true);
17461762
blk_mq_unquiesce_queue(disk->queue);
17471763
out:
17481764
mutex_unlock(&ub->cancel_mutex);
@@ -1942,10 +1958,11 @@ static void ublk_reset_io_flags(struct ublk_device *ub)
19421958
for (j = 0; j < ubq->q_depth; j++)
19431959
ubq->ios[j].flags &= ~UBLK_IO_FLAG_CANCELED;
19441960
spin_unlock(&ubq->cancel_lock);
1945-
ubq->canceling = false;
19461961
ubq->fail_io = false;
19471962
}
1948-
ub->canceling = false;
1963+
mutex_lock(&ub->cancel_mutex);
1964+
ublk_set_canceling(ub, false);
1965+
mutex_unlock(&ub->cancel_mutex);
19491966
}
19501967

19511968
/* device can only be started after all IOs are ready */
@@ -3417,7 +3434,7 @@ static int ublk_ctrl_quiesce_dev(struct ublk_device *ub,
34173434
/* zero means wait forever */
34183435
u64 timeout_ms = header->data[0];
34193436
struct gendisk *disk;
3420-
int i, ret = -ENODEV;
3437+
int ret = -ENODEV;
34213438

34223439
if (!(ub->dev_info.flags & UBLK_F_QUIESCE))
34233440
return -EOPNOTSUPP;
@@ -3435,14 +3452,11 @@ static int ublk_ctrl_quiesce_dev(struct ublk_device *ub,
34353452
goto put_disk;
34363453

34373454
/* Mark the device as canceling */
3455+
mutex_lock(&ub->cancel_mutex);
34383456
blk_mq_quiesce_queue(disk->queue);
3439-
ub->canceling = true;
3440-
for (i = 0; i < ub->dev_info.nr_hw_queues; i++) {
3441-
struct ublk_queue *ubq = ublk_get_queue(ub, i);
3442-
3443-
ubq->canceling = true;
3444-
}
3457+
ublk_set_canceling(ub, true);
34453458
blk_mq_unquiesce_queue(disk->queue);
3459+
mutex_unlock(&ub->cancel_mutex);
34463460

34473461
if (!timeout_ms)
34483462
timeout_ms = UINT_MAX;

0 commit comments

Comments
 (0)