Skip to contentSkip to conversation

nvme: add support to delay retrying aborted requests

PATCH In review Jiewei Ke 2 participants 6 messages Apr 10, 2025 – Apr 14, 2025 NVMe

lore
Share & tools
lore

Jiewei Ke

author
#0 lore raw

cover nvme: add support to delay retrying aborted requests

Hi all,

In this thread [1], we discussed a potential data corruption issue in NVMe multipath scenarios when retrying I/O during path failover. According to section 9.6 "Communication Loss Handling" of the NVMe Base Specification 2.1, to avoid such risks, the NVMe host must close the connection and wait for a sufficient period before retrying an aborted request. This ensures that the target has enough time to detect the connection loss and clean up any residual I/O.

With this patchset, the host will close the connection and wait for delay_io_retry_time before retrying the aborted I/O.

Comments and reviews are welcome.

[1] https://lore.kernel.org/r/[email protected]

Jiewei Ke (3):
nvme: check IO queues liveness during controller reconnection
nvme: add support to delay retrying aborted requests
nvme: export delay_io_retry_time to sysfs

 drivers/nvme/host/core.c      |  3 ++
 drivers/nvme/host/fabrics.c   | 23 ++++++++++++++-
 drivers/nvme/host/fabrics.h   |  3 ++
 drivers/nvme/host/multipath.c | 54 ++++++++++++++++++++++++++++++++---
 drivers/nvme/host/nvme.h      |  7 +++++
 drivers/nvme/host/rdma.c      | 27 +++++++++++++++++-
 drivers/nvme/host/sysfs.c     | 37 ++++++++++++++++++++++++
 drivers/nvme/host/tcp.c       | 27 +++++++++++++++++-
 8 files changed, 174 insertions(+), 7 deletions(-)
Signature · 1 line
2.36.0
author
#1 lore raw

PATCH 1/3 nvme: check IO queues liveness during controller reconnection

During the controller reconnection process, nvme_unquiesce_io_queues temporarily allows IO submissions. If any IO times out, nvme_[tcp|rdma]_complete_timed_out may close an individual IO queue. Due to timing gaps, nvme_wait_freeze_timeout might not yet have timed out, causing the controller to continue operating with some closed IO queues. This issue can be reproduced by injecting high IO latency and resetting the controller.

Add a check after nvme_wait_freeze_timeout to verify whether all IO queues are in the LIVE state. If any IO queue is not LIVE, the controller initialization will fail, and the controller will be cleaned up accordingly.

---
 drivers/nvme/host/rdma.c | 24 +++++++++++++++++++++++-
 drivers/nvme/host/tcp.c  | 24 +++++++++++++++++++++++-
 2 files changed, 46 insertions(+), 2 deletions(-)
drivers/nvme/host/rdma.c +23 -1
index b5a0295b5bf4..4297507725cf 100644
@@ -861,6 +861,23 @@ static int nvme_rdma_configure_admin_queue(struct nvme_rdma_ctrl *ctrl,
 	return error;
 }
 
