Skip to content

Commit 2eb94dd

Browse files
maurizio-lombardikeithbusch
authored andcommitted
nvme: do not let the user delete a ctrl before a complete initialization
If a userspace application performes a "delete_controller" command early during the ctrl initialization, the delete operation may race against the init code and the kernel will crash. nvme nvme5: Connect command failed: host path error nvme nvme5: failed to connect queue: 0 ret=880 PF: supervisor write access in kernel mode PF: error_code(0x0002) - not-present page blk_mq_quiesce_queue+0x18/0x90 nvme_tcp_delete_ctrl+0x24/0x40 [nvme_tcp] nvme_do_delete_ctrl+0x7f/0x8b [nvme_core] nvme_sysfs_delete.cold+0x8/0xd [nvme_core] kernfs_fop_write_iter+0x124/0x1b0 new_sync_write+0xff/0x190 vfs_write+0x1ef/0x280 Fix the crash by checking the NVME_CTRL_STARTED_ONCE bit; if it's not set it means that the nvme controller is still in the process of getting initialized and the kernel will return an -EBUSY error to userspace. Set the NVME_CTRL_STARTED_ONCE later in the nvme_start_ctrl() function, after the controller start operation is completed. Signed-off-by: Maurizio Lombardi <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 1743e5f commit 2eb94dd

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

drivers/nvme/host/core.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3574,6 +3574,9 @@ static ssize_t nvme_sysfs_delete(struct device *dev,
35743574
{
35753575
struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
35763576

3577+
if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
3578+
return -EBUSY;
3579+
35773580
if (device_remove_file_self(dev, attr))
35783581
nvme_delete_ctrl_sync(ctrl);
35793582
return count;
@@ -5034,7 +5037,7 @@ void nvme_start_ctrl(struct nvme_ctrl *ctrl)
50345037
* that were missed. We identify persistent discovery controllers by
50355038
* checking that they started once before, hence are reconnecting back.
50365039
*/
5037-
if (test_and_set_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags) &&
5040+
if (test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags) &&
50385041
nvme_discovery_ctrl(ctrl))
50395042
nvme_change_uevent(ctrl, "NVME_EVENT=rediscover");
50405043

@@ -5045,6 +5048,7 @@ void nvme_start_ctrl(struct nvme_ctrl *ctrl)
50455048
}
50465049

50475050
nvme_change_uevent(ctrl, "NVME_EVENT=connected");
5051+
set_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags);
50485052
}
50495053
EXPORT_SYMBOL_GPL(nvme_start_ctrl);
50505054

0 commit comments

Comments
 (0)