linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] nvme: do not let the user delete a ctrl before a complete initialization
@ 2023-05-11 11:07 Maurizio Lombardi
  2023-05-12 15:03 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Maurizio Lombardi @ 2023-05-11 11:07 UTC (permalink / raw)
  To: linux-nvme; +Cc: axboe, sagi, hch, kbusch

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 <mlombard@redhat.com>
---
 drivers/nvme/host/core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index ccb6eb1282f8..1f0cbb77b249 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -3585,6 +3585,9 @@ static ssize_t nvme_sysfs_delete(struct device *dev,
 {
 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
 
+	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
+		return -EBUSY;
+
 	if (device_remove_file_self(dev, attr))
 		nvme_delete_ctrl_sync(ctrl);
 	return count;
@@ -5045,7 +5048,7 @@ void nvme_start_ctrl(struct nvme_ctrl *ctrl)
 	 * that were missed. We identify persistent discovery controllers by
 	 * checking that they started once before, hence are reconnecting back.
 	 */
-	if (test_and_set_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags) &&
+	if (test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags) &&
 	    nvme_discovery_ctrl(ctrl))
 		nvme_change_uevent(ctrl, "NVME_EVENT=rediscover");
 
@@ -5056,6 +5059,7 @@ void nvme_start_ctrl(struct nvme_ctrl *ctrl)
 	}
 
 	nvme_change_uevent(ctrl, "NVME_EVENT=connected");
+	set_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags);
 }
 EXPORT_SYMBOL_GPL(nvme_start_ctrl);
 
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme: do not let the user delete a ctrl before a complete initialization
  2023-05-11 11:07 [PATCH] nvme: do not let the user delete a ctrl before a complete initialization Maurizio Lombardi
@ 2023-05-12 15:03 ` Christoph Hellwig
  2023-05-17  7:09 ` Sagi Grimberg
  2023-05-17 14:59 ` Keith Busch
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2023-05-12 15:03 UTC (permalink / raw)
  To: Maurizio Lombardi; +Cc: linux-nvme, axboe, sagi, hch, kbusch

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme: do not let the user delete a ctrl before a complete initialization
  2023-05-11 11:07 [PATCH] nvme: do not let the user delete a ctrl before a complete initialization Maurizio Lombardi
  2023-05-12 15:03 ` Christoph Hellwig
@ 2023-05-17  7:09 ` Sagi Grimberg
  2023-05-17 14:59 ` Keith Busch
  2 siblings, 0 replies; 4+ messages in thread
From: Sagi Grimberg @ 2023-05-17  7:09 UTC (permalink / raw)
  To: Maurizio Lombardi, linux-nvme; +Cc: axboe, hch, kbusch

Reviewed-by: Sagi Grimberg <sagi@grimberg.me>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvme: do not let the user delete a ctrl before a complete initialization
  2023-05-11 11:07 [PATCH] nvme: do not let the user delete a ctrl before a complete initialization Maurizio Lombardi
  2023-05-12 15:03 ` Christoph Hellwig
  2023-05-17  7:09 ` Sagi Grimberg
@ 2023-05-17 14:59 ` Keith Busch
  2 siblings, 0 replies; 4+ messages in thread
From: Keith Busch @ 2023-05-17 14:59 UTC (permalink / raw)
  To: Maurizio Lombardi; +Cc: linux-nvme, axboe, sagi, hch

Thanks, applied to nvme-6.4.


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-17 14:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-11 11:07 [PATCH] nvme: do not let the user delete a ctrl before a complete initialization Maurizio Lombardi
2023-05-12 15:03 ` Christoph Hellwig
2023-05-17  7:09 ` Sagi Grimberg
2023-05-17 14:59 ` Keith Busch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).