+static bool nvme_rdma_check_io_queues_liveness(struct nvme_rdma_ctrl *ctrl,
+					       int first, int last)
+{
+	int i;
+	struct nvme_rdma_queue *queue;
+
+	for (i = first; i < last; i++) {
+		queue = &ctrl->queues[i];
+		if (!test_bit(NVME_RDMA_Q_LIVE, &queue->flags)) {
+			dev_warn(ctrl->ctrl.device, "I/O queue %d not live", i);
+			return false;
+		}
+	}
+
+	return true;
+}
+
 static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
 {
 	int ret, nr_queues;
@@ -888,11 +905,16 @@ static int nvme_rdma_configure_io_queues(struct nvme_rdma_ctrl *ctrl, bool new)
 	if (!new) {
 		nvme_start_freeze(&ctrl->ctrl);
 		nvme_unquiesce_io_queues(&ctrl->ctrl);
-		if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT)) {
+		if (!nvme_wait_freeze_timeout(&ctrl->ctrl, NVME_IO_TIMEOUT) ||
+		    !nvme_rdma_check_io_queues_liveness(ctrl, 1, nr_queues)) {
 			/*
 			 * If we timed out waiting for freeze we are likely to
 			 * be stuck.  Fail the controller initialization just
 			 * to be safe.
+			 * If any IO timed out during waiting for freeze, some IO
+			 * queue may be stopped by nvme_rdma_complete_timed_out.
+			 * Fail the controller initialization to avoid running
+			 * with missing IO queues.
 			 */
 			ret = -ENODEV;
 			nvme_unfreeze(&ctrl->ctrl);
drivers/nvme/host/tcp.c +23 -1
index 26c459f0198d..9109d5476417 100644
@@ -2152,6 +2152,23 @@ static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
 	return __nvme_tcp_alloc_io_queues(ctrl);
 }
 
+static bool nvme_tcp_check_io_queues_liveness(struct nvme_tcp_ctrl *ctrl,
+					      int first, int last)
+{
+	int i;
+	struct nvme_tcp_queue *queue;
+
+	for (i = first; i < last; i++) {
+		queue = &ctrl->queues[i];
+		if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags)) {
+			dev_warn(ctrl->ctrl.device, "I/O queue %d not live", i);
+			return false;
+		}
+	}
+
+	return true;
+}
+
 static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
 {
 	int ret, nr_queues;
@@ -2182,11 +2199,16 @@ static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
 	if (!new) {
 		nvme_start_freeze(ctrl);
 		nvme_unquiesce_io_queues(ctrl);
-		if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
+		if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT) ||
+		    !nvme_tcp_check_io_queues_liveness(to_tcp_ctrl(ctrl), 1, nr_queues)) {
 			/*
 			 * If we timed out waiting for freeze we are likely to
 			 * be stuck.  Fail the controller initialization just
 			 * to be safe.
+			 * If any IO timed out during waiting for freeze, some IO
+			 * queue may be stopped by nvme_tcp_complete_timed_out.
+			 * Fail the controller initialization to avoid running
+			 * with missing IO queues.
 			 */
 			ret = -ENODEV;
 			nvme_unfreeze(ctrl);
Signature · 1 line
2.36.0
author
#2 lore raw

PATCH 2/3 nvme: add support to delay retrying aborted requests

In reply to Jiewei Ke

According to section 9.6 "Communication Loss Handling" of the NVMe Base Specification 2.1, to avoid potential data corruption risks, the NVMe Host must close the connection and wait for a sufficient period before retrying an aborted request. This allows the Target to detect the connection loss and clean up any residual I/O.

Adds support for delaying the retry of aborted requests in nvme_tcp and nvme_rdma. The delay duration can be configured via the new "delay_io_retry_time" session parameter and may be passed during 'nvme connect'. The default value of 0 means no delay (similar to current behavior).

When delay_io_retry_time is set to a nonzero value, aborted requests will be added to the controller's retry_list. After closing the connection, nvme_delay_kick_retry_lists will be called to synchronously wait for the configured delay_io_retry_time before triggering a retry.

---
 drivers/nvme/host/core.c      |  3 ++
 drivers/nvme/host/fabrics.c   | 23 ++++++++++++++-
 drivers/nvme/host/fabrics.h   |  3 ++
 drivers/nvme/host/multipath.c | 54 ++++++++++++++++++++++++++++++++---
 drivers/nvme/host/nvme.h      |  7 +++++
 drivers/nvme/host/rdma.c      |  3 ++
 drivers/nvme/host/tcp.c       |  3 ++
 7 files changed, 91 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +3 -0
index 5ffc8f23a174..5a3cb019d6b8 100644
@@ -4852,6 +4852,9 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 	ctrl->ka_cmd.common.opcode = nvme_admin_keep_alive;
 	ctrl->ka_last_check_time = jiffies;
 
+	bio_list_init(&ctrl->retry_list);
+	spin_lock_init(&ctrl->retry_lock);
+
 	BUILD_BUG_ON(NVME_DSM_MAX_RANGES * sizeof(struct nvme_dsm_range) >
 			PAGE_SIZE);
 	ctrl->discard_page = alloc_page(GFP_KERNEL);
drivers/nvme/host/fabrics.c +22 -1
index 93e9041b9657..edb8d54b00c7 100644
@@ -709,6 +709,7 @@ static const match_table_t opt_tokens = {
 	{ NVMF_OPT_TLS,			"tls"			},
 	{ NVMF_OPT_CONCAT,		"concat"		},
 #endif
+	{ NVMF_OPT_DELAY_IO_RETRY_TIME, "delay_io_retry_time=%d" },
 	{ NVMF_OPT_ERR,			NULL			}
 };
 
@@ -738,6 +739,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 	opts->tls_key = NULL;
 	opts->keyring = NULL;
 	opts->concat = false;
+	opts->delay_io_retry_time = 0;
 
 	options = o = kstrdup(buf, GFP_KERNEL);
 	if (!options)
@@ -1064,6 +1066,25 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,
 			}
 			opts->concat = true;
 			break;
+		case NVMF_OPT_DELAY_IO_RETRY_TIME:
+			if (match_int(args, &token)) {
+				ret = -EINVAL;
+				goto out;
+			}
+			if (token < 0) {
+				pr_err("Invalid delay_io_retry_time %d\n", token);
+				ret = -EINVAL;
+				goto out;
+			}
+			if (!opts->transport ||
+			    (strcmp(opts->transport, "tcp") != 0 &&
+			     strcmp(opts->transport, "rdma") != 0)) {
+				pr_err("delay_io_retry_time is only supported for tcp and rdma\n");
+				ret = -EINVAL;
+				goto out;
+			}
+			opts->delay_io_retry_time = token;
+			break;
 		default:
 			pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n",
 				p);
@@ -1302,7 +1323,7 @@ EXPORT_SYMBOL_GPL(nvmf_free_options);
 				 NVMF_OPT_HOST_ID | NVMF_OPT_DUP_CONNECT |\
 				 NVMF_OPT_DISABLE_SQFLOW | NVMF_OPT_DISCOVERY |\
 				 NVMF_OPT_FAIL_FAST_TMO | NVMF_OPT_DHCHAP_SECRET |\
-				 NVMF_OPT_DHCHAP_CTRL_SECRET)
+				 NVMF_OPT_DHCHAP_CTRL_SECRET | NVMF_OPT_DELAY_IO_RETRY_TIME)
 
 static struct nvme_ctrl *
 nvmf_create_ctrl(struct device *dev, const char *buf)
drivers/nvme/host/fabrics.h +3 -0
index 9cf5b020adba..51c42d2d19af 100644
@@ -67,6 +67,7 @@ enum {
 	NVMF_OPT_KEYRING	= 1 << 26,
 	NVMF_OPT_TLS_KEY	= 1 << 27,
 	NVMF_OPT_CONCAT		= 1 << 28,
+	NVMF_OPT_DELAY_IO_RETRY_TIME = 1 << 29,
 };
 
 /**
@@ -110,6 +111,7 @@ enum {
  * @nr_poll_queues: number of queues for polling I/O
  * @tos: type of service
  * @fast_io_fail_tmo: Fast I/O fail timeout in seconds
+ * @delay_io_retry_time: Time to wait (in seconds) before retrying an aborted I/O request
  */
 struct nvmf_ctrl_options {
 	unsigned		mask;
@@ -140,6 +142,7 @@ struct nvmf_ctrl_options {
 	unsigned int		nr_poll_queues;
 	int			tos;
 	int			fast_io_fail_tmo;
+	int			delay_io_retry_time;
 };
 
 /*
drivers/nvme/host/multipath.c +50 -4

91 lines not on this page · raw message

drivers/nvme/host/nvme.h +7 -0
index 51e078642127..afb63e7d4c41 100644
@@ -410,6 +410,9 @@ struct nvme_ctrl {
 
 	enum nvme_ctrl_type cntrltype;
 	enum nvme_dctype dctype;
+
+	struct bio_list retry_list;
+	spinlock_t retry_lock;
 };
 
 static inline enum nvme_ctrl_state nvme_ctrl_state(struct nvme_ctrl *ctrl)
@@ -956,6 +959,7 @@ void nvme_mpath_start_freeze(struct nvme_subsystem *subsys);
 void nvme_mpath_default_iopolicy(struct nvme_subsystem *subsys);
 void nvme_failover_req(struct request *req);
 void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl);
+void nvme_delay_kick_retry_lists(struct nvme_ctrl *ctrl);
 int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,struct nvme_ns_head *head);
 void nvme_mpath_add_sysfs_link(struct nvme_ns_head *ns);
 void nvme_mpath_remove_sysfs_link(struct nvme_ns *ns);
@@ -1004,6 +1008,9 @@ static inline void nvme_failover_req(struct request *req)
 static inline void nvme_kick_requeue_lists(struct nvme_ctrl *ctrl)
 {
 }
+static inline void nvme_delay_kick_retry_lists(struct nvme_ctrl *ctrl)
+{
+}
 static inline int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl,
 		struct nvme_ns_head *head)
 {
drivers/nvme/host/rdma.c +3 -0
index 4297507725cf..1bcd7946dedc 100644
@@ -1114,6 +1114,7 @@ static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
 destroy_admin:
 	nvme_stop_keep_alive(&ctrl->ctrl);
 	nvme_rdma_teardown_admin_queue(ctrl, new);
+	nvme_delay_kick_retry_lists(&ctrl->ctrl);
 	return ret;
 }
 
@@ -1154,6 +1155,7 @@ static void nvme_rdma_error_recovery_work(struct work_struct *work)
 	nvme_rdma_teardown_admin_queue(ctrl, false);
 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
 	nvme_auth_stop(&ctrl->ctrl);
+	nvme_delay_kick_retry_lists(&ctrl->ctrl);
 
 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
 		/* state change failure is ok if we started ctrl delete */
@@ -2178,6 +2180,7 @@ static void nvme_rdma_shutdown_ctrl(struct nvme_rdma_ctrl *ctrl, bool shutdown)
 	nvme_quiesce_admin_queue(&ctrl->ctrl);
 	nvme_disable_ctrl(&ctrl->ctrl, shutdown);
 	nvme_rdma_teardown_admin_queue(ctrl, shutdown);
+	nvme_delay_kick_retry_lists(&ctrl->ctrl);
 }
 
 static void nvme_rdma_delete_ctrl(struct nvme_ctrl *ctrl)
drivers/nvme/host/tcp.c +3 -0
index 9109d5476417..f07b3960df7c 100644
@@ -2449,6 +2449,7 @@ static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
 destroy_admin:
 	nvme_stop_keep_alive(ctrl);
 	nvme_tcp_teardown_admin_queue(ctrl, new);
+	nvme_delay_kick_retry_lists(ctrl);
 	return ret;
 }
 
@@ -2494,6 +2495,7 @@ static void nvme_tcp_error_recovery_work(struct work_struct *work)
 	nvme_tcp_teardown_admin_queue(ctrl, false);
 	nvme_unquiesce_admin_queue(ctrl);
 	nvme_auth_stop(ctrl);
+	nvme_delay_kick_retry_lists(ctrl);
 
 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
 		/* state change failure is ok if we started ctrl delete */
@@ -2513,6 +2515,7 @@ static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
 	nvme_quiesce_admin_queue(ctrl);
 	nvme_disable_ctrl(ctrl, shutdown);
 	nvme_tcp_teardown_admin_queue(ctrl, shutdown);
+	nvme_delay_kick_retry_lists(ctrl);
 }
 
 static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
Signature · 1 line
2.36.0
author
#3 lore raw

PATCH 3/3 nvme: export delay_io_retry_time to sysfs

In reply to Jiewei Ke

Export the delay_io_retry_time value to user space via sysfs to allow runtime configuration.

---
 drivers/nvme/host/sysfs.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/nvme/host/sysfs.c +37 -0
index 6d31226f7a4f..d3fb39ea7728 100644
@@ -684,6 +684,40 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
 	nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
 #endif
 
+static ssize_t nvme_ctrl_delay_io_retry_time_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	if (ctrl->opts->delay_io_retry_time == 0)
+		return sysfs_emit(buf, "off\n");
+	return sysfs_emit(buf, "%d\n", ctrl->opts->delay_io_retry_time);
+}
+
+static ssize_t nvme_ctrl_delay_io_retry_time_store(struct device *dev,
+		struct device_attribute *attr, const char *buf, size_t count)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+	struct nvmf_ctrl_options *opts = ctrl->opts;
+	int delay_io_retry_time, err;
+
+	err = kstrtoint(buf, 10, &delay_io_retry_time);
+	if (err)
+		return -EINVAL;
+
+	if (strcmp(opts->transport, "tcp") != 0 && strcmp(opts->transport, "rdma") != 0)
+		return -EINVAL;
+
+	if (delay_io_retry_time < 0)
+		opts->delay_io_retry_time = 0;
+	else
+		opts->delay_io_retry_time = delay_io_retry_time;
+	return count;
+}
+
+static DEVICE_ATTR(delay_io_retry_time, S_IRUGO | S_IWUSR,
+	nvme_ctrl_delay_io_retry_time_show, nvme_ctrl_delay_io_retry_time_store);
+
 static struct attribute *nvme_dev_attrs[] = {
 	&dev_attr_reset_controller.attr,
 	&dev_attr_rescan_controller.attr,
@@ -712,6 +746,7 @@ static struct attribute *nvme_dev_attrs[] = {
 	&dev_attr_dhchap_ctrl_secret.attr,
 #endif
 	&dev_attr_adm_passthru_err_log_enabled.attr,
+	&dev_attr_delay_io_retry_time.attr,
 	NULL
 };
 
@@ -741,6 +776,8 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
 	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
 		return 0;
 #endif
+	if (a == &dev_attr_delay_io_retry_time.attr && !ctrl->opts)
+		return 0;
 
 	return a->mode;
 }
Signature · 1 line
2.36.0
author
#5 lore raw

Hi Sagi,

Yes, the second patch, "[PATCH 2/3] nvme: add support to delay retrying aborted requests," aims to address the same issue as Daniel's patch. However, there are some differences in the implementation. I’ve listed these differences under Daniel’s patchset for further discussion, see [1], and I welcome more input from others as well.

In addition, feel free to skip reviewing the first patch, "[PATCH 1/3] nvme: check IO queues liveness during controller reconnection”. It may not be necessary. Maurizio's patch [2] can resolve the issue and also cover a broader range of scenarios.

[1] https://lore.kernel.org/linux-nvme/[email protected]/T/#m727bfbd968d40ba007549ffa1445f6d8f92e51e4
[2] https://lore.kernel.org/linux-nvme/[email protected]/T/#mb34c7bfec63dbccc207425af9a516ec308d202b1

Thanks,
Jiewei

Quoted 5 lines

2025年4月14日 06:06,Sagi Grimberg <[email protected]> 写道:

Hey,

Is this relevant given the patchset from Daniel on implementing CQT?