Skip to contentSkip to conversation

TP8028 Rapid Path Failure Recovery

PATCH Superseded Mohamed Khalfella 3 participants 74 messages Sep 20 – Nov 3

Subsystems (3)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 cover TP8028 Rapid Path Failure Recovery

This patchset adds support for TP8028 Rapid Path Failure Recovery for both the nvme target and initiator. Rapid Path Failure Recovery brings Cross-Controller Reset (CCR) functionality to nvme. This allows an nvme host to send an nvme command to a source nvme controller to reset the impacted nvme controller, provided that both source and impacted controllers are in the same nvme subsystem.

The main use of CCR is when one path to the nvme subsystem fails. Inflight IOs on the impacted nvme controller need to be terminated first before they can be retried on another path. Otherwise, data corruption may happen. CCR provides a quick way to terminate these IOs on the unreachable nvme controller, allowing recovery to move quickly and avoid unnecessary delays. In case CCR is not possible, inflight requests are held for a duration defined by TP4129 KATO Corrections and Clarifications before they are allowed to be retried.

On the target side:

* New struct members have been added to support CCR. struct nvme_id_ctrl has been updated with CIU (Controller Instance Uniquifier), CIRN (Controller Instance Random Number), and CQT (Command Quiesce Time). The combination of CIU, CNTLID, and CIRN is used to identify the impacted controller in the CCR command.

* The CCR nvme command implemented on the target causes the impacted controller to fail and drop its connections to the host.

* The CCR log page contains the status of pending CCR requests. An entry is added to the log page after a CCR request is validated. Completed CCR requests are removed from the log page when the controller becomes ready or when requested in the Get Log Page command.

* An AEN is sent when a CCR completes to let the host know that it is safe to retry inflight requests.

On the host side:

* CIU, CIRN, and CQT have been added to struct nvme_ctrl. CIU and CIRN have been added to sysfs to make the values visible to the user. CIU and CIRN can be used to construct and manually send admin-passthru CCR commands.

* New controller states FENCING and FENCED have been added to make sure that inflight requests do not get canceled if they time out during the fencing process. FENCED exists so that the controller state machine does not have a transition from FENCING to RESETTING. Instead, FENCING -> FENCED -> RESETTING. This prevents a controller being fenced from getting reset. Only after fencing finishes is the impacted controller reset.

* Controller recovery in nvme_fence_ctrl() is invoked when a LIVE controller hits an error or when a request times out. CCR is attempted first to reset the impacted controller. If it fails, inflight requests are held until it is safe to retry them.

* Updated the nvme fabric transports nvme-tcp, nvme-rdma, and nvme-fc to use CCR recovery.

* Controller deletion now waits for an active fencing window to end instead of failing, so a sysfs disconnect, rdma device removal, or module unload during fencing no longer drops the deletion or leaks the controller.

Ideally, all inflight requests should be held during controller recovery and only retried after recovery is done. However, there are known situations where that is not the case in this implementation. These gaps will be addressed in future patches:

* A manual controller reset from sysfs of a LIVE controller will result in the controller going to the RESETTING state and all inflight requests being canceled immediately, and they may be retried on another path. A reset issued during a fencing window is rejected by the state machine.

* A manual controller delete from sysfs of a LIVE controller will also result in all inflight requests being canceled immediately, and they may be retried on another path. A delete issued during a fencing window now waits for fencing to end instead of being dropped.

* In nvme-fc, the nvme controller will be deleted if the remote port disappears with no timeout specified. For a LIVE controller this still results in immediate cancellation of requests that may be retried on another path. If the controller is already fencing, the association is torn down without completing the held requests and they are only allowed to fail over once fencing ends.

* In nvme-rdma, if the HCA is removed, all nvme controllers will be deleted. Deleting LIVE controllers still cancels inflight IOs, and they may be retried on another path. Controllers in a fencing window are now deleted only after fencing ends.

Changes from v5:

- nvme: Introduce FENCING and FENCED controller states
- Treat FENCING/FENCED controllers as available paths in nvme_available_path() so a multipath head does not fail all IO while its last path is being fenced

- nvme-fc: Refactor IO error recovery
- Split into two patches, "nvme-fc: start error recovery instead of aborting timed out IOs" and "nvme-fc: perform error recovery directly from ioerr_work"
- nvme_fc_start_ioerr_recovery() queues ioerr_work directly in DELETING/DELETING_NOIO so that a dead target does not hang controller deletion
- nvme_fc_ctrl_ioerr_work() claims RESETTING before tearing the association down and skips recovery when another state owns it
- nvme_fc_reset_ctrl_work() tears the association down before nvme_stop_ctrl() so that flushing ana_work or fw_act_work does not get stuck waiting on IOs that never complete

- nvme-fc: Use CCR to recover controller that hits an error
- Tear the association down at the start of fencing_work, releasing all LLDD resources as soon as the controller enters FENCING. This fixes a use-after-free followed by a panic when the LLDD is unloaded or shut down (e.g. lpfc during kexec) while a fencing window is running: the LLDD's bounded unload waits expire before the fence does, its resources are freed, and the post-fence remoteport_delete upcall lands on freed memory
- Stop keep-alive and cancel async_event_work before the teardown. AER submission bypasses blk-mq and must not reach the LLDD after the hw queues are deleted. cancel_work_sync() is used instead of flush_work() because fencing_work runs on nvme_wq, the same rescuer-equipped workqueue async_event_work is queued on

- nvme-fc: Hold inflight requests while in FENCING state
- Split nvme_fc_delete_association() into __nvme_fc_teardown_association() and nvme_fc_flush_held_requests(). fencing_work now runs only the teardown at fence start and the held requests are completed on the FENCING -> FENCED transition, so they can fail over only after CCR succeeds or time-based recovery ends
- Complete the held requests while still in FENCING, before moving to FENCED, so an io timeout cannot claim FENCED -> RESETTING and start reconnecting while the flush is running

- nvme: Add support for CQT to nvme host
- nvme-fc: complete the held requests in fenced_work when time-based recovery finishes, matching fencing_work
- Dropped the Reviewed-by tags due to the above change

- New patch "nvme: let controller deletion wait out a fencing window"
- DELETING is not reachable from FENCING or FENCED, so during a fencing window nvme_delete_ctrl() fails with -EBUSY and its callers silently lose the deletion: a sysfs disconnect is dropped, rdma device removal returns early, and module unload leaks live controllers. Add nvme_delete_ctrl_wait(), use it in the tcp/rdma module exit paths and rdma device removal, and make nvme_delete_ctrl_sync() wait the same way

v5: https://lore.kernel.org/all/[email protected]/

Mohamed Khalfella (18):
nvmet: Rapid Path Failure Recovery set controller identify fields nvmet/debugfs: Export controller CIU and CIRN via debugfs
nvmet: Implement CCR nvme command
nvmet: Implement CCR logpage
nvmet: Send an AEN on CCR completion
nvme: Rapid Path Failure Recovery read controller identify fields
nvme: Introduce FENCING and FENCED controller states
nvme: Implement cross-controller reset recovery
nvme: Implement cross-controller reset completion
nvme-tcp: Use CCR to recover controller that hits an error
nvme-rdma: Use CCR to recover controller that hits an error
nvme-fc: start error recovery instead of aborting timed out IOs
nvme-fc: perform error recovery directly from ioerr_work
nvme-fc: Use CCR to recover controller that hits an error
nvme-fc: Hold inflight requests while in FENCING state
nvmet: Add support for CQT to nvme target
nvme: Add support for CQT to nvme host
nvme: let controller deletion wait out a fencing window

 drivers/nvme/host/constants.c   |   1 +
 drivers/nvme/host/core.c        | 275 +++++++++++++++++++++++++-
 drivers/nvme/host/fc.c          | 333 +++++++++++++++++++++++++-------
 drivers/nvme/host/multipath.c   |   2 +
 drivers/nvme/host/nvme.h        |  27 +++
 drivers/nvme/host/rdma.c        |  61 +++++-
 drivers/nvme/host/sysfs.c       |  27 +++
 drivers/nvme/host/tcp.c         |  59 +++++-
 drivers/nvme/target/admin-cmd.c | 126 ++++++++++++
 drivers/nvme/target/configfs.c  |  36 ++++
 drivers/nvme/target/core.c      | 115 ++++++++++-
 drivers/nvme/target/debugfs.c   |  21 ++
 drivers/nvme/target/nvmet.h     |  20 +-
 include/linux/nvme.h            |  70 ++++++-
 14 files changed, 1083 insertions(+), 90 deletions(-)

base-commit: fd9beb8870736e1c6a0b2351d88a161aaeb2b326

Signature · 1 line
2.55.0
#1 lore raw

PATCH v6 1/18 nvmet: Rapid Path Failure Recovery set controller identify fields

TP8028 Rapid Path Failure Recovery defined new fields in controller identify response. The newly defined fields are:

- CIU (Controller Instance Uniquifier): is an 8bit non-zero value that is assigned a random value when controller is first created. The value will be incremented when RDY bit in CSTS register is asserted.
- CIRN (Controller Instance Random Number): is 64bit random value that gets generated when controller is created. CIRN is regenerated everytime RDY bit in CSTS register is asserted.
- CCRL (Cross-Controller Reset Limit): is an 8bit value that defines the maximum number of in-progress controller reset operations. CCRL is hardcoded to 4 as recommended by TP8028.

These fields are set for non-discovery subsystems only.

---
 drivers/nvme/target/admin-cmd.c |  5 +++++
 drivers/nvme/target/core.c      |  9 +++++++++
 drivers/nvme/target/nvmet.h     |  2 ++
 include/linux/nvme.h            | 10 ++++++++--
 4 files changed, 24 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +5 -0
index 7764a3c0195c..bf7c5f5927e3 100644
@@ -693,6 +693,11 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
 	id->mdts = nvmet_ctrl_mdts(req);
 	id->cntlid = cpu_to_le16(ctrl->cntlid);
 	id->ver = cpu_to_le32(ctrl->subsys->ver);
+	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		id->ciu = ctrl->ciu;
+		id->cirn = cpu_to_le64(ctrl->cirn);
+		id->ccrl = NVMF_CCR_LIMIT;
+	}
 
 	/* XXX: figure out what to do about RTD3R/RTD3 */
 	id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
drivers/nvme/target/core.c +9 -0
index 43871a8f56ca..4c8f30c5ff7e 100644
@@ -1412,6 +1412,10 @@ static void nvmet_start_ctrl(struct nvmet_ctrl *ctrl)
 		return;
 	}
 
+	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		ctrl->ciu = ((u8)(ctrl->ciu + 1)) ? : 1;
+		ctrl->cirn = get_random_u64();
+	}
 	ctrl->csts = NVME_CSTS_RDY;
 
 	/*
@@ -1695,6 +1699,11 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 	}
 	ctrl->cntlid = ret;
 
+	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		ctrl->ciu = get_random_u8() ? : 1;
+		ctrl->cirn = get_random_u64();
+	}
+
 	ret = nvmet_ctrl_init_pr(ctrl);
 	if (ret)
 		goto init_pr_fail;
drivers/nvme/target/nvmet.h +2 -0
index dbda55895f4f..e3e414ef68d3 100644
@@ -269,7 +269,9 @@ struct nvmet_ctrl {
 	uuid_t			hostid;
 	u16			cntlid;
 	u16			max_qid;
+	u8			ciu;
 	u32			kato;
+	u64			cirn;
 
 	struct nvmet_port	*port;
 
include/linux/nvme.h +8 -2
index 91ce434a7e8d..cbaf02b0cf07 100644
@@ -21,6 +21,8 @@
 #define NVMF_TRADDR_SIZE	256
 #define NVMF_TSAS_SIZE		256
 
+#define NVMF_CCR_LIMIT		4
+
 #define NVME_DISC_SUBSYS_NAME	"nqn.2014-08.org.nvmexpress.discovery"
 
 #define NVME_NSID_ALL		0xffffffff
@@ -328,7 +330,10 @@ struct nvme_id_ctrl {
 	__le16			crdt1;
 	__le16			crdt2;
 	__le16			crdt3;
-	__u8			rsvd134[122];
+	__u8			rsvd134[1];
+	__u8			ciu;
+	__le64			cirn;
+	__u8			rsvd144[112];
 	__le16			oacs;
 	__u8			acl;
 	__u8			aerl;
@@ -389,7 +394,8 @@ struct nvme_id_ctrl {
 	__u8			msdbd;
 	__u8			rsvd1804[2];
 	__u8			dctype;
-	__u8			rsvd1807[241];
+	__u8			ccrl;
+	__u8			rsvd1808[240];
 	struct nvme_id_power_state	psd[32];
 	__u8			vs[1024];
 };
Signature · 1 line
2.55.0
#4 lore raw

PATCH v6 2/18 nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Mohamed Khalfella

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory.

---
 drivers/nvme/target/debugfs.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/nvme/target/debugfs.c +21 -0
index e85fe1d4c9f8..4de460879d0b 100644
@@ -152,6 +152,23 @@ static int nvmet_ctrl_tls_concat_show(struct seq_file *m, void *p)
 }
 NVMET_DEBUGFS_ATTR(nvmet_ctrl_tls_concat);
 #endif
+static int nvmet_ctrl_instance_ciu_show(struct seq_file *m, void *p)
+{
+	struct nvmet_ctrl *ctrl = m->private;
+
+	seq_printf(m, "%02x\n", ctrl->ciu);
+	return 0;
+}
+NVMET_DEBUGFS_ATTR(nvmet_ctrl_instance_ciu);
+
+static int nvmet_ctrl_instance_cirn_show(struct seq_file *m, void *p)
+{
+	struct nvmet_ctrl *ctrl = m->private;
+
+	seq_printf(m, "%016llx\n", ctrl->cirn);
+	return 0;
+}
+NVMET_DEBUGFS_ATTR(nvmet_ctrl_instance_cirn);
 
 static const char *const nvmet_pr_type_names[] = {
 	[NVME_PR_WRITE_EXCLUSIVE]		= "write_exclusive",
@@ -287,6 +304,10 @@ int nvmet_debugfs_ctrl_setup(struct nvmet_ctrl *ctrl)
 	debugfs_create_file("tls_key", S_IRUSR, ctrl->debugfs_dir, ctrl,
 			    &nvmet_ctrl_tls_key_fops);
 #endif
+	debugfs_create_file("ciu", S_IRUSR, ctrl->debugfs_dir, ctrl,
+			    &nvmet_ctrl_instance_ciu_fops);
+	debugfs_create_file("cirn", S_IRUSR, ctrl->debugfs_dir, ctrl,
+			    &nvmet_ctrl_instance_cirn_fops);
 	return 0;
 }
 
Signature · 1 line
2.55.0
#11 lore raw

PATCH v6 3/18 nvmet: Implement CCR nvme command

In reply to Mohamed Khalfella

Defined by TP8028 Rapid Path Failure Recovery, CCR (Cross-Controller Reset) command is an nvme command issued to source controller by initiator to reset impacted controller. Implement CCR command for linux nvme target.

---
 drivers/nvme/target/admin-cmd.c | 76 ++++++++++++++++++++++++++++++++
 drivers/nvme/target/core.c      | 78 +++++++++++++++++++++++++++++++++
 drivers/nvme/target/nvmet.h     | 13 ++++++
 include/linux/nvme.h            | 23 ++++++++++
 4 files changed, 190 insertions(+)
drivers/nvme/target/admin-cmd.c +76 -0
index bf7c5f5927e3..21538e7c83be 100644
@@ -378,6 +378,7 @@ static void nvmet_get_cmd_effects_admin(struct nvmet_ctrl *ctrl,
 	log->acs[nvme_admin_get_features] =
 	log->acs[nvme_admin_async_event] =
 	log->acs[nvme_admin_keep_alive] =
+	log->acs[nvme_admin_cross_ctrl_reset] =
 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
 }
 
@@ -1613,6 +1614,78 @@ void nvmet_execute_keep_alive(struct nvmet_req *req)
 	nvmet_req_complete(req, status);
 }
 
+void nvmet_execute_cross_ctrl_reset(struct nvmet_req *req)
+{
+	struct nvmet_ctrl *ictrl, *sctrl = req->sq->ctrl;
+	struct nvme_command *cmd = req->cmd;
+	struct nvmet_ccr *ccr, *new_ccr;
+	int ccr_active, ccr_total;
+	u16 cntlid, status = NVME_SC_SUCCESS;
+
+	if (!nvmet_check_transfer_len(req, 0))
+		return;
+
+	cntlid = le16_to_cpu(cmd->ccr.icid);
+	if (sctrl->cntlid == cntlid) {
+		req->error_loc =
+			offsetof(struct nvme_cross_ctrl_reset_cmd, icid);
+		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
+		goto out;
+	}
+
+	/* Find and get impacted controller */
+	ictrl = nvmet_ctrl_find_get_ccr(sctrl->subsys, sctrl->hostnqn,
+					cmd->ccr.ciu, cntlid,
+					le64_to_cpu(cmd->ccr.cirn));
+	if (!ictrl) {
+		/* Immediate Reset Successful */
+		nvmet_set_result(req, 1);
+		status = NVME_SC_SUCCESS;
+		goto out;
+	}
+
+	ccr_total = ccr_active = 0;
+	mutex_lock(&sctrl->lock);
+	list_for_each_entry(ccr, &sctrl->ccr_list, entry) {
+		if (ccr->ctrl == ictrl) {
+			status = NVME_SC_CCR_IN_PROGRESS | NVME_STATUS_DNR;
+			goto out_unlock;
+		}
+
+		ccr_total++;
+		if (ccr->ctrl)
+			ccr_active++;
+	}
+
+	if (ccr_active >= NVMF_CCR_LIMIT) {
+		status = NVME_SC_CCR_LIMIT_EXCEEDED;
+		goto out_unlock;
+	}
+	if (ccr_total >= NVMF_CCR_PER_PAGE) {
+		status = NVME_SC_CCR_LOGPAGE_FULL;
+		goto out_unlock;
+	}
+
+	new_ccr = kmalloc_obj(*new_ccr, GFP_KERNEL);
+	if (!new_ccr) {
+		status = NVME_SC_INTERNAL;
+		goto out_unlock;
+	}
+
+	new_ccr->ciu = cmd->ccr.ciu;
+	new_ccr->icid = cntlid;
+	new_ccr->ctrl = ictrl;
+	list_add_tail(&new_ccr->entry, &sctrl->ccr_list);
+
+out_unlock:
+	mutex_unlock(&sctrl->lock);
+	if (status == NVME_SC_SUCCESS)
+		nvmet_ctrl_fatal_error(ictrl);
+	nvmet_ctrl_put(ictrl);
+out:
+	nvmet_req_complete(req, status);
+}
+
 u32 nvmet_admin_cmd_data_len(struct nvmet_req *req)
 {
 	struct nvme_command *cmd = req->cmd;
@@ -1690,6 +1763,9 @@ u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
 	case nvme_admin_keep_alive:
 		req->execute = nvmet_execute_keep_alive;
 		return 0;
+	case nvme_admin_cross_ctrl_reset:
+		req->execute = nvmet_execute_cross_ctrl_reset;
+		return 0;
 	default:
 		return nvmet_report_invalid_opcode(req);
 	}
drivers/nvme/target/core.c +78 -0

114 lines not on this page · raw message

drivers/nvme/target/nvmet.h +13 -0
index e3e414ef68d3..3c70ef13c9b4 100644
@@ -273,6 +273,7 @@ struct nvmet_ctrl {
 	u32			kato;
 	u64			cirn;
 
+	struct list_head	ccr_list;
 	struct nvmet_port	*port;
 
 	u32			aen_enabled;
@@ -319,6 +320,13 @@ struct nvmet_ctrl {
 	struct nvmet_pr_log_mgr pr_log_mgr;
 };
 
+struct nvmet_ccr {
+	struct nvmet_ctrl	*ctrl;
+	struct list_head	entry;
+	u16			icid;
+	u8			ciu;
+};
+
 struct nvmet_subsys {
 	enum nvme_subsys_type	type;
 
@@ -584,6 +592,7 @@ void nvmet_req_free_sgls(struct nvmet_req *req);
 void nvmet_execute_set_features(struct nvmet_req *req);
 void nvmet_execute_get_features(struct nvmet_req *req);
 void nvmet_execute_keep_alive(struct nvmet_req *req);
+void nvmet_execute_cross_ctrl_reset(struct nvmet_req *req);
 
 u16 nvmet_check_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create);
 u16 nvmet_check_io_cqid(struct nvmet_ctrl *ctrl, u16 cqid, bool create);
@@ -626,6 +635,10 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args);
 struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn,
 				       const char *hostnqn, u16 cntlid,
 				       struct nvmet_req *req);
+struct nvmet_ctrl *nvmet_ctrl_find_get_ccr(struct nvmet_subsys *subsys,
+					   const char *hostnqn, u8 ciu,
+					   u16 cntlid, u64 cirn);
+void nvmet_ctrl_cleanup_ccrs(struct nvmet_ctrl *ctrl, bool all);
 void nvmet_ctrl_put(struct nvmet_ctrl *ctrl);
 u16 nvmet_check_ctrl_status(struct nvmet_req *req);
 ssize_t nvmet_ctrl_host_traddr(struct nvmet_ctrl *ctrl,
include/linux/nvme.h +23 -0
index cbaf02b0cf07..7e6150d9494a 100644
@@ -22,6 +22,7 @@
 #define NVMF_TSAS_SIZE		256
 
 #define NVMF_CCR_LIMIT		4
+#define NVMF_CCR_PER_PAGE	511
 
 #define NVME_DISC_SUBSYS_NAME	"nqn.2014-08.org.nvmexpress.discovery"
 
@@ -1233,6 +1234,22 @@ struct nvme_zone_mgmt_recv_cmd {
 	__le32			cdw14[2];
 };
 
+struct nvme_cross_ctrl_reset_cmd {
+	__u8			opcode;
+	__u8			flags;
+	__u16			command_id;
+	__le32			nsid;
+	__le64			rsvd2[2];
+	union nvme_data_ptr	dptr;
+	__le16			icid;
+	__u8			ciu;
+	__u8			rsvd10;
+	__le32			cdw11;
+	__le64			cirn;
+	__le32			cdw14;
+	__le32			cdw15;
+};
+
 struct nvme_io_mgmt_recv_cmd {
 	__u8			opcode;
 	__u8			flags;
@@ -1331,6 +1348,7 @@ enum nvme_admin_opcode {
 	nvme_admin_virtual_mgmt		= 0x1c,
 	nvme_admin_nvme_mi_send		= 0x1d,
 	nvme_admin_nvme_mi_recv		= 0x1e,
+	nvme_admin_cross_ctrl_reset	= 0x38,
 	nvme_admin_dbbuf		= 0x7C,
 	nvme_admin_format_nvm		= 0x80,
 	nvme_admin_security_send	= 0x81,
@@ -1364,6 +1382,7 @@ enum nvme_admin_opcode {
 		nvme_admin_opcode_name(nvme_admin_virtual_mgmt),	\
 		nvme_admin_opcode_name(nvme_admin_nvme_mi_send),	\
 		nvme_admin_opcode_name(nvme_admin_nvme_mi_recv),	\
+		nvme_admin_opcode_name(nvme_admin_cross_ctrl_reset),	\
 		nvme_admin_opcode_name(nvme_admin_dbbuf),		\
 		nvme_admin_opcode_name(nvme_admin_format_nvm),		\
 		nvme_admin_opcode_name(nvme_admin_security_send),	\
@@ -2022,6 +2041,7 @@ struct nvme_command {
 		struct nvme_dbbuf dbbuf;
 		struct nvme_directive_cmd directive;
 		struct nvme_io_mgmt_recv_cmd imr;
+		struct nvme_cross_ctrl_reset_cmd ccr;
 	};
 };
 
@@ -2186,6 +2206,9 @@ enum {
 	NVME_SC_PMR_SAN_PROHIBITED	= 0x123,
 	NVME_SC_ANA_GROUP_ID_INVALID	= 0x124,
 	NVME_SC_ANA_ATTACH_FAILED	= 0x125,
+	NVME_SC_CCR_IN_PROGRESS		= 0x13f,
+	NVME_SC_CCR_LOGPAGE_FULL	= 0x140,
+	NVME_SC_CCR_LIMIT_EXCEEDED	= 0x141,
 
 	/*
 	 * I/O Command Set Specific - NVM commands:
Signature · 1 line
2.55.0
#14 lore raw

PATCH v6 4/18 nvmet: Implement CCR logpage

In reply to Mohamed Khalfella

Defined by TP8028 Rapid Path Failure Recovery, CCR (Cross-Controller Reset) log page contains an entry for each CCR request submitted to source controller. Implement CCR logpage for nvme linux target.

---
 drivers/nvme/target/admin-cmd.c | 44 +++++++++++++++++++++++++++++++++
 include/linux/nvme.h            | 29 ++++++++++++++++++++++
 2 files changed, 73 insertions(+)
drivers/nvme/target/admin-cmd.c +44 -0
index 21538e7c83be..a60b2f271e98 100644
@@ -220,6 +220,7 @@ static void nvmet_execute_get_supported_log_pages(struct nvmet_req *req)
 	logs->lids[NVME_LOG_FEATURES] = cpu_to_le32(NVME_LIDS_LSUPP);
 	logs->lids[NVME_LOG_RMI] = cpu_to_le32(NVME_LIDS_LSUPP);
 	logs->lids[NVME_LOG_RESERVATION] = cpu_to_le32(NVME_LIDS_LSUPP);
+	logs->lids[NVME_LOG_CCR] = cpu_to_le32(NVME_LIDS_LSUPP);
 
 	status = nvmet_copy_to_sgl(req, 0, logs, sizeof(*logs));
 	kfree(logs);
@@ -608,6 +609,47 @@ static void nvmet_execute_get_log_page_features(struct nvmet_req *req)
 	nvmet_req_complete(req, status);
 }
 
+static void nvmet_execute_get_log_page_ccr(struct nvmet_req *req)
+{
+	struct nvmet_ctrl *ctrl = req->sq->ctrl;
+	struct nvmet_ccr *ccr;
+	struct nvme_ccr_log *log;
+	int index = 0;
+	u16 status;
+
+	log = kzalloc_obj(*log);
+	if (!log) {
+		status = NVME_SC_INTERNAL;
+		goto out;
+	}
+
+	mutex_lock(&ctrl->lock);
+	list_for_each_entry(ccr, &ctrl->ccr_list, entry) {
+		u8 flags = NVME_CCR_FLAGS_VALIDATED | NVME_CCR_FLAGS_INITIATED;
+		u8 ccr_status = ccr->ctrl ? NVME_CCR_STATUS_IN_PROGRESS :
+					NVME_CCR_STATUS_SUCCESS;
+
+		log->entries[index].icid = cpu_to_le16(ccr->icid);
+		log->entries[index].ciu = ccr->ciu;
+		log->entries[index].acid = cpu_to_le16(0xffff);
+		log->entries[index].ccrs = ccr_status;
+		log->entries[index].ccrf = flags;
+		index++;
+	}
+
+	/* Cleanup completed CCRs if requested */
+	if (req->cmd->get_log_page.lsp & 0x1)
+		nvmet_ctrl_cleanup_ccrs(ctrl, false);
+	mutex_unlock(&ctrl->lock);
+
+	log->ne = cpu_to_le16(index);
+	nvmet_clear_aen_bit(req, NVME_AEN_BIT_CCR_COMPLETE);
+	status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
+	kfree(log);
+out:
+	nvmet_req_complete(req, status);
+}
+
 static void nvmet_execute_get_log_page(struct nvmet_req *req)
 {
 	if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd)))
@@ -641,6 +683,8 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)
 		return nvmet_execute_get_log_page_rmi(req);
 	case NVME_LOG_RESERVATION:
 		return nvmet_execute_get_log_page_resv(req);
+	case NVME_LOG_CCR:
+		return nvmet_execute_get_log_page_ccr(req);
 	}
 	pr_debug("unhandled lid %d on qid %d\n",
 	       req->cmd->get_log_page.lid, req->sq->qid);
include/linux/nvme.h +29 -0
index 7e6150d9494a..70dd0770e032 100644
@@ -1440,6 +1440,7 @@ enum {
 	NVME_LOG_ANA		= 0x0c,
 	NVME_LOG_FEATURES	= 0x12,
 	NVME_LOG_RMI		= 0x16,
+	NVME_LOG_CCR		= 0x1e,
 	NVME_LOG_FDP_CONFIGS	= 0x20,
 	NVME_LOG_DISC		= 0x70,
 	NVME_LOG_RESERVATION	= 0x80,
@@ -1466,6 +1467,34 @@ enum {
 	NVME_FIS_CSCPE	= 1 << 21,
 };
 
+/* NVMe Cross-Controller Reset Status */
+enum {
+	NVME_CCR_STATUS_IN_PROGRESS,
+	NVME_CCR_STATUS_SUCCESS,
+	NVME_CCR_STATUS_FAILED,
+};
+
+/* NVMe Cross-Controller Reset Flags */
+enum {
+	NVME_CCR_FLAGS_VALIDATED	= 0x01,
+	NVME_CCR_FLAGS_INITIATED	= 0x02,
+};
+
+struct nvme_ccr_log_entry {
+	__le16			icid;
+	__u8			ciu;
+	__u8			rsvd3;
+	__le16			acid;
+	__u8			ccrs;
+	__u8			ccrf;
+};
+
+struct nvme_ccr_log {
+	__le16				ne;
+	__u8				rsvd2[6];
+	struct nvme_ccr_log_entry	entries[NVMF_CCR_PER_PAGE];
+};
+
 /* NVMe Namespace Write Protect State */
 enum {
 	NVME_NS_NO_WRITE_PROTECT = 0,
Signature · 1 line
2.55.0
#16 lore raw

PATCH v6 5/18 nvmet: Send an AEN on CCR completion

In reply to Mohamed Khalfella

When an impacted controller that is the target of pending CCRs is torn down, send an AEN to each source controller that issued a CCR against it. The notification points to the CCR log page that the source controller can read to check which CCR operation completed.

Split nvmet_add_async_event() into a locked helper so the AEN can be queued while ctrl->lock is already held.

---
 drivers/nvme/target/core.c  | 25 ++++++++++++++++++++++---
 drivers/nvme/target/nvmet.h |  3 ++-
 include/linux/nvme.h        |  3 +++
 3 files changed, 27 insertions(+), 4 deletions(-)
drivers/nvme/target/core.c +22 -3
index f5d7cb9c943a..69491e8e52ba 100644
@@ -205,7 +205,7 @@ static void nvmet_async_event_work(struct work_struct *work)
 	nvmet_async_events_process(ctrl);
 }
 
-void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
+static void nvmet_add_async_event_locked(struct nvmet_ctrl *ctrl, u8 event_type,
 		u8 event_info, u8 log_page)
 {
 	struct nvmet_async_event *aen;
@@ -218,13 +218,19 @@ void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
 	aen->event_info = event_info;
 	aen->log_page = log_page;
 
-	mutex_lock(&ctrl->lock);
 	list_add_tail(&aen->entry, &ctrl->async_events);
-	mutex_unlock(&ctrl->lock);
 
 	queue_work(nvmet_aen_wq, &ctrl->async_event_work);
 }
 
+void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
+		u8 event_info, u8 log_page)
+{
+	mutex_lock(&ctrl->lock);
+	nvmet_add_async_event_locked(ctrl, event_type, event_info, log_page);
+	mutex_unlock(&ctrl->lock);
+}
+
 static void nvmet_add_to_changed_ns_log(struct nvmet_ctrl *ctrl, __le32 nsid)
 {
 	u32 i;
@@ -1807,6 +1813,18 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 }
 EXPORT_SYMBOL_GPL(nvmet_alloc_ctrl);
 
+static void nvmet_ctrl_notify_ccr(struct nvmet_ctrl *ctrl)
+{
+	lockdep_assert_held(&ctrl->lock);
+
+	if (nvmet_aen_bit_disabled(ctrl, NVME_AEN_BIT_CCR_COMPLETE))
+		return;
+
+	nvmet_add_async_event_locked(ctrl, NVME_AER_NOTICE,
+				     NVME_AER_NOTICE_CCR_COMPLETED,
+				     NVME_LOG_CCR);
+}
+
 static void nvmet_ctrl_complete_pending_ccr(struct nvmet_ctrl *ctrl)
 {
 	struct nvmet_subsys *subsys = ctrl->subsys;
@@ -1830,6 +1848,7 @@ static void nvmet_ctrl_complete_pending_ccr(struct nvmet_ctrl *ctrl)
 		list_for_each_entry(ccr, &sctrl->ccr_list, entry) {
 			if (ccr->ctrl == ctrl) {
 				ccr->ctrl = NULL;
+				nvmet_ctrl_notify_ccr(sctrl);
 				break;
 			}
 		}
drivers/nvme/target/nvmet.h +2 -1
index 3c70ef13c9b4..88fa11aa0587 100644
@@ -44,7 +44,8 @@
  * Supported optional AENs:
  */
 #define NVMET_AEN_CFG_OPTIONAL \
-	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE)
+	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_ANA_CHANGE | \
+	 NVME_AEN_CFG_CCR_COMPLETE)
 #define NVMET_DISC_AEN_CFG_OPTIONAL \
 	(NVME_AEN_CFG_DISC_CHANGE)
 
include/linux/nvme.h +3 -0
index 70dd0770e032..3757d11ffea3 100644
@@ -871,12 +871,14 @@ enum {
 	NVME_AER_NOTICE_FW_ACT_STARTING = 0x01,
 	NVME_AER_NOTICE_ANA		= 0x03,
 	NVME_AER_NOTICE_DISC_CHANGED	= 0xf0,
+	NVME_AER_NOTICE_CCR_COMPLETED	= 0xf4,
 };
 
 enum {
 	NVME_AEN_BIT_NS_ATTR		= 8,
 	NVME_AEN_BIT_FW_ACT		= 9,
 	NVME_AEN_BIT_ANA_CHANGE		= 11,
+	NVME_AEN_BIT_CCR_COMPLETE	= 20,
 	NVME_AEN_BIT_DISC_CHANGE	= 31,
 };
 
@@ -884,6 +886,7 @@ enum {
 	NVME_AEN_CFG_NS_ATTR		= 1 << NVME_AEN_BIT_NS_ATTR,
 	NVME_AEN_CFG_FW_ACT		= 1 << NVME_AEN_BIT_FW_ACT,
 	NVME_AEN_CFG_ANA_CHANGE		= 1 << NVME_AEN_BIT_ANA_CHANGE,
+	NVME_AEN_CFG_CCR_COMPLETE	= 1 << NVME_AEN_BIT_CCR_COMPLETE,
 	NVME_AEN_CFG_DISC_CHANGE	= 1 << NVME_AEN_BIT_DISC_CHANGE,
 };
 
Signature · 1 line
2.55.0
#20 lore raw

PATCH v6 6/18 nvme: Rapid Path Failure Recovery read controller identify fields

In reply to Mohamed Khalfella

TP8028 Rapid Path Failure added new fields to controller identify response. Read CIU (Controller Instance Uniquifier), CIRN (Controller Instance Random Number), and CCRL (Cross-Controller Reset Limit) from controller identify response. Expose CIU and CIRN as sysfs attributes so the values can be used directly by user if needed.

Also add the nvme_fence_timeout_ms() helper that derives a fencing timeout from the controller keep-alive timeout (kato).

---
 drivers/nvme/host/core.c  |  4 ++++
 drivers/nvme/host/nvme.h  | 10 ++++++++++
 drivers/nvme/host/sysfs.c | 23 +++++++++++++++++++++++
 3 files changed, 37 insertions(+)
drivers/nvme/host/core.c +4 -0
index 758245c799a1..ae7c04b77a3e 100644
@@ -3666,6 +3666,10 @@ static int nvme_init_identify(struct nvme_ctrl *ctrl)
 	ctrl->crdt[1] = le16_to_cpu(id->crdt2);
 	ctrl->crdt[2] = le16_to_cpu(id->crdt3);
 
+	ctrl->ciu = id->ciu;
+	ctrl->cirn = le64_to_cpu(id->cirn);
+	ctrl->ccrl = id->ccrl;
+
 	ctrl->oacs = le16_to_cpu(id->oacs);
 	ctrl->oncs = le16_to_cpu(id->oncs);
 	ctrl->mtfa = le16_to_cpu(id->mtfa);
drivers/nvme/host/nvme.h +10 -0
index 2cff9fcbf740..cb9d7f45188f 100644
@@ -393,11 +393,14 @@ struct nvme_ctrl {
 	u16 crdt[3];
 	u16 oncs;
 	u8 dmrl;
+	u8 ciu;
 	u32 dmrsl;
+	u64 cirn;
 	u16 oacs;
 	u16 sqsize;
 	u32 max_namespaces;
 	atomic_t abort_limit;
+	u8 ccrl;
 	u8 vwc;
 	u32 vs;
 	u32 sgls;
@@ -1339,4 +1342,11 @@ static inline bool nvme_multi_css(struct nvme_ctrl *ctrl)
 	return (ctrl->ctrl_config & NVME_CC_CSS_MASK) == NVME_CC_CSS_CSI;
 }
 
+static inline unsigned long nvme_fence_timeout_ms(struct nvme_ctrl *ctrl)
+{
+	if (ctrl->ctratt & NVME_CTRL_ATTR_TBKAS)
+		return 3 * ctrl->kato * 1000;
+	return 2 * ctrl->kato * 1000;
+}
+
 #endif /* _NVME_H */
drivers/nvme/host/sysfs.c +23 -0
index 02a2490a9ed7..b60f5b66a1d1 100644
@@ -487,6 +487,27 @@ nvme_show_int_function(queue_count);
 nvme_show_int_function(sqsize);
 nvme_show_int_function(kato);
 
+static ssize_t nvme_sysfs_ciu_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%02x\n", ctrl->ciu);
+}
+static DEVICE_ATTR(ciu, S_IRUSR, nvme_sysfs_ciu_show, NULL);
+
+static ssize_t nvme_sysfs_cirn_show(struct device *dev,
+					  struct device_attribute *attr,
+					  char *buf)
+{
+	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
+
+	return sysfs_emit(buf, "%016llx\n", ctrl->cirn);
+}
+static DEVICE_ATTR(cirn, S_IRUSR, nvme_sysfs_cirn_show, NULL);
+
+
 static ssize_t nvme_sysfs_delete(struct device *dev,
 				struct device_attribute *attr, const char *buf,
 				size_t count)
@@ -941,6 +962,8 @@ static struct attribute *nvme_dev_attrs[] = {
 	&dev_attr_numa_node.attr,
 	&dev_attr_queue_count.attr,
 	&dev_attr_sqsize.attr,
+	&dev_attr_ciu.attr,
+	&dev_attr_cirn.attr,
 	&dev_attr_hostnqn.attr,
 	&dev_attr_hostid.attr,
 	&dev_attr_ctrl_loss_tmo.attr,
Signature · 1 line
2.55.0
#24 lore raw

PATCH v6 7/18 nvme: Introduce FENCING and FENCED controller states

In reply to Mohamed Khalfella

Introduce two new controller states, FENCING and FENCED, and the state machine transitions needed to support them. Transports will move a LIVE controller into these states when an error is encountered; this patch only adds the states themselves.

FENCING is entered from LIVE. While in FENCING the queues remain alive but new requests are not allowed to be sent, and the controller can be neither reset nor deleted (there is no transition from FENCING to RESETTING, DELETING or DELETING_NOIO). This is intentional because resetting or deleting the controller cancels inflight IOs, which should be held until either CCR succeeds or time-based recovery completes.

FENCED is a short-lived state entered from FENCING before a reset. It is the only state from which RESETTING is reachable, so it exists to prevent a manual reset from taking effect while the controller is still in FENCING.

Update nvme_available_path() to treat a controller in FENCING/FENCED as an available path, both states are non-terminal states, and finally add the state names to be exposed via the sysfs state attribute.

---
 drivers/nvme/host/core.c      | 27 +++++++++++++++++++++++++--
 drivers/nvme/host/multipath.c |  2 ++
 drivers/nvme/host/nvme.h      |  4 ++++
 drivers/nvme/host/sysfs.c     |  2 ++
 4 files changed, 33 insertions(+), 2 deletions(-)
drivers/nvme/host/core.c +25 -2
index ae7c04b77a3e..e4e18fb72159 100644
@@ -596,10 +596,29 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
 			break;
 		}
 		break;
+	case NVME_CTRL_FENCING:
+		switch (old_state) {
+		case NVME_CTRL_LIVE:
+			changed = true;
+			fallthrough;
+		default:
+			break;
+		}
+		break;
+	case NVME_CTRL_FENCED:
+		switch (old_state) {
+		case NVME_CTRL_FENCING:
+			changed = true;
+			fallthrough;
+		default:
+			break;
+		}
+		break;
 	case NVME_CTRL_RESETTING:
 		switch (old_state) {
 		case NVME_CTRL_NEW:
 		case NVME_CTRL_LIVE:
+		case NVME_CTRL_FENCED:
 			changed = true;
 			atomic_long_inc(&ctrl->nr_reset);
 			fallthrough;
@@ -786,6 +805,8 @@ blk_status_t nvme_fail_nonready_command(struct nvme_ctrl *ctrl,
 
 	if (state != NVME_CTRL_DELETING_NOIO &&
 	    state != NVME_CTRL_DELETING &&
+	    state != NVME_CTRL_FENCING &&
+	    state != NVME_CTRL_FENCED &&
 	    state != NVME_CTRL_DEAD &&
 	    !test_bit(NVME_CTRL_FAILFAST_EXPIRED, &ctrl->flags) &&
 	    !blk_noretry_request(rq) && !(rq->cmd_flags & REQ_NVME_MPATH))
@@ -828,10 +849,12 @@ bool __nvme_check_ready(struct nvme_ctrl *ctrl, struct request *rq,
 			     req->cmd->fabrics.fctype == nvme_fabrics_type_auth_receive))
 				return true;
 			break;
-		default:
-			break;
+		case NVME_CTRL_FENCING:
+		case NVME_CTRL_FENCED:
 		case NVME_CTRL_DEAD:
 			return false;
+		default:
+			break;
 		}
 	}
 
drivers/nvme/host/multipath.c +2 -0
index 75dbb58286a3..023d09f3c8ba 100644
@@ -512,6 +512,8 @@ static bool nvme_available_path(struct nvme_ns_head *head)
 		case NVME_CTRL_LIVE:
 		case NVME_CTRL_RESETTING:
 		case NVME_CTRL_CONNECTING:
+		case NVME_CTRL_FENCING:
+		case NVME_CTRL_FENCED:
 			return true;
 		default:
 			break;
drivers/nvme/host/nvme.h +4 -0
index cb9d7f45188f..84ea3f2728bd 100644
@@ -312,6 +312,8 @@ static inline u16 nvme_req_qid(struct request *req)
 enum nvme_ctrl_state {
 	NVME_CTRL_NEW,
 	NVME_CTRL_LIVE,
+	NVME_CTRL_FENCING,
+	NVME_CTRL_FENCED,
 	NVME_CTRL_RESETTING,
 	NVME_CTRL_CONNECTING,
 	NVME_CTRL_DELETING,
@@ -865,6 +867,8 @@ static inline bool nvme_state_terminal(struct nvme_ctrl *ctrl)
 	switch (nvme_ctrl_state(ctrl)) {
 	case NVME_CTRL_NEW:
 	case NVME_CTRL_LIVE:
+	case NVME_CTRL_FENCING:
+	case NVME_CTRL_FENCED:
 	case NVME_CTRL_RESETTING:
 	case NVME_CTRL_CONNECTING:
 		return false;
drivers/nvme/host/sysfs.c +2 -0
index b60f5b66a1d1..3a12b07149d2 100644
@@ -542,6 +542,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
 		[NVME_CTRL_LIVE]	= "live",
+		[NVME_CTRL_FENCING]	= "fencing",
+		[NVME_CTRL_FENCED]	= "fenced",
 		[NVME_CTRL_RESETTING]	= "resetting",
 		[NVME_CTRL_CONNECTING]	= "connecting",
 		[NVME_CTRL_DELETING]	= "deleting",
Signature · 1 line
2.55.0
#34 lore raw

PATCH v6 8/18 nvme: Implement cross-controller reset recovery

In reply to Mohamed Khalfella

A host that has more than one path connecting to an nvme subsystem typically has an nvme controller associated with every path. This is mostly applicable to nvmeof. If one path goes down, inflight IOs on that path should not be retried immediately on another path because this could lead to data corruption as described in TP4129. TP8028 defines cross-controller reset mechanism that can be used by host to terminate IOs on the failed path using one of the remaining healthy paths. Only after IOs are terminated, or long enough time passes as defined by TP4129, inflight IOs should be retried on another path. Implement core cross-controller reset shared logic to be used by the transports.

---
 drivers/nvme/host/constants.c |   1 +
 drivers/nvme/host/core.c      | 152 ++++++++++++++++++++++++++++++++++
 drivers/nvme/host/nvme.h      |  10 +++
 3 files changed, 163 insertions(+)
drivers/nvme/host/constants.c +1 -0
index dc90df9e13a2..f679efd5110e 100644
@@ -46,6 +46,7 @@ static const char * const nvme_admin_ops[] = {
 	[nvme_admin_virtual_mgmt] = "Virtual Management",
 	[nvme_admin_nvme_mi_send] = "NVMe Send MI",
 	[nvme_admin_nvme_mi_recv] = "NVMe Receive MI",
+	[nvme_admin_cross_ctrl_reset] = "Cross Controller Reset",
 	[nvme_admin_dbbuf] = "Doorbell Buffer Config",
 	[nvme_admin_format_nvm] = "Format NVM",
 	[nvme_admin_security_send] = "Security Send",
drivers/nvme/host/core.c +152 -0

164 lines not on this page · raw message

drivers/nvme/host/nvme.h +10 -0
index 84ea3f2728bd..8da75a15ac04 100644
@@ -341,6 +341,13 @@ enum nvme_ctrl_flags {
 	NVME_CTRL_FROZEN		= 6,
 };
 
+struct nvme_ccr_entry {
+	struct list_head list;
+	struct completion complete;
+	struct nvme_ctrl *ictrl;
+	u8 ccrs;
+};
+
 struct nvme_ctrl {
 	bool comp_seen;
 	bool identified;
@@ -358,6 +365,7 @@ struct nvme_ctrl {
 	struct blk_mq_tag_set *tagset;
 	struct blk_mq_tag_set *admin_tagset;
 	struct list_head namespaces;
+	struct list_head ccr_list;
 	struct mutex namespaces_lock;
 	struct srcu_struct srcu;
 	struct device ctrl_device;
@@ -402,6 +410,7 @@ struct nvme_ctrl {
 	u16 sqsize;
 	u32 max_namespaces;
 	atomic_t abort_limit;
+	atomic_t ccr_used;
 	u8 ccrl;
 	u8 vwc;
 	u32 vs;
@@ -902,6 +911,7 @@ blk_status_t nvme_host_path_error(struct request *req);
 bool nvme_cancel_request(struct request *req, void *data);
 void nvme_cancel_tagset(struct nvme_ctrl *ctrl);
 void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl);
+unsigned long nvme_fence_ctrl(struct nvme_ctrl *ctrl);
 bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
 		enum nvme_ctrl_state new_state);
 int nvme_disable_ctrl(struct nvme_ctrl *ctrl, bool shutdown);
Signature · 1 line
2.55.0
#41 lore raw

PATCH v6 9/18 nvme: Implement cross-controller reset completion

In reply to Mohamed Khalfella

An nvme source controller that issues CCR command expects to receive an NVME_AER_NOTICE_CCR_COMPLETED when pending CCR succeeds or fails. Add ctrl->ccr_work to read NVME_LOG_CCR logpage and wakeup threads waiting on CCR completion.

---
 drivers/nvme/host/core.c | 50 +++++++++++++++++++++++++++++++++++++++-
 drivers/nvme/host/nvme.h |  1 +
 2 files changed, 50 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +49 -1
index c8e3129d84a8..5509b58b51db 100644
@@ -1959,7 +1959,8 @@ EXPORT_SYMBOL_GPL(nvme_set_queue_count);
 
 #define NVME_AEN_SUPPORTED \
 	(NVME_AEN_CFG_NS_ATTR | NVME_AEN_CFG_FW_ACT | \
-	 NVME_AEN_CFG_ANA_CHANGE | NVME_AEN_CFG_DISC_CHANGE)
+	 NVME_AEN_CFG_ANA_CHANGE | NVME_AEN_CFG_CCR_COMPLETE | \
+	 NVME_AEN_CFG_DISC_CHANGE)
 
 static void nvme_enable_aen(struct nvme_ctrl *ctrl)
 {
@@ -5023,6 +5024,48 @@ static void nvme_get_fw_slot_info(struct nvme_ctrl *ctrl)
 	kfree(log);
 }
 
+static void nvme_ccr_work(struct work_struct *work)
+{
+	struct nvme_ctrl *ctrl = container_of(work, struct nvme_ctrl, ccr_work);
+	struct nvme_ccr_entry *ccr;
+	struct nvme_ccr_log_entry *entry;
+	struct nvme_ccr_log *log;
+	int num_entries, ret, i;
+	unsigned long flags;
+
+	log = kmalloc_obj(*log);
+	if (!log)
+		return;
+
+	ret = nvme_get_log(ctrl, 0, NVME_LOG_CCR, 0x01,
+			   0x00, log, sizeof(*log), 0);
+	if (ret)
+		goto out;
+
+	spin_lock_irqsave(&ctrl->lock, flags);
+	num_entries = min(le16_to_cpu(log->ne), NVMF_CCR_PER_PAGE);
+	for (i = 0; i < num_entries; i++) {
+		entry = &log->entries[i];
+		if (entry->ccrs == NVME_CCR_STATUS_IN_PROGRESS)
+			continue;
+
+		list_for_each_entry(ccr, &ctrl->ccr_list, list) {
+			struct nvme_ctrl *ictrl = ccr->ictrl;
+
+			if (ictrl->cntlid != le16_to_cpu(entry->icid) ||
+			    ictrl->ciu != entry->ciu)
+				continue;
+
+			/* Complete matching entry */
+			ccr->ccrs = entry->ccrs;
+			complete(&ccr->complete);
+		}
+	}
+	spin_unlock_irqrestore(&ctrl->lock, flags);
+out:
+	kfree(log);
+}
+
 static void nvme_fw_act_work(struct work_struct *work)
 {
 	struct nvme_ctrl *ctrl = container_of(work,
@@ -5099,6 +5142,9 @@ static bool nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result)
 	case NVME_AER_NOTICE_DISC_CHANGED:
 		ctrl->aen_result = result;
 		break;
+	case NVME_AER_NOTICE_CCR_COMPLETED:
+		queue_work(nvme_wq, &ctrl->ccr_work);
+		break;
 	default:
 		dev_warn(ctrl->device, "async event result %08x\n", result);
 	}
@@ -5287,6 +5333,7 @@ void nvme_stop_ctrl(struct nvme_ctrl *ctrl)
 	nvme_stop_failfast_work(ctrl);
 	flush_work(&ctrl->async_event_work);
 	cancel_work_sync(&ctrl->fw_act_work);
+	cancel_work_sync(&ctrl->ccr_work);
 	if (ctrl->ops->stop_ctrl)
 		ctrl->ops->stop_ctrl(ctrl);
 }
@@ -5412,6 +5459,7 @@ int nvme_init_ctrl(struct nvme_ctrl *ctrl, struct device *dev,
 	ctrl->quirks = quirks;
 	ctrl->numa_node = NUMA_NO_NODE;
 	INIT_WORK(&ctrl->scan_work, nvme_scan_work);
+	INIT_WORK(&ctrl->ccr_work, nvme_ccr_work);
 	INIT_WORK(&ctrl->async_event_work, nvme_async_event_work);
 	INIT_WORK(&ctrl->fw_act_work, nvme_fw_act_work);
 	INIT_WORK(&ctrl->delete_work, nvme_delete_ctrl_work);
drivers/nvme/host/nvme.h +1 -0
index 8da75a15ac04..7096f1e4f84e 100644
@@ -431,6 +431,7 @@ struct nvme_ctrl {
 	struct nvme_effects_log *effects;
 	struct xarray cels;
 	struct work_struct scan_work;
+	struct work_struct ccr_work;
 	struct work_struct async_event_work;
 	struct delayed_work ka_work;
 	struct delayed_work failfast_work;
Signature · 1 line
2.55.0
#46 lore raw

PATCH v6 10/18 nvme-tcp: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella

An alive nvme controller that hits an error now will move to FENCING state instead of RESETTING state. ctrl->fencing_work attempts CCR to terminate inflight IOs. Regardless of the success or failure of CCR operation the controller is transitioned to RESETTING state to continue error recovery process.

---
 drivers/nvme/host/tcp.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/nvme/host/tcp.c +28 -1
index 921934028e0b..febbe8954473 100644
@@ -167,6 +167,7 @@ struct nvme_tcp_ctrl {
 	struct sockaddr_storage src_addr;
 	struct nvme_ctrl	ctrl;
 
+	struct work_struct	fencing_work;
 	struct work_struct	err_work;
 	struct delayed_work	connect_work;
 	struct nvme_tcp_request async_req;
@@ -616,6 +617,12 @@ static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
 
 static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
 {
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCING)) {
+		dev_warn(ctrl->device, "starting controller fencing\n");
+		queue_work(nvme_wq, &to_tcp_ctrl(ctrl)->fencing_work);
+		return;
+	}
+
 	if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
 		return;
 
@@ -2558,6 +2565,22 @@ static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
 	nvme_tcp_reconnect_or_remove(ctrl, ret);
 }
 
+static void nvme_tcp_fencing_work(struct work_struct *work)
+{
+	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
+			struct nvme_tcp_ctrl, fencing_work);
+	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
+	unsigned long rem;
+
+	rem = nvme_fence_ctrl(ctrl);
+	if (rem)
+		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &tcp_ctrl->err_work);
+}
+
 static void nvme_tcp_error_recovery_work(struct work_struct *work)
 {
 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
@@ -2633,6 +2656,7 @@ static void nvme_reset_ctrl_work(struct work_struct *work)
 
 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
 {
+	flush_work(&to_tcp_ctrl(ctrl)->fencing_work);
 	flush_work(&to_tcp_ctrl(ctrl)->err_work);
 	cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
 }
@@ -2731,13 +2755,15 @@ static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
 	struct nvme_command *cmd = &pdu->cmd;
 	int qid = nvme_tcp_queue_id(req->queue);
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->device,
 		 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
-	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
 		/*
 		 * If we are resetting, connecting or deleting we should
 		 * complete immediately because we may block controller
@@ -2996,6 +3022,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 
 	INIT_DELAYED_WORK(&ctrl->connect_work,
 			nvme_tcp_reconnect_ctrl_work);
+	INIT_WORK(&ctrl->fencing_work, nvme_tcp_fencing_work);
 	INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
 
Signature · 1 line
2.55.0
#49 lore raw

PATCH v6 11/18 nvme-rdma: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella

An alive nvme controller that hits an error now will move to FENCING state instead of RESETTING state. ctrl->fencing_work attempts CCR to terminate inflight IOs. Regardless of the success or failure of CCR operation the controller is transitioned to RESETTING state to continue error recovery process.

---
 drivers/nvme/host/rdma.c | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/nvme/host/rdma.c +28 -1
index 29ecbe71bb2e..9c50421d79c4 100644
@@ -119,6 +119,7 @@ struct nvme_rdma_ctrl {
 
 	/* other member variables */
 	struct blk_mq_tag_set	tag_set;
+	struct work_struct	fencing_work;
 	struct work_struct	err_work;
 
 	struct nvme_rdma_qe	async_event_sqe;
@@ -992,6 +993,7 @@ static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
 {
 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
 
+	flush_work(&ctrl->fencing_work);
 	flush_work(&ctrl->err_work);
 	cancel_delayed_work_sync(&ctrl->reconnect_work);
 }
@@ -1153,6 +1155,22 @@ static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
 	nvme_rdma_reconnect_or_remove(ctrl, ret);
 }
 
+static void nvme_rdma_fencing_work(struct work_struct *work)
+{
+	struct nvme_rdma_ctrl *rdma_ctrl = container_of(work,
+			struct nvme_rdma_ctrl, fencing_work);
+	struct nvme_ctrl *ctrl = &rdma_ctrl->ctrl;
+	unsigned long rem;
+
+	rem = nvme_fence_ctrl(ctrl);
+	if (rem)
+		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &rdma_ctrl->err_work);
+}
+
 static void nvme_rdma_error_recovery_work(struct work_struct *work)
 {
 	struct nvme_rdma_ctrl *ctrl = container_of(work,
@@ -1180,6 +1198,12 @@ static void nvme_rdma_error_recovery_work(struct work_struct *work)
 
 static void nvme_rdma_error_recovery(struct nvme_rdma_ctrl *ctrl)
 {
+	if (nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_FENCING)) {
+		dev_warn(ctrl->ctrl.device, "starting controller fencing\n");
+		queue_work(nvme_wq, &ctrl->fencing_work);
+		return;
+	}
+
 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
 		return;
 
@@ -1990,13 +2014,15 @@ static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
 	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
 	struct nvme_command *cmd = req->req.cmd;
 	int qid = nvme_rdma_queue_idx(queue);
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->ctrl.device,
 		 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
-	if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(&ctrl->ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
 		/*
 		 * If we are resetting, connecting or deleting we should
 		 * complete immediately because we may block controller
@@ -2327,6 +2353,7 @@ static struct nvme_rdma_ctrl *nvme_rdma_alloc_ctrl(struct device *dev,
 
 	INIT_DELAYED_WORK(&ctrl->reconnect_work,
 			nvme_rdma_reconnect_ctrl_work);
+	INIT_WORK(&ctrl->fencing_work, nvme_rdma_fencing_work);
 	INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
 
Signature · 1 line
2.55.0
#53 lore raw

PATCH v6 12/18 nvme-fc: start error recovery instead of aborting timed out IOs

In reply to Mohamed Khalfella

Aborts issued from the timeout handler run outside the FCCTRL_TERMIO window, so they are not counted in ctrl->iocnt and nvme_fc_delete_association() does not wait for them. The association can be torn down while the LLDD is still working on the abort.

Instead of aborting the timed out command, reset the controller like the other fabrics transports do. All aborts now happen in nvme_fc_delete_association(), where they are counted and waited for.

The new nvme_fc_start_ioerr_recovery() queues ioerr_work directly in CONNECTING (abort the IOs so the connect attempt fails) and in DELETING/DELETING_NOIO (tear down the association so the IOs the delete path is draining get completed - the timeout handler no longer aborts them, and a dead target would otherwise hang controller deletion). In all other states it moves the controller to RESETTING first. Connectivity loss, disconnect LS and IO errors now go through the same entry point.

With nvme_fc_timeout() no longer aborts timedout IOs the reset code in nvme_fc_reset_ctrl_work() needs to be updated to teardown the association before stopping the controller. This is important because nvme_stop_ctrl() waiting for ana_work or fw_act_work to be flushed can get stuck forever.

---
 drivers/nvme/host/fc.c | 54 ++++++++++++++++++++++++++++--------------
 1 file changed, 36 insertions(+), 18 deletions(-)
drivers/nvme/host/fc.c +36 -18
index 48454cb7a0fc..6181cb7ea8ce 100644
@@ -227,6 +227,8 @@ static DEFINE_IDA(nvme_fc_ctrl_cnt);
 static struct device *fc_udev_device;
 
 static void nvme_fc_complete_rq(struct request *rq);
+static void nvme_fc_start_ioerr_recovery(struct nvme_fc_ctrl *ctrl,
+					 char *errmsg);
 
 /* *********************** FC-NVME Port Management ************************ */
 
@@ -788,7 +790,7 @@ nvme_fc_ctrl_connectivity_loss(struct nvme_fc_ctrl *ctrl)
 		"Reconnect", ctrl->cnum);
 
 	set_bit(ASSOC_FAILED, &ctrl->flags);
-	nvme_reset_ctrl(&ctrl->ctrl);
+	nvme_fc_start_ioerr_recovery(ctrl, "Connectivity Loss");
 }
 
 /**
@@ -1569,7 +1571,8 @@ nvme_fc_ls_disconnect_assoc(struct nvmefc_ls_rcv_op *lsop)
 	 */
 
 	/* fail the association */
-	nvme_fc_error_recovery(ctrl, "Disconnect Association LS received");
+	nvme_fc_start_ioerr_recovery(ctrl,
+				     "Disconnect Association LS received");
 
 	/* release the reference taken by nvme_fc_match_disconn_ls() */
 	nvme_fc_ctrl_put(ctrl);
@@ -1892,6 +1895,30 @@ char *nvme_fc_io_getuuid(struct nvmefc_fcp_req *req)
 }
 EXPORT_SYMBOL_GPL(nvme_fc_io_getuuid);
 
+static void
+nvme_fc_start_ioerr_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
+{
+	enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
+
+	/*
+	 * In CONNECTING, ioerr_work aborts the outstanding ios so the
+	 * connect attempt sees the error. In DELETING/DELETING_NOIO it
+	 * tears down the association so IOs the core delete path is
+	 * draining get completed.
+	 */
+	if (state == NVME_CTRL_CONNECTING || state == NVME_CTRL_DELETING ||
+	    state == NVME_CTRL_DELETING_NOIO) {
+		queue_work(nvme_reset_wq, &ctrl->ioerr_work);
+		return;
+	}
+
+	if (nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) {
+		dev_warn(ctrl->ctrl.device, "NVME-FC{%d}: starting error recovery %s\n",
+			 ctrl->cnum, errmsg);
+		queue_work(nvme_reset_wq, &ctrl->ioerr_work);
+	}
+}
+
 static void
 nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 {
@@ -2049,9 +2076,8 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 		nvme_fc_complete_rq(rq);
 
 check_error:
-	if (terminate_assoc &&
-	    nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_RESETTING)
-		queue_work(nvme_reset_wq, &ctrl->ioerr_work);
+	if (terminate_assoc)
+		nvme_fc_start_ioerr_recovery(ctrl, "io error");
 }
 
 static int
@@ -2548,24 +2574,14 @@ static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
 	struct nvme_fc_cmd_iu *cmdiu = &op->cmd_iu;
 	struct nvme_command *sqe = &cmdiu->sqe;
 
-	/*
-	 * Attempt to abort the offending command. Command completion
-	 * will detect the aborted io and will fail the connection.
-	 */
 	dev_info(ctrl->ctrl.device,
 		"NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
 		"x%08x/x%08x\n",
 		ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
 		nvme_fabrics_opcode_str(qnum, sqe),
 		sqe->common.cdw10, sqe->common.cdw11);
-	if (__nvme_fc_abort_op(ctrl, op))
-		nvme_fc_error_recovery(ctrl, "io timeout abort failed");
 
-	/*
-	 * the io abort has been initiated. Have the reset timer
-	 * restarted and the abort completion will complete the io
-	 * shortly. Avoids a synchronous wait while the abort finishes.
-	 */
+	nvme_fc_start_ioerr_recovery(ctrl, "io timeout");
 	return BLK_EH_RESET_TIMER;
 }
 
@@ -3348,10 +3364,12 @@ nvme_fc_reset_ctrl_work(struct work_struct *work)
 	struct nvme_fc_ctrl *ctrl =
 		container_of(work, struct nvme_fc_ctrl, ctrl.reset_work);
 
-	nvme_stop_ctrl(&ctrl->ctrl);
+	nvme_stop_keep_alive(&ctrl->ctrl);
+	flush_work(&ctrl->ctrl.async_event_work);
 
-	/* will block will waiting for io to terminate */
+	/* will block while waiting for io to terminate */
 	nvme_fc_delete_association(ctrl);
+	nvme_stop_ctrl(&ctrl->ctrl);
 
 	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
 		dev_err(ctrl->ctrl.device,
Signature · 1 line
2.55.0
#55 lore raw

PATCH v6 13/18 nvme-fc: perform error recovery directly from ioerr_work

In reply to Mohamed Khalfella

Now that nvme_fc_start_ioerr_recovery() moves the controller to RESETTING before queueing ioerr_work, the nvme_reset_ctrl() call in nvme_fc_error_recovery() fails with -EBUSY and no recovery runs.

Do the recovery in the work itself instead: stop the controller, tear down the association, move to CONNECTING and schedule a reconnect, same as nvme_fc_reset_ctrl_work(). If the CONNECTING transition fails the controller is being deleted and the delete path finishes the job. This is how rdma and tcp structure their error recovery too.

The work now re-checks the controller state when it runs. A work queued while CONNECTING can execute after the connect succeeded and the controller went LIVE (a timed out connect command that completes right after the timeout fires). Claim RESETTING in that case; if that fails, a concurrent reset or delete owns the controller and will complete the outstanding IOs.

---
 drivers/nvme/host/fc.c | 96 +++++++++++++++++++++++++++---------------
 1 file changed, 61 insertions(+), 35 deletions(-)
drivers/nvme/host/fc.c +61 -35
index 6181cb7ea8ce..5a530aa37641 100644
@@ -229,6 +229,8 @@ static struct device *fc_udev_device;
 static void nvme_fc_complete_rq(struct request *rq);
 static void nvme_fc_start_ioerr_recovery(struct nvme_fc_ctrl *ctrl,
 					 char *errmsg);
+static void __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl,
+					    bool start_queues);
 
 /* *********************** FC-NVME Port Management ************************ */
 
@@ -987,7 +989,7 @@ fc_dma_unmap_sg(struct device *dev, struct scatterlist *sg, int nents,
 static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
 
-static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg);
+static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl);
 
 static void
 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
@@ -1873,8 +1875,44 @@ nvme_fc_ctrl_ioerr_work(struct work_struct *work)
 {
 	struct nvme_fc_ctrl *ctrl =
 			container_of(work, struct nvme_fc_ctrl, ioerr_work);
+	enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
+
+	/*
+	 * if an error (io timeout, etc) while (re)connecting, the remote
+	 * port requested terminating of the association (disconnect_ls)
+	 * or an error (timeout or abort) occurred on an io while creating
+	 * the controller.  Abort any ios on the association and let the
+	 * create_association error path resolve things.
+	 */
+	if (state == NVME_CTRL_CONNECTING) {
+		__nvme_fc_abort_outstanding_ios(ctrl, true);
+		dev_warn(ctrl->ctrl.device,
+			 "NVME-FC{%d}: transport error during (re)connect\n",
+			 ctrl->cnum);
+		return;
+	}
+
+	/*
+	 * Tear the association down only if this work owns recovery via a
+	 * RESETTING claim, or if the delete path is waiting for IOs to
+	 * complete.
+	 */
+	if (state == NVME_CTRL_LIVE &&
+	    nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING))
+		state = NVME_CTRL_RESETTING;
 
-	nvme_fc_error_recovery(ctrl, "transport detected io error");
+	switch (state) {
+	case NVME_CTRL_RESETTING:
+	case NVME_CTRL_DELETING:
+	case NVME_CTRL_DELETING_NOIO:
+		nvme_fc_error_recovery(ctrl);
+		break;
+	default:
+		dev_warn(ctrl->ctrl.device,
+			 "NVME-FC{%d}: error recovery skipped, state %d owns recovery\n",
+			 ctrl->cnum, nvme_ctrl_state(&ctrl->ctrl));
+		break;
+	}
 }
 
 /*
@@ -2533,39 +2571,6 @@ __nvme_fc_abort_outstanding_ios(struct nvme_fc_ctrl *ctrl, bool start_queues)
 		nvme_unquiesce_admin_queue(&ctrl->ctrl);
 }
 
-static void
-nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
-{
-	enum nvme_ctrl_state state = nvme_ctrl_state(&ctrl->ctrl);
-
-	/*
-	 * if an error (io timeout, etc) while (re)connecting, the remote
-	 * port requested terminating of the association (disconnect_ls)
-	 * or an error (timeout or abort) occurred on an io while creating
-	 * the controller.  Abort any ios on the association and let the
-	 * create_association error path resolve things.
-	 */
-	if (state == NVME_CTRL_CONNECTING) {
-		__nvme_fc_abort_outstanding_ios(ctrl, true);
-		dev_warn(ctrl->ctrl.device,
-			"NVME-FC{%d}: transport error during (re)connect\n",
-			ctrl->cnum);
-		return;
-	}
-
-	/* Otherwise, only proceed if in LIVE state - e.g. on first error */
-	if (state != NVME_CTRL_LIVE)
-		return;
-
-	dev_warn(ctrl->ctrl.device,
-		"NVME-FC{%d}: transport association event: %s\n",
-		ctrl->cnum, errmsg);
-	dev_warn(ctrl->ctrl.device,
-		"NVME-FC{%d}: resetting controller\n", ctrl->cnum);
-
-	nvme_reset_ctrl(&ctrl->ctrl);
-}
-
 static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq)
 {
 	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(rq);
@@ -3389,6 +3394,27 @@ nvme_fc_reset_ctrl_work(struct work_struct *work)
 	}
 }
 
+static void
+nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl)
+{
+	nvme_stop_keep_alive(&ctrl->ctrl);
+	flush_work(&ctrl->ctrl.async_event_work);
+
+	/* will block while waiting for io to terminate */
+	nvme_fc_delete_association(ctrl);
+	nvme_stop_ctrl(&ctrl->ctrl);
+
+	/* Do not reconnect if controller is being deleted */
+	if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING))
+		return;
+
+	if (ctrl->rport->remoteport.port_state == FC_OBJSTATE_ONLINE) {
+		queue_delayed_work(nvme_wq, &ctrl->connect_work, 0);
+		return;
+	}
+
+	nvme_fc_reconnect_or_delete(ctrl, -ENOTCONN);
+}
 
 static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
 	.name			= "fc",
Signature · 1 line
2.55.0
#57 lore raw

PATCH v6 14/18 nvme-fc: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella

An alive nvme controller that hits an error now will move to FENCING state instead of RESETTING state. ctrl->fencing_work attempts CCR to terminate inflight IOs. Regardless of the success or failure of CCR operation the controller is transitioned to RESETTING state to continue error recovery process.

---
 drivers/nvme/host/fc.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/nvme/host/fc.c +42 -0
index 5a530aa37641..42671cca439f 100644
@@ -166,6 +166,7 @@ struct nvme_fc_ctrl {
 	struct blk_mq_tag_set	admin_tag_set;
 	struct blk_mq_tag_set	tag_set;
 
+	struct work_struct	fencing_work;
 	struct work_struct	ioerr_work;
 	struct delayed_work	connect_work;
 
@@ -990,6 +991,7 @@ static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
 
 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl);
+static void nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl);
 
 static void
 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
@@ -1870,6 +1872,30 @@ __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
 	}
 }
 
+static void nvme_fc_fencing_work(struct work_struct *work)
+{
+	struct nvme_fc_ctrl *fc_ctrl =
+			container_of(work, struct nvme_fc_ctrl, fencing_work);
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+	unsigned long rem;
+
+	/*
+	 * Tear the association down now so the LLDD can finish unregistering
+	 * the remoteport/localport while the fencing this controller.
+	 */
+	nvme_stop_keep_alive(ctrl);
+	cancel_work_sync(&ctrl->async_event_work);
+	nvme_fc_delete_association(fc_ctrl);
+
+	rem = nvme_fence_ctrl(ctrl);
+	if (rem)
+		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &fc_ctrl->ioerr_work);
+}
+
 static void
 nvme_fc_ctrl_ioerr_work(struct work_struct *work)
 {
@@ -1950,6 +1976,14 @@ nvme_fc_start_ioerr_recovery(struct nvme_fc_ctrl *ctrl, char *errmsg)
 		return;
 	}
 
+	if (nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_FENCING)) {
+		dev_warn(ctrl->ctrl.device,
+			 "NVME-FC{%d}: starting controller fencing %s\n",
+			 ctrl->cnum, errmsg);
+		queue_work(nvme_wq, &ctrl->fencing_work);
+		return;
+	}
+
 	if (nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_RESETTING)) {
 		dev_warn(ctrl->ctrl.device, "NVME-FC{%d}: starting error recovery %s\n",
 			 ctrl->cnum, errmsg);
@@ -2463,6 +2497,12 @@ nvme_fc_ctrl_get(struct nvme_fc_ctrl *ctrl)
 	return kref_get_unless_zero(&ctrl->ref);
 }
 
+static void
+nvme_fc_stop_ctrl(struct nvme_ctrl *nctrl)
+{
+	flush_work(&to_fc_ctrl(nctrl)->fencing_work);
+}
+
 /*
  * All accesses from nvme core layer done - can now free the
  * controller. Called after last nvme_put_ctrl() call
@@ -3428,6 +3468,7 @@ static const struct nvme_ctrl_ops nvme_fc_ctrl_ops = {
 	.submit_async_event	= nvme_fc_submit_async_event,
 	.delete_ctrl		= nvme_fc_delete_ctrl,
 	.get_address		= nvmf_get_address,
+	.stop_ctrl		= nvme_fc_stop_ctrl,
 	.get_virt_boundary	= nvmf_get_virt_boundary,
 };
 
@@ -3547,6 +3588,7 @@ nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
 
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
+	INIT_WORK(&ctrl->fencing_work, nvme_fc_fencing_work);
 	INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
 	spin_lock_init(&ctrl->lock);
 
Signature · 1 line
2.55.0
#60 lore raw

PATCH v6 15/18 nvme-fc: Hold inflight requests while in FENCING state

In reply to Mohamed Khalfella

With the association torn down at the start of nvme_fc_fencing_work(), canceling the held requests inside nvme_fc_delete_association() would complete them at fence START, letting them fail over to other paths before CCR/CQT guarantees the target can no longer execute the original commands - defeating the hold this patch introduces.

Split nvme_fc_delete_association() into __nvme_fc_teardown_association() (LLDD-facing teardown, no request completion) and nvme_fc_flush_held_requests() (cancel tagsets + unquiesce queues). nvme_fc_fencing_work() now runs only the teardown half up front and flushes the held requests on the FENCING -> FENCED transition. The queues stay quiesced across the fence so nothing new reaches the dead association.

Update nvme_fc_fcpio_done() to not complete aborted requests or requests with transport errors. Held requests cannot be forced out mid-fence by the block layer, nvme_fc_timeout() always returns BLK_EH_RESET_TIMER and nvme_fc_start_ioerr_recovery() is a no-op in FENCING state. Every held op is FCPOP_STATE_COMPLETE before the fence starts (aborts drained, synchronize_rcu() done), so the flush never touches the LLDD.

---
 drivers/nvme/host/fc.c | 116 +++++++++++++++++++++++++++++++++--------
 1 file changed, 94 insertions(+), 22 deletions(-)
drivers/nvme/host/fc.c +94 -22
index 42671cca439f..a6e0fa2dc341 100644
@@ -172,7 +172,7 @@ struct nvme_fc_ctrl {
 
 	struct kref		ref;
 	unsigned long		flags;
-	u32			iocnt;
+	atomic_t		iocnt;
 	wait_queue_head_t	ioabort_wait;
 
 	struct nvme_fc_fcp_op	aen_ops[NVME_NR_AEN_COMMANDS];
@@ -991,7 +991,8 @@ static void nvme_fc_ctrl_put(struct nvme_fc_ctrl *);
 static int nvme_fc_ctrl_get(struct nvme_fc_ctrl *);
 
 static void nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl);
-static void nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl);
+static void __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl);
+static void nvme_fc_flush_held_requests(struct nvme_fc_ctrl *ctrl);
 
 static void
 __nvme_fc_finish_ls_req(struct nvmefc_ls_req_op *lsop)
@@ -1826,7 +1827,7 @@ __nvme_fc_abort_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_fcp_op *op)
 		atomic_set(&op->state, opstate);
 	else if (test_bit(FCCTRL_TERMIO, &ctrl->flags)) {
 		op->flags |= FCOP_FLAGS_TERMIO;
-		ctrl->iocnt++;
+		atomic_inc(&ctrl->iocnt);
 	}
 	spin_unlock_irqrestore(&ctrl->lock, flags);
 
@@ -1856,20 +1857,29 @@ nvme_fc_abort_aen_ops(struct nvme_fc_ctrl *ctrl)
 }
 
 static inline void
+__nvme_fc_fcpop_count_one_down(struct nvme_fc_ctrl *ctrl)
+{
+	if (atomic_dec_return(&ctrl->iocnt) == 0)
+		wake_up(&ctrl->ioabort_wait);
+}
+
+static inline bool
 __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
 		struct nvme_fc_fcp_op *op, int opstate)
 {
 	unsigned long flags;
+	bool ret = false;
 
 	if (opstate == FCPOP_STATE_ABORTED) {
 		spin_lock_irqsave(&ctrl->lock, flags);
 		if (test_bit(FCCTRL_TERMIO, &ctrl->flags) &&
 		    op->flags & FCOP_FLAGS_TERMIO) {
-			if (!--ctrl->iocnt)
-				wake_up(&ctrl->ioabort_wait);
+			ret = true;
 		}
 		spin_unlock_irqrestore(&ctrl->lock, flags);
 	}
+
+	return ret;
 }
 
 static void nvme_fc_fencing_work(struct work_struct *work)
@@ -1885,12 +1895,14 @@ static void nvme_fc_fencing_work(struct work_struct *work)
 	 */
 	nvme_stop_keep_alive(ctrl);
 	cancel_work_sync(&ctrl->async_event_work);
-	nvme_fc_delete_association(fc_ctrl);
+	if (test_and_clear_bit(ASSOC_ACTIVE, &fc_ctrl->flags))
+		__nvme_fc_teardown_association(fc_ctrl);
 
 	rem = nvme_fence_ctrl(ctrl);
 	if (rem)
 		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
 
+	nvme_fc_flush_held_requests(fc_ctrl);
 	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
 	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
 		queue_work(nvme_reset_wq, &fc_ctrl->ioerr_work);
@@ -2003,7 +2015,8 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 	struct nvme_command *sqe = &op->cmd_iu.sqe;
 	__le16 status = cpu_to_le16(NVME_SC_SUCCESS << 1);
 	union nvme_result result;
-	bool terminate_assoc = true;
+	bool op_term, terminate_assoc = true;
+	enum nvme_ctrl_state state;
 	int opstate;
 
 	/*
@@ -2043,6 +2056,9 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 	 * association to be terminated.
 	 */
 
+	/* Pairs with synchronize_rcu() in __nvme_fc_teardown_association() */
+	rcu_read_lock();
+
 	opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
 
 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
@@ -2136,20 +2152,44 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 done:
 	if (op->flags & FCOP_FLAGS_AEN) {
 		nvme_complete_async_event(&queue->ctrl->ctrl, status, &result);
-		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
+		if (__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate))
+			__nvme_fc_fcpop_count_one_down(ctrl);
 		atomic_set(&op->state, FCPOP_STATE_IDLE);
 		op->flags = FCOP_FLAGS_AEN;	/* clear other flags */
 		nvme_fc_ctrl_put(ctrl);
 		goto check_error;
 	}
 
-	__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
+	/*
+	 * We can not access op after the request is completed because it can
+	 * be reused immediately. At the same time we want to wakeup the thread
+	 * waiting for ongoing IOs _after_ requests are completed. This is
+	 * necessary because that thread will start canceling inflight IOs
+	 * and we want to avoid request completion racing with cancellation.
+	 */
+	op_term = __nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
+
+	/*
+	 * If we are going to terminate associations and the controller is
+	 * LIVE or FENCING, then do not complete this request now. Let error
+	 * recovery cancel this request when it is safe to do so.
+	 */
+	state = nvme_ctrl_state(&ctrl->ctrl);
+	if (terminate_assoc &&
+	    (state == NVME_CTRL_LIVE || state == NVME_CTRL_FENCING))
+		goto check_op_term;
+
 	if (!nvme_try_complete_req(rq, status, result))
 		nvme_fc_complete_rq(rq);
+check_op_term:
+	if (op_term)
+		__nvme_fc_fcpop_count_one_down(ctrl);
 
 check_error:
 	if (terminate_assoc)
 		nvme_fc_start_ioerr_recovery(ctrl, "io error");
+
+	rcu_read_unlock();
 }
 
 static int
@@ -2802,7 +2842,8 @@ nvme_fc_start_fcp_op(struct nvme_fc_ctrl *ctrl, struct nvme_fc_queue *queue,
 		 * cmd with the csn was supposed to arrive.
 		 */
 		opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
-		__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate);
+		if (__nvme_fc_fcpop_chk_teardowns(ctrl, op, opstate))
+			__nvme_fc_fcpop_count_one_down(ctrl);
 
 		if (!(op->flags & FCOP_FLAGS_AEN)) {
 			nvme_fc_unmap_data(ctrl, op->rq, op);
@@ -3260,25 +3301,25 @@ nvme_fc_create_association(struct nvme_fc_ctrl *ctrl)
 	return ret;
 }
 
-
 /*
- * This routine stops operation of the controller on the host side.
- * On the host os stack side: Admin and IO queues are stopped,
- *   outstanding ios on them terminated via FC ABTS.
- * On the link side: the association is terminated.
+ * This routine tears down the association on the link side and releases
+ * all LLDD resources: outstanding ios are terminated via FC ABTS, hw
+ * queues are deleted and the controller is marked inactive on the
+ * rport. Requests that nvme_fc_fcpio_done() decided to hold are NOT
+ * completed here, the caller must follow up with
+ * nvme_fc_flush_held_requests() once it is safe to release them.
+ *
+ * The caller must own the ASSOC_ACTIVE bit (test_and_clear_bit()).
  */
 static void
-nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
+__nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl)
 {
 	struct nvmefc_ls_rcv_op *disls = NULL;
 	unsigned long flags;
 
-	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
-		return;
-
 	spin_lock_irqsave(&ctrl->lock, flags);
 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
-	ctrl->iocnt = 0;
+	atomic_set(&ctrl->iocnt, 0);
 	spin_unlock_irqrestore(&ctrl->lock, flags);
 
 	__nvme_fc_abort_outstanding_ios(ctrl, false);
@@ -3287,11 +3328,18 @@ nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
 	nvme_fc_abort_aen_ops(ctrl);
 
 	/* wait for all io that had to be aborted */
+	wait_event(ctrl->ioabort_wait, atomic_read(&ctrl->iocnt) == 0);
 	spin_lock_irq(&ctrl->lock);
-	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
 	spin_unlock_irq(&ctrl->lock);
 
+	/*
+	 * Wait for a started nvme_fc_fcpio_done() to complete before we
+	 * proceed with cancelling inflight requests. This guarantees a
+	 * request is completed by one of the two codepaths, not both.
+	 */
+	synchronize_rcu();
+
 	nvme_fc_term_aen_ops(ctrl);
 
 	/*
@@ -3323,13 +3371,37 @@ nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
 	__nvme_fc_delete_hw_queue(ctrl, &ctrl->queues[0], 0);
 	nvme_fc_free_queue(&ctrl->queues[0]);
 
+	nvme_fc_ctlr_inactive_on_rport(ctrl);
+}
+
+/* Complete the requests held in __nvme_fc_teardown_association() */
+static void
+nvme_fc_flush_held_requests(struct nvme_fc_ctrl *ctrl)
+{
+	nvme_cancel_tagset(&ctrl->ctrl);
+	nvme_cancel_admin_tagset(&ctrl->ctrl);
+
 	/* re-enable the admin_q so anything new can fast fail */
 	nvme_unquiesce_admin_queue(&ctrl->ctrl);
 
 	/* resume the io queues so that things will fast fail */
 	nvme_unquiesce_io_queues(&ctrl->ctrl);
+}
 
-	nvme_fc_ctlr_inactive_on_rport(ctrl);
+/*
+ * This routine stops operation of the controller on the host side.
+ * On the host os stack side: Admin and IO queues are stopped,
+ *   outstanding ios on them terminated via FC ABTS.
+ * On the link side: the association is terminated.
+ */
+static void
+nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
+{
+	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
+		return;
+
+	__nvme_fc_teardown_association(ctrl);
+	nvme_fc_flush_held_requests(ctrl);
 }
 
 static void
Signature · 1 line
2.55.0
#68 lore raw

PATCH v6 16/18 nvmet: Add support for CQT to nvme target

In reply to Mohamed Khalfella

TP4129 KATO Corrections and Clarifications defined CQT (Command Quiesce Time) which is used along with KATO (Keep Alive Timeout) to set an upper time limit for attempting Cross-Controller Recovery. CQT is added as a subsystem attribute that defaults to 0 to maintain the current behavior.

---
 drivers/nvme/target/admin-cmd.c |  1 +
 drivers/nvme/target/configfs.c  | 36 +++++++++++++++++++++++++++++++++
 drivers/nvme/target/core.c      |  3 +++
 drivers/nvme/target/nvmet.h     |  2 ++
 include/linux/nvme.h            |  5 ++++-
 5 files changed, 46 insertions(+), 1 deletion(-)
drivers/nvme/target/admin-cmd.c +1 -0
index a60b2f271e98..c0cd233048eb 100644
@@ -739,6 +739,7 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
 	id->cntlid = cpu_to_le16(ctrl->cntlid);
 	id->ver = cpu_to_le32(ctrl->subsys->ver);
 	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		id->cqt = cpu_to_le16(ctrl->cqt);
 		id->ciu = ctrl->ciu;
 		id->cirn = cpu_to_le64(ctrl->cirn);
 		id->ccrl = NVMF_CCR_LIMIT;
drivers/nvme/target/configfs.c +36 -0
index 6286e38436dd..88a59ac1e93a 100644
@@ -1663,6 +1663,41 @@ static ssize_t nvmet_subsys_attr_pi_enable_store(struct config_item *item,
 CONFIGFS_ATTR(nvmet_subsys_, attr_pi_enable);
 #endif
 
+static ssize_t nvmet_subsys_attr_cqt_show(struct config_item *item,
+					  char *page)
+{
+	return snprintf(page, PAGE_SIZE, "%u\n", to_subsys(item)->cqt);
+}
+
+static ssize_t nvmet_subsys_attr_cqt_store(struct config_item *item,
+					   const char *page, size_t cnt)
+{
+	struct nvmet_subsys *subsys = to_subsys(item);
+	struct nvmet_ctrl *ctrl;
+	u16 cqt;
+
+	if (nvmet_is_disc_subsys(subsys))
+		return -EINVAL;
+
+	if (sscanf(page, "%hu\n", &cqt) != 1)
+		return -EINVAL;
+
+	down_write(&nvmet_config_sem);
+	mutex_lock(&subsys->lock);
+	if (subsys->cqt == cqt)
+		goto out;
+
+	subsys->cqt = cqt;
+	/* Force reconnect */
+	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry)
+		ctrl->ops->delete_ctrl(ctrl);
+out:
+	mutex_unlock(&subsys->lock);
+	up_write(&nvmet_config_sem);
+	return cnt;
+}
+CONFIGFS_ATTR(nvmet_subsys_, attr_cqt);
+
 static ssize_t nvmet_subsys_attr_qid_max_show(struct config_item *item,
 					      char *page)
 {
@@ -1703,6 +1738,7 @@ static struct configfs_attribute *nvmet_subsys_attrs[] = {
 	&nvmet_subsys_attr_attr_vendor_id,
 	&nvmet_subsys_attr_attr_subsys_vendor_id,
 	&nvmet_subsys_attr_attr_model,
+	&nvmet_subsys_attr_attr_cqt,
 	&nvmet_subsys_attr_attr_qid_max,
 	&nvmet_subsys_attr_attr_ieee_oui,
 	&nvmet_subsys_attr_attr_firmware,
drivers/nvme/target/core.c +3 -0
index 69491e8e52ba..a93cf7e3a704 100644
@@ -1753,6 +1753,7 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 	ctrl->cntlid = ret;
 
 	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		ctrl->cqt = subsys->cqt;
 		ctrl->ciu = get_random_u8() ? : 1;
 		ctrl->cirn = get_random_u64();
 	}
@@ -1980,10 +1981,12 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
 
 	switch (type) {
 	case NVME_NQN_NVME:
+		subsys->cqt = NVMF_CQT_MS;
 		subsys->max_qid = NVMET_NR_QUEUES;
 		break;
 	case NVME_NQN_DISC:
 	case NVME_NQN_CURR:
+		subsys->cqt = 0;
 		subsys->max_qid = 0;
 		break;
 	default:
drivers/nvme/target/nvmet.h +2 -0
index 88fa11aa0587..ddf88c772938 100644
@@ -270,6 +270,7 @@ struct nvmet_ctrl {
 	uuid_t			hostid;
 	u16			cntlid;
 	u16			max_qid;
+	u16			cqt;
 	u8			ciu;
 	u32			kato;
 	u64			cirn;
@@ -347,6 +348,7 @@ struct nvmet_subsys {
 #ifdef CONFIG_NVME_TARGET_DEBUGFS
 	struct dentry		*debugfs_dir;
 #endif
+	u16			cqt;
 	u16			max_qid;
 
 	u64			ver;
include/linux/nvme.h +4 -1
index 3757d11ffea3..0b69ed53f347 100644
@@ -21,6 +21,7 @@
 #define NVMF_TRADDR_SIZE	256
 #define NVMF_TSAS_SIZE		256
 
+#define NVMF_CQT_MS		0
 #define NVMF_CCR_LIMIT		4
 #define NVMF_CCR_PER_PAGE	511
 
@@ -368,7 +369,9 @@ struct nvme_id_ctrl {
 	__u8			anacap;
 	__le32			anagrpmax;
 	__le32			nanagrpid;
-	__u8			rsvd352[160];
+	__u8			rsvd352[34];
+	__le16			cqt;
+	__u8			rsvd388[124];
 	__u8			sqes;
 	__u8			cqes;
 	__le16			maxcmd;
Signature · 1 line
2.55.0
#70 lore raw

PATCH v6 17/18 nvme: Add support for CQT to nvme host

In reply to Mohamed Khalfella

TP4129 KATO Corrections and Clarifications defined CQT (Command Quiesce Time) which is used along with KATO (Keep Alive Timeout) to set an upper limit for attempting Cross-Controller Recovery. Add ctrl->cqt, read its value from controller identify response, expose it via sysfs, and have nvme_fence_timeout_ms() account for it.

Use CQT to drive time-based recovery in fc, rdma, and tcp transports. Add a fenced_work delayed work to each transport controller. If CCR fails, fencing_work switches to error recovery immediately if CQT is not supported. Otherwise it schedules fenced_work after the remaining fence timeout, which also switches to error recovery when scheduled.

---
 drivers/nvme/host/core.c  |  1 +
 drivers/nvme/host/fc.c    | 33 +++++++++++++++++++++++++++++++--
 drivers/nvme/host/nvme.h  |  5 +++--
 drivers/nvme/host/rdma.c  | 32 ++++++++++++++++++++++++++++++--
 drivers/nvme/host/sysfs.c |  2 ++
 drivers/nvme/host/tcp.c   | 32 ++++++++++++++++++++++++++++++--
 6 files changed, 97 insertions(+), 8 deletions(-)
drivers/nvme/host/core.c +1 -0
index 5509b58b51db..dc44e3af5f14 100644
@@ -3844,6 +3844,7 @@ static int nvme_init_identify(struct nvme_ctrl *ctrl)
 	ctrl->ciu = id->ciu;
 	ctrl->cirn = le64_to_cpu(id->cirn);
 	ctrl->ccrl = id->ccrl;
+	ctrl->cqt = le16_to_cpu(id->cqt);
 
 	ctrl->oacs = le16_to_cpu(id->oacs);
 	ctrl->oncs = le16_to_cpu(id->oncs);
drivers/nvme/host/fc.c +31 -2
index a6e0fa2dc341..3db111accd0c 100644
@@ -167,6 +167,7 @@ struct nvme_fc_ctrl {
 	struct blk_mq_tag_set	tag_set;
 
 	struct work_struct	fencing_work;
+	struct delayed_work	fenced_work;
 	struct work_struct	ioerr_work;
 	struct delayed_work	connect_work;
 
@@ -1882,6 +1883,19 @@ __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl,
 	return ret;
 }
 
+static void nvme_fc_fenced_work(struct work_struct *work)
+{
+	struct nvme_fc_ctrl *fc_ctrl = container_of(to_delayed_work(work),
+					struct nvme_fc_ctrl, fenced_work);
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+
+	dev_info(ctrl->device, "Time-based recovery finished\n");
+	nvme_fc_flush_held_requests(fc_ctrl);
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &fc_ctrl->ioerr_work);
+}
+
 static void nvme_fc_fencing_work(struct work_struct *work)
 {
 	struct nvme_fc_ctrl *fc_ctrl =
@@ -1899,9 +1913,22 @@ static void nvme_fc_fencing_work(struct work_struct *work)
 		__nvme_fc_teardown_association(fc_ctrl);
 
 	rem = nvme_fence_ctrl(ctrl);
-	if (rem)
-		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+	if (!rem)
+		goto done;
 
+	if (!ctrl->cqt) {
+		dev_info(ctrl->device,
+			 "CCR failed, CQT not supported, skip time-based recovery\n");
+		goto done;
+	}
+
+	dev_info(ctrl->device,
+		 "CCR failed, switch to time-based recovery, timeout = %ums\n",
+		 jiffies_to_msecs(rem));
+	queue_delayed_work(nvme_wq, &fc_ctrl->fenced_work, rem);
+	return;
+
+done:
 	nvme_fc_flush_held_requests(fc_ctrl);
 	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
 	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
@@ -2541,6 +2568,7 @@ static void
 nvme_fc_stop_ctrl(struct nvme_ctrl *nctrl)
 {
 	flush_work(&to_fc_ctrl(nctrl)->fencing_work);
+	flush_delayed_work(&to_fc_ctrl(nctrl)->fenced_work);
 }
 
 /*
@@ -3661,6 +3689,7 @@ nvme_fc_alloc_ctrl(struct device *dev, struct nvmf_ctrl_options *opts,
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_fc_reset_ctrl_work);
 	INIT_DELAYED_WORK(&ctrl->connect_work, nvme_fc_connect_ctrl_work);
 	INIT_WORK(&ctrl->fencing_work, nvme_fc_fencing_work);
+	INIT_DELAYED_WORK(&ctrl->fenced_work, nvme_fc_fenced_work);
 	INIT_WORK(&ctrl->ioerr_work, nvme_fc_ctrl_ioerr_work);
 	spin_lock_init(&ctrl->lock);
 
drivers/nvme/host/nvme.h +3 -2
index 7096f1e4f84e..bcecaaf06f7e 100644
@@ -401,6 +401,7 @@ struct nvme_ctrl {
 	u32 max_zone_append;
 #endif
 	u16 crdt[3];
+	u16 cqt;
 	u16 oncs;
 	u8 dmrl;
 	u8 ciu;
@@ -1360,8 +1361,8 @@ static inline bool nvme_multi_css(struct nvme_ctrl *ctrl)
 static inline unsigned long nvme_fence_timeout_ms(struct nvme_ctrl *ctrl)
 {
 	if (ctrl->ctratt & NVME_CTRL_ATTR_TBKAS)
-		return 3 * ctrl->kato * 1000;
-	return 2 * ctrl->kato * 1000;
+		return 3 * ctrl->kato * 1000 + ctrl->cqt;
+	return 2 * ctrl->kato * 1000 + ctrl->cqt;
 }
 
 #endif /* _NVME_H */
drivers/nvme/host/rdma.c +30 -2
index 9c50421d79c4..8ff5aa312377 100644
@@ -120,6 +120,7 @@ struct nvme_rdma_ctrl {
 	/* other member variables */
 	struct blk_mq_tag_set	tag_set;
 	struct work_struct	fencing_work;
+	struct delayed_work	fenced_work;
 	struct work_struct	err_work;
 
 	struct nvme_rdma_qe	async_event_sqe;
@@ -994,6 +995,7 @@ static void nvme_rdma_stop_ctrl(struct nvme_ctrl *nctrl)
 	struct nvme_rdma_ctrl *ctrl = to_rdma_ctrl(nctrl);
 
 	flush_work(&ctrl->fencing_work);
+	flush_delayed_work(&ctrl->fenced_work);
 	flush_work(&ctrl->err_work);
 	cancel_delayed_work_sync(&ctrl->reconnect_work);
 }
@@ -1155,6 +1157,18 @@ static void nvme_rdma_reconnect_ctrl_work(struct work_struct *work)
 	nvme_rdma_reconnect_or_remove(ctrl, ret);
 }
 
+static void nvme_rdma_fenced_work(struct work_struct *work)
+{
+	struct nvme_rdma_ctrl *rdma_ctrl = container_of(to_delayed_work(work),
+					   struct nvme_rdma_ctrl, fenced_work);
+	struct nvme_ctrl *ctrl = &rdma_ctrl->ctrl;
+
+	dev_info(ctrl->device, "Time-based recovery finished\n");
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &rdma_ctrl->err_work);
+}
+
 static void nvme_rdma_fencing_work(struct work_struct *work)
 {
 	struct nvme_rdma_ctrl *rdma_ctrl = container_of(work,
@@ -1163,9 +1177,22 @@ static void nvme_rdma_fencing_work(struct work_struct *work)
 	unsigned long rem;
 
 	rem = nvme_fence_ctrl(ctrl);
-	if (rem)
-		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+	if (!rem)
+		goto done;
+
+	if (!ctrl->cqt) {
+		dev_info(ctrl->device,
+			 "CCR failed, CQT not supported, skip time-based recovery\n");
+		goto done;
+	}
+
+	dev_info(ctrl->device,
+		 "CCR failed, switch to time-based recovery, timeout = %ums\n",
+		 jiffies_to_msecs(rem));
+	queue_delayed_work(nvme_wq, &rdma_ctrl->fenced_work, rem);
+	return;
 
+done:
 	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
 	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
 		queue_work(nvme_reset_wq, &rdma_ctrl->err_work);
@@ -2354,6 +2381,7 @@ static struct nvme_rdma_ctrl *nvme_rdma_alloc_ctrl(struct device *dev,
 	INIT_DELAYED_WORK(&ctrl->reconnect_work,
 			nvme_rdma_reconnect_ctrl_work);
 	INIT_WORK(&ctrl->fencing_work, nvme_rdma_fencing_work);
+	INIT_DELAYED_WORK(&ctrl->fenced_work, nvme_rdma_fenced_work);
 	INIT_WORK(&ctrl->err_work, nvme_rdma_error_recovery_work);
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_rdma_reset_ctrl_work);
 
drivers/nvme/host/sysfs.c +2 -0
index 3a12b07149d2..e8dad4722347 100644
@@ -486,6 +486,7 @@ nvme_show_int_function(numa_node);
 nvme_show_int_function(queue_count);
 nvme_show_int_function(sqsize);
 nvme_show_int_function(kato);
+nvme_show_int_function(cqt);
 
 static ssize_t nvme_sysfs_ciu_show(struct device *dev,
 					  struct device_attribute *attr,
@@ -966,6 +967,7 @@ static struct attribute *nvme_dev_attrs[] = {
 	&dev_attr_sqsize.attr,
 	&dev_attr_ciu.attr,
 	&dev_attr_cirn.attr,
+	&dev_attr_cqt.attr,
 	&dev_attr_hostnqn.attr,
 	&dev_attr_hostid.attr,
 	&dev_attr_ctrl_loss_tmo.attr,
drivers/nvme/host/tcp.c +30 -2
index febbe8954473..7b1d9e2cb00d 100644
@@ -168,6 +168,7 @@ struct nvme_tcp_ctrl {
 	struct nvme_ctrl	ctrl;
 
 	struct work_struct	fencing_work;
+	struct delayed_work	fenced_work;
 	struct work_struct	err_work;
 	struct delayed_work	connect_work;
 	struct nvme_tcp_request async_req;
@@ -2565,6 +2566,18 @@ static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
 	nvme_tcp_reconnect_or_remove(ctrl, ret);
 }
 
+static void nvme_tcp_fenced_work(struct work_struct *work)
+{
+	struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
+					 struct nvme_tcp_ctrl, fenced_work);
+	struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
+
+	dev_info(ctrl->device, "Time-based recovery finished\n");
+	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
+	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
+		queue_work(nvme_reset_wq, &tcp_ctrl->err_work);
+}
+
 static void nvme_tcp_fencing_work(struct work_struct *work)
 {
 	struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
@@ -2573,9 +2586,22 @@ static void nvme_tcp_fencing_work(struct work_struct *work)
 	unsigned long rem;
 
 	rem = nvme_fence_ctrl(ctrl);
-	if (rem)
-		dev_info(ctrl->device, "CCR failed, starting error recovery\n");
+	if (!rem)
+		goto done;
+
+	if (!ctrl->cqt) {
+		dev_info(ctrl->device,
+			 "CCR failed, CQT not supported, skip time-based recovery\n");
+		goto done;
+	}
 
+	dev_info(ctrl->device,
+		 "CCR failed, switch to time-based recovery, timeout = %ums\n",
+		 jiffies_to_msecs(rem));
+	queue_delayed_work(nvme_wq, &tcp_ctrl->fenced_work, rem);
+	return;
+
+done:
 	nvme_change_ctrl_state(ctrl, NVME_CTRL_FENCED);
 	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
 		queue_work(nvme_reset_wq, &tcp_ctrl->err_work);
@@ -2657,6 +2683,7 @@ static void nvme_reset_ctrl_work(struct work_struct *work)
 static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
 {
 	flush_work(&to_tcp_ctrl(ctrl)->fencing_work);
+	flush_delayed_work(&to_tcp_ctrl(ctrl)->fenced_work);
 	flush_work(&to_tcp_ctrl(ctrl)->err_work);
 	cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
 }
@@ -3023,6 +3050,7 @@ static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
 	INIT_DELAYED_WORK(&ctrl->connect_work,
 			nvme_tcp_reconnect_ctrl_work);
 	INIT_WORK(&ctrl->fencing_work, nvme_tcp_fencing_work);
+	INIT_DELAYED_WORK(&ctrl->fenced_work, nvme_tcp_fenced_work);
 	INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
 	INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
 
Signature · 1 line
2.55.0
#73 lore raw

PATCH v6 18/18 nvme: let controller deletion wait out a fencing window

In reply to Mohamed Khalfella

DELETING is not reachable from FENCING or FENCED, so during a fencing window nvme_delete_ctrl() fails with -EBUSY. Its callers assume deleting a live controller cannot fail: a sysfs disconnect is silently dropped after the delete_controller node is already gone, rdma device removal returns with the controller still holding resources on the outgoing device, and module unload leaks live controllers.

Fencing is time-bounded and always ends in a state that allows deletion. Add nvme_delete_ctrl_wait(), which waits on ctrl->state_wq for the fencing window to end and retries, and use it in the tcp/rdma module exit paths and rdma device removal. Make nvme_delete_ctrl_sync() wait the same way so a sysfs disconnect during fencing is delayed instead of dropped.

nvme-fc's remoteport unregister with dev_loss_tmo == 0 keeps the non-waiting call since it runs under rport->lock; it does not need to wait, as dev_loss_end has already expired and the reconnect path deletes the controller once fencing completes.

---
 drivers/nvme/host/core.c | 41 ++++++++++++++++++++++++++++++++++++++--
 drivers/nvme/host/nvme.h |  1 +
 drivers/nvme/host/rdma.c |  4 ++--
 drivers/nvme/host/tcp.c  |  2 +-
 4 files changed, 43 insertions(+), 5 deletions(-)
drivers/nvme/host/core.c +39 -2
index dc44e3af5f14..ca7f083702f5 100644
@@ -271,6 +271,38 @@ int nvme_delete_ctrl(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_delete_ctrl);
 
+static bool nvme_ctrl_fencing(struct nvme_ctrl *ctrl)
+{
+	enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
+
+	return state == NVME_CTRL_FENCING || state == NVME_CTRL_FENCED;
+}
+
+static void nvme_wait_fencing_done(struct nvme_ctrl *ctrl)
+{
+	wait_event(ctrl->state_wq, !nvme_ctrl_fencing(ctrl));
+}
+
+/*
+ * Like nvme_delete_ctrl(), but waits for a fencing window to end
+ * instead of failing with -EBUSY. May sleep; callers that cannot
+ * sleep must use nvme_delete_ctrl() and handle the failure.
+ */
+int nvme_delete_ctrl_wait(struct nvme_ctrl *ctrl)
+{
+	int ret;
+
+	might_sleep();
+
+	for (;;) {
+		ret = nvme_delete_ctrl(ctrl);
+		if (!ret || !nvme_ctrl_fencing(ctrl))
+			return ret;
+		nvme_wait_fencing_done(ctrl);
+	}
+}
+EXPORT_SYMBOL_GPL(nvme_delete_ctrl_wait);
+
 void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
 {
 	/*
@@ -278,8 +310,13 @@ void nvme_delete_ctrl_sync(struct nvme_ctrl *ctrl)
 	 * since ->delete_ctrl can free the controller.
 	 */
 	nvme_get_ctrl(ctrl);
-	if (nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING))
-		nvme_do_delete_ctrl(ctrl);
+	while (!nvme_change_ctrl_state(ctrl, NVME_CTRL_DELETING)) {
+		if (!nvme_ctrl_fencing(ctrl))
+			goto out_put;
+		nvme_wait_fencing_done(ctrl);
+	}
+	nvme_do_delete_ctrl(ctrl);
+out_put:
 	nvme_put_ctrl(ctrl);
 }
 
drivers/nvme/host/nvme.h +1 -0
index bcecaaf06f7e..bf8c2bf91ad3 100644
@@ -1026,6 +1026,7 @@ void nvme_stop_keep_alive(struct nvme_ctrl *ctrl);
 int nvme_reset_ctrl(struct nvme_ctrl *ctrl);
 int nvme_reset_ctrl_sync(struct nvme_ctrl *ctrl);
 int nvme_delete_ctrl(struct nvme_ctrl *ctrl);
+int nvme_delete_ctrl_wait(struct nvme_ctrl *ctrl);
 void nvme_queue_scan(struct nvme_ctrl *ctrl);
 int nvme_get_log(struct nvme_ctrl *ctrl, u32 nsid, u8 log_page, u8 lsp, u8 csi,
 		void *log, size_t size, u64 offset);
drivers/nvme/host/rdma.c +2 -2
index 8ff5aa312377..ce37e3eafeae 100644
@@ -2483,7 +2483,7 @@ static void nvme_rdma_remove_one(struct ib_device *ib_device, void *client_data)
 	list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list) {
 		if (ctrl->device->dev != ib_device)
 			continue;
-		nvme_delete_ctrl(&ctrl->ctrl);
+		nvme_delete_ctrl_wait(&ctrl->ctrl);
 	}
 	mutex_unlock(&nvme_rdma_ctrl_mutex);
 
@@ -2523,7 +2523,7 @@ static void __exit nvme_rdma_cleanup_module(void)
 
 	mutex_lock(&nvme_rdma_ctrl_mutex);
 	list_for_each_entry(ctrl, &nvme_rdma_ctrl_list, list)
-		nvme_delete_ctrl(&ctrl->ctrl);
+		nvme_delete_ctrl_wait(&ctrl->ctrl);
 	mutex_unlock(&nvme_rdma_ctrl_mutex);
 	flush_workqueue(nvme_delete_wq);
 }
drivers/nvme/host/tcp.c +1 -1
index 7b1d9e2cb00d..9331a3e0bd9a 100644
@@ -3208,7 +3208,7 @@ static void __exit nvme_tcp_cleanup_module(void)
 
 	mutex_lock(&nvme_tcp_ctrl_mutex);
 	list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
-		nvme_delete_ctrl(&ctrl->ctrl);
+		nvme_delete_ctrl_wait(&ctrl->ctrl);
 	mutex_unlock(&nvme_tcp_ctrl_mutex);
 	flush_workqueue(nvme_delete_wq);
 
Signature · 1 line
2.55.0
Reviewed-by on v6 1/18
#2 lore raw

Re: [PATCH v6 01/18] nvmet: Rapid Path Failure Recovery set controller identify fields

In reply to Mohamed Khalfella
Quoted 2 lines

maximum number of in-progress controller reset operations. CCRL is hardcoded to 4 as recommended by TP8028.

The spec says at least 4.

drivers/nvme/target/core.c struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) · line 1704

Quoted 7 lines
drivers/nvme/target/core.c +3 -0
@@ -1695,6 +1699,11 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 	}
 	ctrl->cntlid = ret;
 
+	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		ctrl->ciu = get_random_u8() ? : 1;
+		ctrl->cirn = get_random_u64();

While get_random_u64 is very unlikely to return 0, it still possible, thus

   get_random_u64() ? : 1;

would be the safe choice. Besides this nitpicking:

Signature · 1 line
Daniel Wagner <[email protected]>
Reviewed-by on v6 2/18
#5 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Mohamed Khalfella
Quoted 2 lines · Mohamed Khalfella

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory.

Could you add the 'why' part to the commit message?

Quoted 4 lines

Besides the missing 'why', it looks good.

#12 lore raw

Re: [PATCH v6 03/18] nvmet: Implement CCR nvme command

In reply to Mohamed Khalfella
Quoted 57 lines
drivers/nvme/target/admin-cmd.c +57 -0
+void nvmet_execute_cross_ctrl_reset(struct nvmet_req *req)
+{
+	struct nvmet_ctrl *ictrl, *sctrl = req->sq->ctrl;
+	struct nvme_command *cmd = req->cmd;
+	struct nvmet_ccr *ccr, *new_ccr;
+	int ccr_active, ccr_total;
+	u16 cntlid, status = NVME_SC_SUCCESS;
+
+	if (!nvmet_check_transfer_len(req, 0))
+		return;
+
+	cntlid = le16_to_cpu(cmd->ccr.icid);
+	if (sctrl->cntlid == cntlid) {
+		req->error_loc =
+			offsetof(struct nvme_cross_ctrl_reset_cmd, icid);
+		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
+		goto out;
+	}
+
+	/* Find and get impacted controller */
+	ictrl = nvmet_ctrl_find_get_ccr(sctrl->subsys, sctrl->hostnqn,
+					cmd->ccr.ciu, cntlid,
+					le64_to_cpu(cmd->ccr.cirn));
+	if (!ictrl) {
+		/* Immediate Reset Successful */
+		nvmet_set_result(req, 1);
+		status = NVME_SC_SUCCESS;
+		goto out;
+	}
+
+	ccr_total = ccr_active = 0;
+	mutex_lock(&sctrl->lock);
+	list_for_each_entry(ccr, &sctrl->ccr_list, entry) {
+		if (ccr->ctrl == ictrl) {
+			status = NVME_SC_CCR_IN_PROGRESS | NVME_STATUS_DNR;
+			goto out_unlock;
+		}
+
+		ccr_total++;
+		if (ccr->ctrl)
+			ccr_active++;
+	}
+
+	if (ccr_active >= NVMF_CCR_LIMIT) {
+		status = NVME_SC_CCR_LIMIT_EXCEEDED;
+		goto out_unlock;
+	}
+	if (ccr_total >= NVMF_CCR_PER_PAGE) {
+		status = NVME_SC_CCR_LOGPAGE_FULL;
+		goto out_unlock;
+	}
+
+	new_ccr = kmalloc_obj(*new_ccr, GFP_KERNEL);
+	if (!new_ccr) {
+		status = NVME_SC_INTERNAL;
+		goto out_unlock;
+	}

drivers/nvme/target/admin-cmd.c void nvmet_execute_keep_alive(struct nvmet_req *req) · line 1683

Quoted 10 lines
drivers/nvme/target/admin-cmd.c +10 -0
+
+	new_ccr->ciu = cmd->ccr.ciu;
+	new_ccr->icid = cntlid;
+	new_ccr->ctrl = ictrl;
+	list_add_tail(&new_ccr->entry, &sctrl->ccr_list);
+
+out_unlock:
+	mutex_unlock(&sctrl->lock);
+	if (status == NVME_SC_SUCCESS)
+		nvmet_ctrl_fatal_error(ictrl);

nvmet_ctrl_fatal_error will log an error, is this desired?

Quoted 5 lines
drivers/nvme/target/core.c +5 -0
+struct nvmet_ctrl *nvmet_ctrl_find_get_ccr(struct nvmet_subsys *subsys,
+					   const char *hostnqn, u8 ciu,
+					   u16 cntlid, u64 cirn)
+{
+	struct nvmet_ctrl *ctrl, *ictrl = NULL;

drivers/nvme/target/core.c struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn, · line 1552

Quoted 10 lines
drivers/nvme/target/core.c +10 -0
+	bool found = false;
+
+	mutex_lock(&subsys->lock);
+	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+		if (ctrl->cntlid != cntlid)
+			continue;
+		if (strncmp(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE))
+			continue;
+
+		/* Avoid racing with a controller that is becoming ready */

I think this comment could a bit more eleborate, it took me a while to figure out the impact of it. What about something like:

/*
* Enabling the controller (nvmet_start_ctrl()) regenerates
* ciu and cirn under ctrl->lock. Hold the lock so that a
* controller becoming ready concurrently is matched against
* either the old or the new instance, never a mix of both. *
* CSTS.RDY is not checked on purpose: a disabled controller
* can still have commands in flight, so a matching instance
* must go through the CCR path rather than being reported as
* already reset.
*/

Signature · 1 line
Daniel Wagner <[email protected]>
Reviewed-by on v6 4/18
#15 lore raw

Re: [PATCH v6 04/18] nvmet: Implement CCR logpage

In reply to Mohamed Khalfella
Quoted 7 lines

Defined by TP8028 Rapid Path Failure Recovery, CCR (Cross-Controller Reset) log page contains an entry for each CCR request submitted to source controller. Implement CCR logpage for nvme linux target.

Signature · 1 line
Daniel Wagner <[email protected]>
Reviewed-by on v6 5/18
#17 lore raw

Re: [PATCH v6 05/18] nvmet: Send an AEN on CCR completion

In reply to Mohamed Khalfella
Quoted 11 lines

When an impacted controller that is the target of pending CCRs is torn down, send an AEN to each source controller that issued a CCR against it. The notification points to the CCR log page that the source controller can read to check which CCR operation completed.

Split nvmet_add_async_event() into a locked helper so the AEN can be queued while ctrl->lock is already held.

Signature · 1 line
Daniel Wagner <[email protected]>
#21 lore raw

Re: [PATCH v6 06/18] nvme: Rapid Path Failure Recovery read controller identify fields

In reply to Mohamed Khalfella
Quoted 5 lines

TP8028 Rapid Path Failure added new fields to controller identify response. Read CIU (Controller Instance Uniquifier), CIRN (Controller Instance Random Number), and CCRL (Cross-Controller Reset Limit) from controller identify response. Expose CIU and CIRN as sysfs attributes so the values can be used directly by user if needed.

What's the use case for it? This is an public/stable interface, it needs a justification and documentation

Quoted 2 lines

Also add the nvme_fence_timeout_ms() helper that derives a fencing timeout from the controller keep-alive timeout (kato).

There is no user in this patch, introduce it when you use it.

Signature · 1 line
Daniel Wagner <[email protected]>
#25 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Mohamed Khalfella

drivers/nvme/host/sysfs.c static ssize_t nvme_sysfs_show_state(struct device *dev, · line 549

Quoted 11 lines
drivers/nvme/host/sysfs.c +2 -0
@@ -542,6 +542,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
 		[NVME_CTRL_LIVE]	= "live",
+		[NVME_CTRL_FENCING]	= "fencing",
+		[NVME_CTRL_FENCED]	= "fenced",
 		[NVME_CTRL_RESETTING]	= "resetting",
 		[NVME_CTRL_CONNECTING]	= "connecting",
 		[NVME_CTRL_DELETING]	= "deleting",

The ABI documentation needs to be updated.

Signature · 1 line
Daniel Wagner <[email protected]>
#26 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

Quoted 5 lines · Daniel Wagner
Quoted 5 lines
drivers/nvme/host/sysfs.c +0 -0
@@ -542,6 +542,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
Quoted 8 lines

drivers/nvme/host/sysfs.c static ssize_t nvme_sysfs_show_state(struct device *dev, · line 549

Quoted 6 lines
drivers/nvme/host/sysfs.c +2 -0
 		[NVME_CTRL_LIVE]	= "live",
+		[NVME_CTRL_FENCING]	= "fencing",
+		[NVME_CTRL_FENCED]	= "fenced",
 		[NVME_CTRL_RESETTING]	= "resetting",
 		[NVME_CTRL_CONNECTING]	= "connecting",
 		[NVME_CTRL_DELETING]	= "deleting",

The ABI documentation needs to be updated.

And while you’re at it, what’s the impact on blktests? I can’t quite recall what you presented during ALPSS regarding blktests. Is this covered?

Keith, Christoph, and I are mostly concerned about this patch. Experience tells us that anyone touching the state machine will introduce regressions. Because of that, I’d really like to make sure the upstream testing side is covered. I know you’ve run tests in your lab, but we need test cases that everyone can execute in their own environments, and blktests is the right vehicle for that.

#3 lore raw

Re: [PATCH v6 01/18] nvmet: Rapid Path Failure Recovery set controller identify fields

In reply to Daniel Wagner
Quoted 4 lines · Daniel Wagner
Quoted 2 lines

maximum number of in-progress controller reset operations. CCRL is hardcoded to 4 as recommended by TP8028.

The spec says at least 4.

- CCRL (Cross-Controller Reset Limit): is an 8bit value that defines the maximum number of in-progress controller reset operations. CCRL is hardcoded to 4 because TP8028 recommended a value no less than 4.

Right, I updated the commit message to mention that recommendation.

Quoted 5 lines
Quoted 5 lines
drivers/nvme/target/core.c +0 -0
@@ -1695,6 +1699,11 @@ struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args)
 	}
 	ctrl->cntlid = ret;
 
Quoted 7 lines

drivers/nvme/target/core.c struct nvmet_ctrl *nvmet_alloc_ctrl(struct nvmet_alloc_ctrl_args *args) · line 1704

Quoted 3 lines
drivers/nvme/target/core.c +3 -0
+	if (!nvmet_is_disc_subsys(ctrl->subsys)) {
+		ctrl->ciu = get_random_u8() ? : 1;
+		ctrl->cirn = get_random_u64();

While get_random_u64 is very unlikely to return 0, it still possible, thus

   get_random_u64() ? : 1;

Good point. Updated.

Quoted 7 lines

would be the safe choice. Besides this nitpicking:

Signature · 1 line
Daniel Wagner <[email protected]>
#6 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Daniel Wagner
Quoted 5 lines · Daniel Wagner
Quoted 2 lines · Mohamed Khalfella

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory.

Could you add the 'why' part to the commit message?

target# grep . /sys/kernel/debug/nvmet/mysubsystem/ctrl1/{ciu,cirn} /sys/kernel/debug/nvmet/mysubsystem/ctrl1/ciu:57 /sys/kernel/debug/nvmet/mysubsystem/ctrl1/cirn:42541d24813bb536 target#

host# grep . /sys/class/nvme/nvme0/{cntlid,cirn,ciu} /sys/class/nvme/nvme0/cntlid:1 /sys/class/nvme/nvme0/cirn:42541d24813bb536 /sys/class/nvme/nvme0/ciu:57
host# nvme admin-passthru \
--opcode 0x38 \
--cdw10=$(( 0x1 | 0x57<<16 )) \
--cdw12=$(( 0x42541d24813bb536 & 0xffffffff )) \
--cdw13=$(( ( 0x42541d24813bb536 >> 32 ) & 0xffffffff )) \
/dev/nvme1
Admin Command Vendor Specific is Success and result: 0x00000000 host#

I found it useful to export these values while working on this change. For example the command above can be used to trigger CCR and observe the target behavior with the expectation that ctrl1 on the target will be failed.

Added a sentence to the commit message to mention that.

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory. The values are useful in matching host controller with the corresponding target controller.

Quoted 10 lines
Quoted 5 lines

Besides the missing 'why', it looks good.

#13 lore raw

Re: [PATCH v6 03/18] nvmet: Implement CCR nvme command

In reply to Daniel Wagner
Quoted 61 lines · Daniel Wagner
Quoted 61 lines
drivers/nvme/target/admin-cmd.c +61 -0
+void nvmet_execute_cross_ctrl_reset(struct nvmet_req *req)
+{
+	struct nvmet_ctrl *ictrl, *sctrl = req->sq->ctrl;
+	struct nvme_command *cmd = req->cmd;
+	struct nvmet_ccr *ccr, *new_ccr;
+	int ccr_active, ccr_total;
+	u16 cntlid, status = NVME_SC_SUCCESS;
+
+	if (!nvmet_check_transfer_len(req, 0))
+		return;
+
+	cntlid = le16_to_cpu(cmd->ccr.icid);
+	if (sctrl->cntlid == cntlid) {
+		req->error_loc =
+			offsetof(struct nvme_cross_ctrl_reset_cmd, icid);
+		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
+		goto out;
+	}
+
+	/* Find and get impacted controller */
+	ictrl = nvmet_ctrl_find_get_ccr(sctrl->subsys, sctrl->hostnqn,
+					cmd->ccr.ciu, cntlid,
+					le64_to_cpu(cmd->ccr.cirn));
+	if (!ictrl) {
+		/* Immediate Reset Successful */
+		nvmet_set_result(req, 1);
+		status = NVME_SC_SUCCESS;
+		goto out;
+	}
+
+	ccr_total = ccr_active = 0;
+	mutex_lock(&sctrl->lock);
+	list_for_each_entry(ccr, &sctrl->ccr_list, entry) {
+		if (ccr->ctrl == ictrl) {
+			status = NVME_SC_CCR_IN_PROGRESS | NVME_STATUS_DNR;
+			goto out_unlock;
+		}
+
+		ccr_total++;
+		if (ccr->ctrl)
+			ccr_active++;
+	}
+
+	if (ccr_active >= NVMF_CCR_LIMIT) {
+		status = NVME_SC_CCR_LIMIT_EXCEEDED;
+		goto out_unlock;
+	}
+	if (ccr_total >= NVMF_CCR_PER_PAGE) {
+		status = NVME_SC_CCR_LOGPAGE_FULL;
+		goto out_unlock;
+	}
+
+	new_ccr = kmalloc_obj(*new_ccr, GFP_KERNEL);
+	if (!new_ccr) {
+		status = NVME_SC_INTERNAL;
+		goto out_unlock;
+	}
+
+	new_ccr->ciu = cmd->ccr.ciu;
+	new_ccr->icid = cntlid;
+	new_ccr->ctrl = ictrl;
Quoted 8 lines

drivers/nvme/target/admin-cmd.c void nvmet_execute_keep_alive(struct nvmet_req *req) · line 1683

Quoted 6 lines
drivers/nvme/target/admin-cmd.c +6 -0
+	list_add_tail(&new_ccr->entry, &sctrl->ccr_list);
+
+out_unlock:
+	mutex_unlock(&sctrl->lock);
+	if (status == NVME_SC_SUCCESS)
+		nvmet_ctrl_fatal_error(ictrl);

nvmet_ctrl_fatal_error will log an error, is this desired?

I think it is good to have the error logged. The fatal error might not have been observed by the impacted controller we are failing yet, but it is still okay to consider it as fatal error IMO.

Quoted 20 lines
Quoted 6 lines
drivers/nvme/target/core.c +5 -0
+struct nvmet_ctrl *nvmet_ctrl_find_get_ccr(struct nvmet_subsys *subsys,
+					   const char *hostnqn, u8 ciu,
+					   u16 cntlid, u64 cirn)
+{
+	struct nvmet_ctrl *ctrl, *ictrl = NULL;

drivers/nvme/target/core.c struct nvmet_ctrl *nvmet_ctrl_find_get(const char *subsysnqn, · line 1552

Quoted 10 lines
drivers/nvme/target/core.c +10 -0
+	bool found = false;
+
+	mutex_lock(&subsys->lock);
+	list_for_each_entry(ctrl, &subsys->ctrls, subsys_entry) {
+		if (ctrl->cntlid != cntlid)
+			continue;
+		if (strncmp(ctrl->hostnqn, hostnqn, NVMF_NQN_SIZE))
+			continue;
+
+		/* Avoid racing with a controller that is becoming ready */

I think this comment could a bit more eleborate, it took me a while to figure out the impact of it. What about something like:

Quoted 11 lines

/*
* Enabling the controller (nvmet_start_ctrl()) regenerates
* ciu and cirn under ctrl->lock. Hold the lock so that a
* controller becoming ready concurrently is matched against
* either the old or the new instance, never a mix of both. *
* CSTS.RDY is not checked on purpose: a disabled controller
* can still have commands in flight, so a matching instance
* must go through the CCR path rather than being reported as
* already reset.
*/

Okay, I will take the first paragraph and remove the mention to nvmet_start_ctrl() because comments with function names do not usually age well. The second paragraph is not directly related to the lock IMO.

Quoted 3 lines
Signature · 2 lines
-- 
Daniel Wagner <[email protected]>
#22 lore raw

Re: [PATCH v6 06/18] nvme: Rapid Path Failure Recovery read controller identify fields

In reply to Daniel Wagner
Quoted 8 lines · Daniel Wagner
Quoted 5 lines

TP8028 Rapid Path Failure added new fields to controller identify response. Read CIU (Controller Instance Uniquifier), CIRN (Controller Instance Random Number), and CCRL (Cross-Controller Reset Limit) from controller identify response. Expose CIU and CIRN as sysfs attributes so the values can be used directly by user if needed.

What's the use case for it? This is an public/stable interface, it needs a justification and documentation

CIU and CIRN identify an instance of an NVMe controller. For a given CNTLID different values of CIU and CIRN identify different controller instances of the same controller. The justification is that the values allow the user to identify a specific controller instance.

host# grep . /sys/class/nvme/nvme0/{cntlid,cirn,ciu} /sys/class/nvme/nvme0/cntlid:1 /sys/class/nvme/nvme0/cirn:42541d24813bb536 /sys/class/nvme/nvme0/ciu:57
host# nvme admin-passthru \
--opcode 0x38 \
--cdw10=$(( 0x1 | 0x57<<16 )) \
--cdw12=$(( 0x42541d24813bb536 & 0xffffffff )) \
--cdw13=$(( ( 0x42541d24813bb536 >> 32 ) & 0xffffffff )) \
/dev/nvme1
Admin Command Vendor Specific is Success and result: 0x00000000 host#

One usecase is to use the values to issue CCR command manually. Not that I imagine one would want to do that, but if they want to do it they need this information. CIU is also present in CCR log page. Someone reading the log page might want to correlate a log page entry with a specific controller, so they need the information to be exported somehow.

Documentation/ABI/stable/sysfs-nvme +11 -0
index a2f5d0710db4..ebc3634c1a96 100644
@@ -9,6 +9,17 @@ Description:
 		of the NVMe controller, as reported in the Identify
 		Controller data structure.
 
+What:		/sys/class/nvme/nvmeX/cirn
+What:		/sys/class/nvme/nvmeX/ciu
+Date:		October 2026
+KernelVersion:	7.5
+Contact:	Mohamed Khalfella <[email protected]>
+Description:
+		Shows CIRN (Controller Instance Random Number) and CIU
+		(Controller Instance Uniquifier) of the current instance
+		of the NVMe controller as reported by Identify Controller
+		data structure.
+
 What:		/sys/class/nvme/nvmeX/cntlid
 Date:		February 2016
 KernelVersion:	4.6

How about a diff like the one above for a documentation? I can add something similar to the one above assuming the change will be merged in 7.5. Or I can add it later on? Maybe in an -rc after the change is merged?

What do you think?

Quoted 5 lines
Quoted 3 lines

Also add the nvme_fence_timeout_ms() helper that derives a fencing timeout from the controller keep-alive timeout (kato).

There is no user in this patch, introduce it when you use it.

Moved the hunk to patch "nvme: Implement cross-controller reset recovery" where the function is actually used.

Quoted 3 lines
Signature · 2 lines
-- 
Daniel Wagner <[email protected]>
#29 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Daniel Wagner
Quoted 5 lines · Daniel Wagner
Quoted 5 lines
drivers/nvme/host/sysfs.c +0 -0
@@ -542,6 +542,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
Quoted 8 lines

drivers/nvme/host/sysfs.c static ssize_t nvme_sysfs_show_state(struct device *dev, · line 549

Quoted 6 lines
drivers/nvme/host/sysfs.c +2 -0
 		[NVME_CTRL_LIVE]	= "live",
+		[NVME_CTRL_FENCING]	= "fencing",
+		[NVME_CTRL_FENCED]	= "fenced",
 		[NVME_CTRL_RESETTING]	= "resetting",
 		[NVME_CTRL_CONNECTING]	= "connecting",
 		[NVME_CTRL_DELETING]	= "deleting",

The ABI documentation needs to be updated.

Documentation/ABI/stable/sysfs-nvme +4 -4
index a2f5d0710db4..c47274fdd9a5 100644
@@ -140,13 +140,13 @@ Description:
 		PCI transport only.
 
 What:		/sys/class/nvme/nvmeX/state
-Date:		November 2016
-KernelVersion:	4.11
+Date:		October 2026
+KernelVersion:	7.5
 Contact:	Sagi Grimberg <[email protected]>
 Description:
 		Shows the current state of the controller. Possible values:
-		"new", "live", "resetting", "connecting", "deleting",
-		"deleting (no IO)", "dead".
+		"new", "live", "fencing", "fenced", "resetting", "connecting",
+		"deleting", "deleting (no IO)", "dead".
 
 What:		/sys/class/nvme/nvmeX/numa_node
 Date:		November 2018

How about a diff like the one above? Similar to CIRN and CIU, I am not sure what to put in "KernelVersion".

What do you think?

Quoted 3 lines
Signature · 2 lines
-- 
Daniel Wagner <[email protected]>
#27 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Daniel Wagner
Quoted 15 lines · Daniel Wagner
Quoted 5 lines · Daniel Wagner
Quoted 5 lines
drivers/nvme/host/sysfs.c +0 -0
@@ -542,6 +542,8 @@ static ssize_t nvme_sysfs_show_state(struct device *dev,
 	static const char *const state_name[] = {
 		[NVME_CTRL_NEW]		= "new",
Quoted 8 lines

drivers/nvme/host/sysfs.c static ssize_t nvme_sysfs_show_state(struct device *dev, · line 549

Quoted 6 lines
drivers/nvme/host/sysfs.c +2 -0
 		[NVME_CTRL_LIVE]	= "live",
+		[NVME_CTRL_FENCING]	= "fencing",
+		[NVME_CTRL_FENCED]	= "fenced",
 		[NVME_CTRL_RESETTING]	= "resetting",
 		[NVME_CTRL_CONNECTING]	= "connecting",
 		[NVME_CTRL_DELETING]	= "deleting",

The ABI documentation needs to be updated.

Quoted 3 lines

And while you’re at it, what’s the impact on blktests? I can’t quite recall what you presented during ALPSS regarding blktests. Is this covered?

Both [1] and [2] trigger "fencing" state. [1] focuses on not seeing corruption while [2] also inspects kernel logs to make sure that CCR and CQT are triggered when they should. There is no explict check of the controller state in these tests. We know that CCR/CQT will happen in "fencing" state though.

Quoted 8 lines

Keith, Christoph, and I are mostly concerned about this patch. Experience tells us that anyone touching the state machine will introduce regressions. Because of that, I’d really like to make sure the upstream testing side is covered. I know you’ve run tests in your lab, but we need test cases that everyone can execute in their own environments, and blktests is the right vehicle for that.

I documented part of the testing I did in lab in my ALPSS slides [3].

I addition to [1] and [2], what kind of blktest you would like to see? If you describe the blktest in detail I will implement it and put it for review.

1- https://lore.kernel.org/all/[email protected]/
2- https://lore.kernel.org/all/[email protected]/
3- https://drive.google.com/file/d/17lt_H3FxeoDSWD8q32diO7e03JYJaimn/view?usp=drive_link

#35 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Mohamed Khalfella
Quoted 45 lines
drivers/nvme/host/core.c +45 -0
+static int nvme_issue_wait_ccr(struct nvme_ctrl *sctrl, struct nvme_ctrl *ictrl,
+			       unsigned long deadline)
+{
+	struct nvme_ccr_entry ccr = { };
+	union nvme_result res = { 0 };
+	struct nvme_command c = { };
+	unsigned long flags, now, tmo = 0;
+	bool completed = false;
+	int ret = 0;
+	u32 result;
+
+	init_completion(&ccr.complete);
+	ccr.ictrl = ictrl;
+
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_add_tail(&ccr.list, &sctrl->ccr_list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+
+	c.ccr.opcode = nvme_admin_cross_ctrl_reset;
+	c.ccr.ciu = ictrl->ciu;
+	c.ccr.icid = cpu_to_le16(ictrl->cntlid);
+	c.ccr.cirn = cpu_to_le64(ictrl->cirn);
+	ret = __nvme_submit_sync_cmd(sctrl->admin_q, &c, &res,
+				     NULL, 0, NVME_QID_ANY, 0);
+	if (ret) {
+		ret = -EIO;
+		goto out;
+	}
+
+	result = le32_to_cpu(res.u32);
+	if (result & 0x01) /* Immediate Reset Successful */
+		goto out;
+
+	now = jiffies;
+	if (time_before(now, deadline))
+		tmo = min_t(unsigned long,
+			    secs_to_jiffies(ictrl->kato), deadline - now);
+
+	if (!wait_for_completion_timeout(&ccr.complete, tmo)) {
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+
+	completed = true;
+

drivers/nvme/host/core.c void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl) · line 682

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+out:
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_del(&ccr.list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+	if (completed) {
+		if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
+			return 0;
+		return -EREMOTEIO;
+	}
+	return ret;

This part could be made a bit simpler and there is no need for the local completed variable:

[...]
if (!wait_for_completion_timeout(&ccr.complete, tmo))
ret = -ETIMEDOUT;

out:

        spin_lock_irqsave(&sctrl->lock, flags);
        list_del(&ccr.list);
        spin_unlock_irqrestore(&sctrl->lock, flags);

        if (ret || !completion_done(&ccr.complete))
                return ret;
        if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
                return 0;
        return -EREMOTEIO;
}
#42 lore raw

Re: [PATCH v6 09/18] nvme: Implement cross-controller reset completion

In reply to Mohamed Khalfella

drivers/nvme/host/core.c static bool nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result) · line 5146

Quoted 9 lines · Mohamed Khalfella

static void nvme_fw_act_work(struct work_struct *work) {

drivers/nvme/host/core.c +2 -0
 	struct nvme_ctrl *ctrl = container_of(work,
@@ -5099,6 +5142,9 @@ static bool nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result)
 	case NVME_AER_NOTICE_DISC_CHANGED:
 		ctrl->aen_result = result;
 		break;
+	case NVME_AER_NOTICE_CCR_COMPLETED:
+		queue_work(nvme_wq, &ctrl->ccr_work);

What happens to the waiters on sctrl->ccr_list when the source controller enters error recovery while a CCR is pending?

As far as I can tell, ccr_work itself is fine. The reset path cancels it via nvme_stop_ctrl(), and in the error recovery path the Get Log Page just fails once the admin queue is torn down. But the target drops all CCRs issued by the old association when it frees the controller, so the CCR completed AEN for them will never arrive. Nothing on the host side walks ccr_list when the controller leaves LIVE, so nvme_issue_wait_ccr() sleeps until min(kato, deadline - now) runs out before nvme_fence_ctrl() tries the next source controller. In the worst case that eats the whole fencing window.

Would it make sense to fail all pending entries (set ccrs to a failure status and complete them under ctrl->lock) when the source controller goes into error recovery, so the fencing code can move on to another path right away?

#47 lore raw

Re: [PATCH v6 10/18] nvme-tcp: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella
Quoted 7 lines · Mohamed Khalfella
drivers/nvme/host/tcp.c +1 -0
@@ -2731,13 +2755,15 @@ static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
 	struct nvme_command *cmd = &pdu->cmd;
 	int qid = nvme_tcp_queue_id(req->queue);
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->device,

drivers/nvme/host/tcp.c static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq) · line 2769

Quoted 10 lines
drivers/nvme/host/tcp.c +2 -1
 		 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
-	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
 		/*
 		 * If we are resetting, connecting or deleting we should
 		 * complete immediately because we may block controller

The documentation should be updated, maybe something like:

state = nvme_ctrl_state(ctrl);
if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
/*
* If fencing has finished or we are resetting, connecting or
* deleting we should complete immediately because we may
* block controller teardown or setup sequence
* - ctrl disable/shutdown fabrics requests
* - connect requests
* - initialization admin requests
* - I/O requests that entered after unquiescing and
* the controller stopped responding
*
* All other requests should be cancelled by the error
* recovery work, so it's fine that we fail it here. */
nvme_tcp_complete_timed_out(rq);
return BLK_EH_DONE;
}

/*
* LIVE state should trigger the normal error recovery which will
* handle completing this request. In FENCING state the request
* must not be completed before fencing has finished, so just
* rearm the timer and let the error recovery pick it up. */
nvme_tcp_error_recovery(ctrl);
return BLK_EH_RESET_TIMER;

#50 lore raw

Re: [PATCH v6 11/18] nvme-rdma: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella
Quoted 4 lines · Mohamed Khalfella
drivers/nvme/host/rdma.c +0 -0
@@ -1990,13 +2014,15 @@ static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
 	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
 	struct nvme_command *cmd = req->req.cmd;
 	int qid = nvme_rdma_queue_idx(queue);

drivers/nvme/host/rdma.c static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq) · line 2025

Quoted 10 lines
drivers/nvme/host/rdma.c +3 -1
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->ctrl.device,
 		 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
-	if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(&ctrl->ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {

documentation needs to be udpated like in the tcp patch.

Reviewed-by on v6 12/18
#54 lore raw

Re: [PATCH v6 12/18] nvme-fc: start error recovery instead of aborting timed out IOs

In reply to Mohamed Khalfella

drivers/nvme/host/fc.c static enum blk_eh_timer_return nvme_fc_timeout(struct request *rq) · line 2561

Quoted 11 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +0 -5
-	/*
-	 * Attempt to abort the offending command. Command completion
-	 * will detect the aborted io and will fail the connection.
-	 */
 	dev_info(ctrl->ctrl.device,
 		"NVME-FC{%d.%d}: io timeout: opcode %d fctype %d (%s) w10/11: "
 		"x%08x/x%08x\n",
 		ctrl->cnum, qnum, sqe->common.opcode, sqe->fabrics.fctype,
 		nvme_fabrics_opcode_str(qnum, sqe),
 		sqe->common.cdw10, sqe->common.cdw11);
-	if (__nvme_fc_abort_op(ctrl, op))

Yes, this makes sense to remove it from this path and do the abort in one place only.

Reviewed-by on v6 13/18
#56 lore raw

Re: [PATCH v6 13/18] nvme-fc: perform error recovery directly from ioerr_work

In reply to Mohamed Khalfella

drivers/nvme/host/fc.c nvme_fc_reset_ctrl_work(struct work_struct *work) · line 3405

Quoted 9 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +9 -0
+static void
+nvme_fc_error_recovery(struct nvme_fc_ctrl *ctrl)
+{
+	nvme_stop_keep_alive(&ctrl->ctrl);
+	flush_work(&ctrl->ctrl.async_event_work);
+
+	/* will block while waiting for io to terminate */
+	nvme_fc_delete_association(ctrl);
+	nvme_stop_ctrl(&ctrl->ctrl);

Not happy that we have duplicated this part, but after reading on tcp/rdma it's the 'common' pattern. I think it make sense to move the fc towards how tcp/rdma is doing it so that we can make common code out of it eventually.

So from this, looks good.

#58 lore raw

Re: [PATCH v6 14/18] nvme-fc: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella

drivers/nvme/host/fc.c __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, · line 1885

Quoted 11 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +11 -0
+static void nvme_fc_fencing_work(struct work_struct *work)
+{
+	struct nvme_fc_ctrl *fc_ctrl =
+			container_of(work, struct nvme_fc_ctrl, fencing_work);
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+	unsigned long rem;
+
+	/*
+	 * Tear the association down now so the LLDD can finish unregistering
+	 * the remoteport/localport while the fencing this controller.
+	 */

Maybe:

/*
* Tear down the association before fencing starts. Fencing can take
* a long time and the LLDD can't finish unregistering the
* remoteport/localport until all associations on it are gone.
* Inflight requests are held and only completed once fencing has
* finished.
*/
?

#61 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Mohamed Khalfella
Quoted 24 lines · Mohamed Khalfella

static void

drivers/nvme/host/fc.c +3 -6
-nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
+__nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl)
 {
 	struct nvmefc_ls_rcv_op *disls = NULL;
 	unsigned long flags;
 
-	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
-		return;
-
 	spin_lock_irqsave(&ctrl->lock, flags);
 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
-	ctrl->iocnt = 0;
+	atomic_set(&ctrl->iocnt, 0);
 	spin_unlock_irqrestore(&ctrl->lock, flags);
 
 	__nvme_fc_abort_outstanding_ios(ctrl, false);
@@ -3287,11 +3328,18 @@ nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
 	nvme_fc_abort_aen_ops(ctrl);
 
 	/* wait for all io that had to be aborted */
+	wait_event(ctrl->ioabort_wait, atomic_read(&ctrl->iocnt) == 0);
 	spin_lock_irq(&ctrl->lock);
-	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);

drivers/nvme/host/fc.c nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) · line 3342

Quoted 10 lines
drivers/nvme/host/fc.c +7 -0
 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
 	spin_unlock_irq(&ctrl->lock);
 
+	/*
+	 * Wait for a started nvme_fc_fcpio_done() to complete before we
+	 * proceed with cancelling inflight requests. This guarantees a
+	 * request is completed by one of the two codepaths, not both.
+	 */
+	synchronize_rcu();
+

The synchronize_rcu() protects against nvme_cancel_tagset() racing with an fcpio_done() for an op that completed normally and therefore wasn't counted in iocnt. Wouldn't it be clearer to make the hold explicit in op->state (e.g. FCPOP_STATE_HELD) and only complete held ops in nvme_fc_flush_held_requests(), instead of using RCU as a barrier for running callbacks?

Reviewed-by on v6 16/18
#69 lore raw

Re: [PATCH v6 16/18] nvmet: Add support for CQT to nvme target

In reply to Mohamed Khalfella
Quoted 8 lines · Mohamed Khalfella

TP4129 KATO Corrections and Clarifications defined CQT (Command Quiesce Time) which is used along with KATO (Keep Alive Timeout) to set an upper time limit for attempting Cross-Controller Recovery. CQT is added as a subsystem attribute that defaults to 0 to maintain the current behavior.

Reviewed-by on v6 17/18
#71 lore raw

Re: [PATCH v6 17/18] nvme: Add support for CQT to nvme host

In reply to Mohamed Khalfella
Quoted 12 lines · Mohamed Khalfella

TP4129 KATO Corrections and Clarifications defined CQT (Command Quiesce Time) which is used along with KATO (Keep Alive Timeout) to set an upper limit for attempting Cross-Controller Recovery. Add ctrl->cqt, read its value from controller identify response, expose it via sysfs, and have nvme_fence_timeout_ms() account for it.

Use CQT to drive time-based recovery in fc, rdma, and tcp transports. Add a fenced_work delayed work to each transport controller. If CCR fails, fencing_work switches to error recovery immediately if CQT is not supported. Otherwise it schedules fenced_work after the remaining fence timeout, which also switches to error recovery when scheduled.

Quoted 3 lines

the sysfs entry needs to be documented, with this

#7 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Mohamed Khalfella
Quoted 12 lines · Mohamed Khalfella
Quoted 5 lines · Daniel Wagner
Quoted 2 lines · Mohamed Khalfella

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory.

Could you add the 'why' part to the commit message?

target# grep . /sys/kernel/debug/nvmet/mysubsystem/ctrl1/{ciu,cirn} /sys/kernel/debug/nvmet/mysubsystem/ctrl1/ciu:57 /sys/kernel/debug/nvmet/mysubsystem/ctrl1/cirn:42541d24813bb536 target#

Quoted 12 lines

host# grep . /sys/class/nvme/nvme0/{cntlid,cirn,ciu} /sys/class/nvme/nvme0/cntlid:1 /sys/class/nvme/nvme0/cirn:42541d24813bb536 /sys/class/nvme/nvme0/ciu:57
host# nvme admin-passthru \
--opcode 0x38 \
--cdw10=$(( 0x1 | 0x57<<16 )) \
--cdw12=$(( 0x42541d24813bb536 & 0xffffffff )) \
--cdw13=$(( ( 0x42541d24813bb536 >> 32 ) & 0xffffffff )) \
/dev/nvme1
Admin Command Vendor Specific is Success and result: 0x00000000 host#

In the meantime, nvme-cli learned to dump those values as well. I'm not sure if it's worth adding these just for this use case.

#30 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Mohamed Khalfella
Quoted 11 lines · Mohamed Khalfella

Description:

Documentation/ABI/stable/sysfs-nvme +2 -2
 		Shows the current state of the controller. Possible values:
-		"new", "live", "resetting", "connecting", "deleting",
-		"deleting (no IO)", "dead".
+		"new", "live", "fencing", "fenced", "resetting", "connecting",
+		"deleting", "deleting (no IO)", "dead".
 
 What:		/sys/class/nvme/nvmeX/numa_node
 Date:		November 2018
Quoted 2 lines

How about a diff like the one above? Similar to CIRN and CIU, I am not sure what to put in "KernelVersion".

I think the KernelVersion is when it first appeared. I would leave it as it is and instead extend the description

Shows the current state of the controller. Possible values:
"new", "live", "resetting", "connecting", "deleting", "deleting (no IO)", "dead".
v7.5 introduced additionally:
"fencing", "fenced"

Obviously, this is not idea. If someone starts to complain we might have to filter out these two new states.

#28 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Mohamed Khalfella
Quoted 5 lines · Mohamed Khalfella

I documented part of the testing I did in lab in my ALPSS slides [3].

I addition to [1] and [2], what kind of blktest you would like to see? If you describe the blktest in detail I will implement it and put it for review.

Sorry for being a bit confusing. I was trying to figure out if there are dedicated state machine tests, e.g trying to force the state machine into impossible states etc, but I suppose a simple state machine transition test is not really meaningful.

#36 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Daniel Wagner
Quoted 65 lines · Daniel Wagner
Quoted 45 lines
drivers/nvme/host/core.c +45 -0
+static int nvme_issue_wait_ccr(struct nvme_ctrl *sctrl, struct nvme_ctrl *ictrl,
+			       unsigned long deadline)
+{
+	struct nvme_ccr_entry ccr = { };
+	union nvme_result res = { 0 };
+	struct nvme_command c = { };
+	unsigned long flags, now, tmo = 0;
+	bool completed = false;
+	int ret = 0;
+	u32 result;
+
+	init_completion(&ccr.complete);
+	ccr.ictrl = ictrl;
+
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_add_tail(&ccr.list, &sctrl->ccr_list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+
+	c.ccr.opcode = nvme_admin_cross_ctrl_reset;
+	c.ccr.ciu = ictrl->ciu;
+	c.ccr.icid = cpu_to_le16(ictrl->cntlid);
+	c.ccr.cirn = cpu_to_le64(ictrl->cirn);
+	ret = __nvme_submit_sync_cmd(sctrl->admin_q, &c, &res,
+				     NULL, 0, NVME_QID_ANY, 0);
+	if (ret) {
+		ret = -EIO;
+		goto out;
+	}
+
+	result = le32_to_cpu(res.u32);
+	if (result & 0x01) /* Immediate Reset Successful */
+		goto out;
+
+	now = jiffies;
+	if (time_before(now, deadline))
+		tmo = min_t(unsigned long,
+			    secs_to_jiffies(ictrl->kato), deadline - now);
+
+	if (!wait_for_completion_timeout(&ccr.complete, tmo)) {
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+
+	completed = true;
+

drivers/nvme/host/core.c void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl) · line 682

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+out:
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_del(&ccr.list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+	if (completed) {
+		if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
+			return 0;
+		return -EREMOTEIO;
+	}
+	return ret;

This part could be made a bit simpler and there is no need for the local completed variable:

[...]
if (!wait_for_completion_timeout(&ccr.complete, tmo))
ret = -ETIMEDOUT;

out:

Quoted 10 lines
        spin_lock_irqsave(&sctrl->lock, flags);
        list_del(&ccr.list);
        spin_unlock_irqrestore(&sctrl->lock, flags);

        if (ret || !completion_done(&ccr.complete))
                return ret;
        if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
                return 0;
        return -EREMOTEIO;
}

You are right, this is deleting "completed" local variable at the expense of checking the ccr.complete again. I find the current code easier to read and reason about. I prefer to keep it as it is.

#43 lore raw

Re: [PATCH v6 09/18] nvme: Implement cross-controller reset completion

In reply to Daniel Wagner
Quoted 13 lines · Daniel Wagner

drivers/nvme/host/core.c static bool nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result) · line 5146

Quoted 9 lines · Mohamed Khalfella

static void nvme_fw_act_work(struct work_struct *work) {

drivers/nvme/host/core.c +2 -0
 	struct nvme_ctrl *ctrl = container_of(work,
@@ -5099,6 +5142,9 @@ static bool nvme_handle_aen_notice(struct nvme_ctrl *ctrl, u32 result)
 	case NVME_AER_NOTICE_DISC_CHANGED:
 		ctrl->aen_result = result;
 		break;
+	case NVME_AER_NOTICE_CCR_COMPLETED:
+		queue_work(nvme_wq, &ctrl->ccr_work);

What happens to the waiters on sctrl->ccr_list when the source controller enters error recovery while a CCR is pending?

You already answered it below :)

Quoted 10 lines

As far as I can tell, ccr_work itself is fine. The reset path cancels it via nvme_stop_ctrl(), and in the error recovery path the Get Log Page just fails once the admin queue is torn down. But the target drops all CCRs issued by the old association when it frees the controller, so the CCR completed AEN for them will never arrive. Nothing on the host side walks ccr_list when the controller leaves LIVE, so nvme_issue_wait_ccr() sleeps until min(kato, deadline - now) runs out before nvme_fence_ctrl() tries the next source controller. In the worst case that eats the whole fencing window.

If the remaining of the fencing window (deadline - now) is less than kato then I am fine with this scenario wasting this time anyway. If we hit the deadline then we consider fencing done anyway. If it consumes kato from the deadline then we retry another path.

Quoted 5 lines

Would it make sense to fail all pending entries (set ccrs to a failure status and complete them under ctrl->lock) when the source controller goes into error recovery, so the fencing code can move on to another path right away?

Failing pending CCR entries that we know we are not going to complete will help in this case. The question is where to do that. A LIVE controller might hit an error and transition to FENCING, or manually reset and move to RESETTING, or deleted and go to DELETING. Do we need to consider all these places to fail the pending CCRs?

You might have also noticed that nvme_issue_wait_ccr() calling __nvme_submit_sync_cmd() might consume the entire fencing window if the CCR command gets held because the source controller hits an error. This is another area be optimized. I am inclined to leave the code as it is today. What is being described here is an optimization that can come later on.

#48 lore raw

Re: [PATCH v6 10/18] nvme-tcp: Use CCR to recover controller that hits an error

In reply to Daniel Wagner
Quoted 40 lines · Daniel Wagner
Quoted 7 lines · Mohamed Khalfella
drivers/nvme/host/tcp.c +1 -0
@@ -2731,13 +2755,15 @@ static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
 	struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
 	struct nvme_command *cmd = &pdu->cmd;
 	int qid = nvme_tcp_queue_id(req->queue);
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->device,

drivers/nvme/host/tcp.c static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq) · line 2769

Quoted 10 lines
drivers/nvme/host/tcp.c +2 -1
 		 "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
-	if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
 		/*
 		 * If we are resetting, connecting or deleting we should
 		 * complete immediately because we may block controller

The documentation should be updated, maybe something like:

state = nvme_ctrl_state(ctrl);
if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {
/*
* If fencing has finished or we are resetting, connecting or
* deleting we should complete immediately because we may
* block controller teardown or setup sequence
* - ctrl disable/shutdown fabrics requests
* - connect requests
* - initialization admin requests
* - I/O requests that entered after unquiescing and
* the controller stopped responding
*
* All other requests should be cancelled by the error
* recovery work, so it's fine that we fail it here. */
nvme_tcp_complete_timed_out(rq);
return BLK_EH_DONE;
}

Quoted 8 lines

/*
* LIVE state should trigger the normal error recovery which will
* handle completing this request. In FENCING state the request
* must not be completed before fencing has finished, so just
* rearm the timer and let the error recovery pick it up. */
nvme_tcp_error_recovery(ctrl);
return BLK_EH_RESET_TIMER;

Good point. I updated the comments.

#51 lore raw

Re: [PATCH v6 11/18] nvme-rdma: Use CCR to recover controller that hits an error

In reply to Daniel Wagner
Quoted 12 lines · Daniel Wagner
Quoted 11 lines · Mohamed Khalfella
drivers/nvme/host/rdma.c +1 -0
@@ -1990,13 +2014,15 @@ static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq)
 	struct nvme_rdma_ctrl *ctrl = queue->ctrl;
 	struct nvme_command *cmd = req->req.cmd;
 	int qid = nvme_rdma_queue_idx(queue);
+	enum nvme_ctrl_state state;
 
 	dev_warn(ctrl->ctrl.device,
 		 "I/O tag %d (%04x) opcode %#x (%s) QID %d timeout\n",
 		 rq->tag, nvme_cid(rq), cmd->common.opcode,
 		 nvme_fabrics_opcode_str(qid, cmd), qid);
 
Quoted 5 lines

drivers/nvme/host/rdma.c static enum blk_eh_timer_return nvme_rdma_timeout(struct request *rq) · line 2025

Quoted 3 lines
drivers/nvme/host/rdma.c +2 -1
-	if (nvme_ctrl_state(&ctrl->ctrl) != NVME_CTRL_LIVE) {
+	state = nvme_ctrl_state(&ctrl->ctrl);
+	if (state != NVME_CTRL_LIVE && state != NVME_CTRL_FENCING) {

documentation needs to be udpated like in the tcp patch.

Done

#59 lore raw

Re: [PATCH v6 14/18] nvme-fc: Use CCR to recover controller that hits an error

In reply to Daniel Wagner
Quoted 16 lines · Daniel Wagner

drivers/nvme/host/fc.c __nvme_fc_fcpop_chk_teardowns(struct nvme_fc_ctrl *ctrl, · line 1885

Quoted 11 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +11 -0
+static void nvme_fc_fencing_work(struct work_struct *work)
+{
+	struct nvme_fc_ctrl *fc_ctrl =
+			container_of(work, struct nvme_fc_ctrl, fencing_work);
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+	unsigned long rem;
+
+	/*
+	 * Tear the association down now so the LLDD can finish unregistering
+	 * the remoteport/localport while the fencing this controller.
+	 */

Maybe:

Quoted 8 lines

/*
* Tear down the association before fencing starts. Fencing can take
* a long time and the LLDD can't finish unregistering the
* remoteport/localport until all associations on it are gone.
* Inflight requests are held and only completed once fencing has
* finished.
*/
?

Yes, this is better. I updated the comment just like suggested above.

#37 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Mohamed Khalfella
Quoted 19 lines · Mohamed Khalfella
Quoted 8 lines

This part could be made a bit simpler and there is no need for the local completed variable:

[...]
if (!wait_for_completion_timeout(&ccr.complete, tmo))
ret = -ETIMEDOUT;

out:

Quoted 10 lines
        spin_lock_irqsave(&sctrl->lock, flags);
        list_del(&ccr.list);
        spin_unlock_irqrestore(&sctrl->lock, flags);

        if (ret || !completion_done(&ccr.complete))
                return ret;
        if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
                return 0;
        return -EREMOTEIO;
}
Quoted 3 lines

You are right, this is deleting "completed" local variable at the expense of checking the ccr.complete again. I find the current code easier to read and reason about. I prefer to keep it as it is.

The point is that with the local variable, the reader has to track back and work out where it is set. I had to do exactly that while reviewing. Putting !completion_done() in the condition makes it self-documenting and ties it directly to the wait_for_completion_timeout() above.

#44 lore raw

Re: [PATCH v6 09/18] nvme: Implement cross-controller reset completion

In reply to Mohamed Khalfella
Quoted 4 lines · Mohamed Khalfella
Quoted 2 lines

What happens to the waiters on sctrl->ccr_list when the source controller enters error recovery while a CCR is pending?

You already answered it below :)

I tried to figure it out, but I'm still not sure if I really understand all the conditions. So bear with me.

Quoted 10 lines
Quoted 9 lines

As far as I can tell, ccr_work itself is fine. The reset path cancels it via nvme_stop_ctrl(), and in the error recovery path the Get Log Page just fails once the admin queue is torn down. But the target drops all CCRs issued by the old association when it frees the controller, so the CCR completed AEN for them will never arrive. Nothing on the host side walks ccr_list when the controller leaves LIVE, so nvme_issue_wait_ccr() sleeps until min(kato, deadline - now) runs out before nvme_fence_ctrl() tries the next source controller. In the worst case that eats the whole fencing window.

Quoted 4 lines

If the remaining of the fencing window (deadline - now) is less than kato then I am fine with this scenario wasting this time anyway. If we hit the deadline then we consider fencing done anyway. If it consumes kato from the deadline then we retry another path.

Okay, this might be something worth documenting? It seems like a case we will get bug reports in the future.

Quoted 10 lines
Quoted 4 lines

Would it make sense to fail all pending entries (set ccrs to a failure status and complete them under ctrl->lock) when the source controller goes into error recovery, so the fencing code can move on to another path right away?

Failing pending CCR entries that we know we are not going to complete will help in this case. The question is where to do that. A LIVE controller might hit an error and transition to FENCING, or manually reset and move to RESETTING, or deleted and go to DELETING. Do we need to consider all these places to fail the pending CCRs?

I'd say yes. Anyway, I don't think this needs to be addressed right away if the current code is functional correct and we are just not as fast as possible.

Quoted 6 lines

You might have also noticed that nvme_issue_wait_ccr() calling __nvme_submit_sync_cmd() might consume the entire fencing window if the CCR command gets held because the source controller hits an error. This is another area be optimized. I am inclined to leave the code as it is today. What is being described here is an optimization that can come later on.

Yes, I was actually about to ask about __nvme_submit_sync_cmd in this path next. Since my questions are about optimization, I don't think we should make the current version any more complex than it already is. We can handle optimization later, but please document those cases in the meantime.

#45 lore raw

Re: [PATCH v6 09/18] nvme: Implement cross-controller reset completion

Quoted 8 lines · Daniel Wagner
Quoted 4 lines · Mohamed Khalfella
Quoted 2 lines

What happens to the waiters on sctrl->ccr_list when the source controller enters error recovery while a CCR is pending?

You already answered it below :)

I tried to figure it out, but I'm still not sure if I really understand all the conditions. So bear with me.

No problem. Take your time.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

As far as I can tell, ccr_work itself is fine. The reset path cancels it via nvme_stop_ctrl(), and in the error recovery path the Get Log Page just fails once the admin queue is torn down. But the target drops all CCRs issued by the old association when it frees the controller, so the CCR completed AEN for them will never arrive. Nothing on the host side walks ccr_list when the controller leaves LIVE, so nvme_issue_wait_ccr() sleeps until min(kato, deadline - now) runs out before nvme_fence_ctrl() tries the next source controller. In the worst case that eats the whole fencing window.

Quoted 7 lines
Quoted 4 lines

If the remaining of the fencing window (deadline - now) is less than kato then I am fine with this scenario wasting this time anyway. If we hit the deadline then we consider fencing done anyway. If it consumes kato from the deadline then we retry another path.

Okay, this might be something worth documenting? It seems like a case we will get bug reports in the future.

I do not think this behavior worth documenting as stable behavior. We should be able to refactor/change/optimize this code in the future without worrying about breaking a documented behavior.

The main point of CCR is to speed up controller fencing in order to avoid CQT delay. I think of it as a best-effort process attempted in a bounded amount of time. If it succeeds we skip the CQT delay, otherwise we spend the time we will have spent in CQT delay anyway.

Can we do better within this bounded amount of time? Sure, this is what is being suggested here. However, I prefer not to do that part of this patchset. Also, do not like to clutter the code with comments about potential changes.

Quoted 12 lines
Quoted 11 lines
Quoted 5 lines

Would it make sense to fail all pending entries (set ccrs to a failure status and complete them under ctrl->lock) when the source controller goes into error recovery, so the fencing code can move on to another path right away?

Failing pending CCR entries that we know we are not going to complete will help in this case. The question is where to do that. A LIVE controller might hit an error and transition to FENCING, or manually reset and move to RESETTING, or deleted and go to DELETING. Do we need to consider all these places to fail the pending CCRs?

Quoted 3 lines

I'd say yes. Anyway, I don't think this needs to be addressed right away if the current code is functional correct and we are just not as fast as possible.

Agree, the goal of the patchset is to add the initial support for CCR/CQT. If there are functional issues they should be addressed now. Known gaps and optimizations can come in later patchsets.

drivers/nvme/host/core.c +3 -0
index ca7f083702f5..6c0703fba8ff 100644
@@ -863,6 +863,9 @@ bool nvme_change_ctrl_state(struct nvme_ctrl *ctrl,
                wake_up_all(&ctrl->state_wq);
        }
 
+       if (changed && old_state == NVME_CTRL_LIVE)
+               nvme_fail_pending_ccrs(ctrl); /* TBD */
+
        spin_unlock_irqrestore(&ctrl->lock, flags);
        if (!changed)
                return false;

One way to fail pending CCRs is to add it to controller state machine. Something like the diff above.

Quoted 8 lines
Quoted 7 lines

You might have also noticed that nvme_issue_wait_ccr() calling __nvme_submit_sync_cmd() might consume the entire fencing window if the CCR command gets held because the source controller hits an error. This is another area be optimized. I am inclined to leave the code as it is today. What is being described here is an optimization that can come later on.

Quoted 5 lines

Yes, I was actually about to ask about __nvme_submit_sync_cmd in this path next. Since my questions are about optimization, I don't think we should make the current version any more complex than it already is. We can handle optimization later, but please document those cases in the meantime.

Hannes also raised this point. It is on my todo list to add timeout parameter to __nvme_submit_sync_cmd() and use that here.

#38 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Daniel Wagner
Quoted 20 lines · Daniel Wagner
Quoted 19 lines · Mohamed Khalfella
Quoted 18 lines

This part could be made a bit simpler and there is no need for the local completed variable:

[...]
if (!wait_for_completion_timeout(&ccr.complete, tmo))
ret = -ETIMEDOUT;

out:

        spin_lock_irqsave(&sctrl->lock, flags);
        list_del(&ccr.list);
        spin_unlock_irqrestore(&sctrl->lock, flags);

        if (ret || !completion_done(&ccr.complete))
                return ret;
        if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
                return 0;
        return -EREMOTEIO;
}
Quoted 8 lines
Quoted 3 lines

You are right, this is deleting "completed" local variable at the expense of checking the ccr.complete again. I find the current code easier to read and reason about. I prefer to keep it as it is.

The point is that with the local variable, the reader has to track back and work out where it is set. I had to do exactly that while reviewing. Putting !completion_done() in the condition makes it self-documenting and ties it directly to the wait_for_completion_timeout() above.

I think this is a subjective point. For me seeing completion_done() after wait_for_completion_timeout() raises the question "Do we not already know if CCR completed or not? Why are we checking again?"

#31 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Daniel Wagner
Quoted 24 lines · Daniel Wagner
Quoted 11 lines · Mohamed Khalfella

Description:

Documentation/ABI/stable/sysfs-nvme +2 -2
 		Shows the current state of the controller. Possible values:
-		"new", "live", "resetting", "connecting", "deleting",
-		"deleting (no IO)", "dead".
+		"new", "live", "fencing", "fenced", "resetting", "connecting",
+		"deleting", "deleting (no IO)", "dead".
 
 What:		/sys/class/nvme/nvmeX/numa_node
 Date:		November 2018
Quoted 2 lines

How about a diff like the one above? Similar to CIRN and CIU, I am not sure what to put in "KernelVersion".

I think the KernelVersion is when it first appeared. I would leave it as it is and instead extend the description

Shows the current state of the controller. Possible values:

(quoted diff) +0 -0
 		"new", "live", "resetting", "connecting", "deleting",
 		"deleting (no IO)", "dead".
        v7.5 introduced additionally:
        "fencing", "fenced"
Quoted 2 lines

Obviously, this is not idea. If someone starts to complain we might have to filter out these two new states.

Is Documentation/ABI/stable/sysfs-nvme a living history of sysfs-nvme documentation or a documentation for the specific kernel release?

#8 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Daniel Wagner
Quoted 26 lines · Daniel Wagner
Quoted 12 lines · Mohamed Khalfella
Quoted 5 lines · Daniel Wagner
Quoted 2 lines · Mohamed Khalfella

Export ctrl->ciu and ctrl->cirn as debugfs files under controller debugfs directory.

Could you add the 'why' part to the commit message?

target# grep . /sys/kernel/debug/nvmet/mysubsystem/ctrl1/{ciu,cirn} /sys/kernel/debug/nvmet/mysubsystem/ctrl1/ciu:57 /sys/kernel/debug/nvmet/mysubsystem/ctrl1/cirn:42541d24813bb536 target#

Quoted 12 lines

host# grep . /sys/class/nvme/nvme0/{cntlid,cirn,ciu} /sys/class/nvme/nvme0/cntlid:1 /sys/class/nvme/nvme0/cirn:42541d24813bb536 /sys/class/nvme/nvme0/ciu:57
host# nvme admin-passthru \
--opcode 0x38 \
--cdw10=$(( 0x1 | 0x57<<16 )) \
--cdw12=$(( 0x42541d24813bb536 & 0xffffffff )) \
--cdw13=$(( ( 0x42541d24813bb536 >> 32 ) & 0xffffffff )) \
/dev/nvme1
Admin Command Vendor Specific is Success and result: 0x00000000 host#

Quoted 4 lines

In the meantime, nvme-cli learned to dump those values as well. I'm not sure if it's worth adding these just for this use case.

Good to know that nvme-cli now parses these fields. That is said, the sysfs and debugfs attributes are useful to have. Say the controller is stuck in a state that it can not process an identify command.

#62 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Daniel Wagner
Quoted 28 lines · Daniel Wagner
Quoted 27 lines · Mohamed Khalfella

static void

drivers/nvme/host/fc.c +3 -6
-nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
+__nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl)
 {
 	struct nvmefc_ls_rcv_op *disls = NULL;
 	unsigned long flags;
 
-	if (!test_and_clear_bit(ASSOC_ACTIVE, &ctrl->flags))
-		return;
-
 	spin_lock_irqsave(&ctrl->lock, flags);
 	set_bit(FCCTRL_TERMIO, &ctrl->flags);
-	ctrl->iocnt = 0;
+	atomic_set(&ctrl->iocnt, 0);
 	spin_unlock_irqrestore(&ctrl->lock, flags);
 
 	__nvme_fc_abort_outstanding_ios(ctrl, false);
@@ -3287,11 +3328,18 @@ nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl)
 	nvme_fc_abort_aen_ops(ctrl);
 
 	/* wait for all io that had to be aborted */
+	wait_event(ctrl->ioabort_wait, atomic_read(&ctrl->iocnt) == 0);
 	spin_lock_irq(&ctrl->lock);
-	wait_event_lock_irq(ctrl->ioabort_wait, ctrl->iocnt == 0, ctrl->lock);
 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
 	spin_unlock_irq(&ctrl->lock);
 
Quoted 14 lines

drivers/nvme/host/fc.c nvme_fc_delete_association(struct nvme_fc_ctrl *ctrl) · line 3342

Quoted 7 lines
drivers/nvme/host/fc.c +7 -0
+	/*
+	 * Wait for a started nvme_fc_fcpio_done() to complete before we
+	 * proceed with cancelling inflight requests. This guarantees a
+	 * request is completed by one of the two codepaths, not both.
+	 */
+	synchronize_rcu();
+

The synchronize_rcu() protects against nvme_cancel_tagset() racing with an fcpio_done() for an op that completed normally and therefore wasn't counted in iocnt. Wouldn't it be clearer to make the hold explicit in op->state (e.g. FCPOP_STATE_HELD) and only complete held ops in nvme_fc_flush_held_requests(), instead of using RCU as a barrier for running callbacks?

Adding new FCPOP_STATE_HELD will result in a more invasive change and it will result in nvme-fc no using nvme_cancel_tagset(). My intent with this change is to bring nvme-fc closer to other transport and only cancel inflight requests. synchronize_rcu() is a simpler in this case.

#52 lore raw

Re: [PATCH v6 11/18] nvme-rdma: Use CCR to recover controller that hits an error

In reply to Mohamed Khalfella
Quoted 7 lines · Mohamed Khalfella

An alive nvme controller that hits an error now will move to FENCING state instead of RESETTING state. ctrl->fencing_work attempts CCR to terminate inflight IOs. Regardless of the success or failure of CCR operation the controller is transitioned to RESETTING state to continue error recovery process.

Hello Sagi,

Can you please take a look v6 of this patch. I applied the change discussed in v5 by moving flush_work(&ctrl->fencing_work) to .stop_ctrl(). It looks much better now.

I would like to get these changes merged in 7.5, and getting this patch reviewed leaves only nvme-fc change which I believe Daniel is looking at now.

Thanks,

#63 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Mohamed Khalfella
Quoted 4 lines · Mohamed Khalfella

Adding new FCPOP_STATE_HELD will result in a more invasive change and it will result in nvme-fc no using nvme_cancel_tagset(). My intent with this change is to bring nvme-fc closer to other transport and only cancel inflight requests. synchronize_rcu() is a simpler in this case.

I am totally against introducing yet another synchronization method. Fix it properly.

#9 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Mohamed Khalfella
Quoted 3 lines · Mohamed Khalfella

Good to know that nvme-cli now parses these fields. That is said, the sysfs and debugfs attributes are useful to have. Say the controller is stuck in a state that it can not process an identify command.

Fair enough. I didn't consider this, in this case having the values can be helpful. Please add this info to the commit message.

#32 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Mohamed Khalfella
Quoted 2 lines · Mohamed Khalfella

Is Documentation/ABI/stable/sysfs-nvme a living history of sysfs-nvme documentation or a documentation for the specific kernel release?

I understand it documents the sysfs API. sysfs is supposed to be stable, so adding a new state is factually a breaking change. The document should document the current API the kernel provides.

#10 lore raw

Re: [PATCH v6 02/18] nvmet/debugfs: Export controller CIU and CIRN via debugfs

In reply to Daniel Wagner
Quoted 7 lines · Daniel Wagner
Quoted 3 lines · Mohamed Khalfella

Good to know that nvme-cli now parses these fields. That is said, the sysfs and debugfs attributes are useful to have. Say the controller is stuck in a state that it can not process an identify command.

Fair enough. I didn't consider this, in this case having the values can be helpful. Please add this info to the commit message.

Added a paragraph to the commit message documenting that.

#33 lore raw

Re: [PATCH v6 07/18] nvme: Introduce FENCING and FENCED controller states

In reply to Daniel Wagner
Quoted 7 lines · Daniel Wagner
Quoted 2 lines · Mohamed Khalfella

Is Documentation/ABI/stable/sysfs-nvme a living history of sysfs-nvme documentation or a documentation for the specific kernel release?

I understand it documents the sysfs API. sysfs is supposed to be stable, so adding a new state is factually a breaking change. The document should document the current API the kernel provides.

Looks like changing the "Date" or "KernelVersion" will make it like "state" attribute has been added new newer kernel. That leaves one place to mention "fencing" and "fenced" are added in newer kernel.

Documentation/ABI/stable/sysfs-nvme +3 -2
@@ -145,8 +145,9 @@ KernelVersion:      4.11
 Contact:       Sagi Grimberg <[email protected]>
 Description:
                Shows the current state of the controller. Possible values:
-               "new", "live", "resetting", "connecting", "deleting",
-               "deleting (no IO)", "dead".
+               "new", "live", "fencing", "fenced", "resetting", "connecting",
+               "deleting", "deleting (no IO)", "dead". "fencing" and "fenced"
+               were added in 7.5.
 
 What:          /sys/class/nvme/nvmeX/numa_node
 Date:          November 2018

I added this diff to this patch. Let me know if you want me to change it.

#23 lore raw

Re: [PATCH v6 06/18] nvme: Rapid Path Failure Recovery read controller identify fields

In reply to Mohamed Khalfella
Quoted 57 lines · Mohamed Khalfella
Quoted 8 lines · Daniel Wagner
Quoted 5 lines

TP8028 Rapid Path Failure added new fields to controller identify response. Read CIU (Controller Instance Uniquifier), CIRN (Controller Instance Random Number), and CCRL (Cross-Controller Reset Limit) from controller identify response. Expose CIU and CIRN as sysfs attributes so the values can be used directly by user if needed.

What's the use case for it? This is an public/stable interface, it needs a justification and documentation

CIU and CIRN identify an instance of an NVMe controller. For a given CNTLID different values of CIU and CIRN identify different controller instances of the same controller. The justification is that the values allow the user to identify a specific controller instance.

host# grep . /sys/class/nvme/nvme0/{cntlid,cirn,ciu} /sys/class/nvme/nvme0/cntlid:1 /sys/class/nvme/nvme0/cirn:42541d24813bb536 /sys/class/nvme/nvme0/ciu:57
host# nvme admin-passthru \
--opcode 0x38 \
--cdw10=$(( 0x1 | 0x57<<16 )) \
--cdw12=$(( 0x42541d24813bb536 & 0xffffffff )) \
--cdw13=$(( ( 0x42541d24813bb536 >> 32 ) & 0xffffffff )) \
/dev/nvme1
Admin Command Vendor Specific is Success and result: 0x00000000 host#

One usecase is to use the values to issue CCR command manually. Not that I imagine one would want to do that, but if they want to do it they need this information. CIU is also present in CCR log page. Someone reading the log page might want to correlate a log page entry with a specific controller, so they need the information to be exported somehow.

Documentation/ABI/stable/sysfs-nvme +11 -0
index a2f5d0710db4..ebc3634c1a96 100644
@@ -9,6 +9,17 @@ Description:
 		of the NVMe controller, as reported in the Identify
 		Controller data structure.
 
+What:		/sys/class/nvme/nvmeX/cirn
+What:		/sys/class/nvme/nvmeX/ciu
+Date:		October 2026
+KernelVersion:	7.5
+Contact:	Mohamed Khalfella <[email protected]>
+Description:
+		Shows CIRN (Controller Instance Random Number) and CIU
+		(Controller Instance Uniquifier) of the current instance
+		of the NVMe controller as reported by Identify Controller
+		data structure.
+
 What:		/sys/class/nvme/nvmeX/cntlid
 Date:		February 2016
 KernelVersion:	4.6
Quoted 6 lines

How about a diff like the one above for a documentation? I can add something similar to the one above assuming the change will be merged in 7.5. Or I can add it later on? Maybe in an -rc after the change is merged?

What do you think?

Added the diff above to the patch. Let me know if you want me to modify it.

Quoted 13 lines
Quoted 6 lines
Quoted 4 lines

Also add the nvme_fence_timeout_ms() helper that derives a fencing timeout from the controller keep-alive timeout (kato).

There is no user in this patch, introduce it when you use it.

Moved the hunk to patch "nvme: Implement cross-controller reset recovery" where the function is actually used.

Quoted 3 lines
Signature · 2 lines
-- 
Daniel Wagner <[email protected]>
#64 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Daniel Wagner
Quoted 8 lines · Daniel Wagner
Quoted 4 lines · Mohamed Khalfella

Adding new FCPOP_STATE_HELD will result in a more invasive change and it will result in nvme-fc no using nvme_cancel_tagset(). My intent with this change is to bring nvme-fc closer to other transport and only cancel inflight requests. synchronize_rcu() is a simpler in this case.

I am totally against introducing yet another synchronization method. Fix it properly.

Okay, I will give it a shot.

#72 lore raw

Re: [PATCH v6 17/18] nvme: Add support for CQT to nvme host

In reply to Daniel Wagner
Quoted 13 lines · Daniel Wagner
Quoted 12 lines · Mohamed Khalfella

TP4129 KATO Corrections and Clarifications defined CQT (Command Quiesce Time) which is used along with KATO (Keep Alive Timeout) to set an upper limit for attempting Cross-Controller Recovery. Add ctrl->cqt, read its value from controller identify response, expose it via sysfs, and have nvme_fence_timeout_ms() account for it.

Use CQT to drive time-based recovery in fc, rdma, and tcp transports. Add a fenced_work delayed work to each transport controller. If CCR fails, fencing_work switches to error recovery immediately if CQT is not supported. Otherwise it schedules fenced_work after the remaining fence timeout, which also switches to error recovery when scheduled.

Quoted 5 lines
Quoted 3 lines

the sysfs entry needs to be documented, with this

Documentation/ABI/stable/sysfs-nvme +10 -0
@@ -120,6 +120,16 @@ Description:
                Shows the Keep Alive Timeout value in milliseconds for
                this controller.
 
+What:          /sys/class/nvme/nvmeX/cqt
+Date:          October 2026
+KernelVersion: 7.5
+Contact:       Mohamed Khalfella <[email protected]>
+Description:
+               Shows CQT (Command Quiesce Time) in milliseconds as reported
+               by the Identify Controller data structure. CQT is the time the
+               controller needs to quiesce outstanding commands once the
+               host loses communication with it.
+
 What:          /sys/class/nvme/nvmeX/cmb
 Date:          October 2016
 KernelVersion: 4.9

Added the diff above to the patch.

Quoted 2 lines
Reviewed-by on v6 5/18
#18 lore raw

Re: [PATCH v6 05/18] nvmet: Send an AEN on CCR completion

In reply to Mohamed Khalfella
Quoted 23 lines · Mohamed Khalfella

When an impacted controller that is the target of pending CCRs is torn down, send an AEN to each source controller that issued a CCR against it. The notification points to the CCR log page that the source controller can read to check which CCR operation completed.

Split nvmet_add_async_event() into a locked helper so the AEN can be queued while ctrl->lock is already held.

drivers/nvme/target/core.c +0 -2
index f5d7cb9c943a..69491e8e52ba 100644
@@ -218,13 +218,19 @@ void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
        aen->event_info = event_info;
        aen->log_page = log_page;
 
-       mutex_lock(&ctrl->lock);
        list_add_tail(&aen->entry, &ctrl->async_events);
-       mutex_unlock(&ctrl->lock);
 
        queue_work(nvmet_aen_wq, &ctrl->async_event_work);
 }

...

Quoted 1 line

+ lockdep_assert_held(&ctrl->lock);

Is there a reason not to replace the mutex_lock() call with a lockdep_assert_held() call?

#19 lore raw

Re: [PATCH v6 05/18] nvmet: Send an AEN on CCR completion

Quoted 23 lines · Randy Jennings
Quoted 21 lines · Mohamed Khalfella

When an impacted controller that is the target of pending CCRs is torn down, send an AEN to each source controller that issued a CCR against it. The notification points to the CCR log page that the source controller can read to check which CCR operation completed.

Split nvmet_add_async_event() into a locked helper so the AEN can be queued while ctrl->lock is already held.

drivers/nvme/target/core.c +0 -2
index f5d7cb9c943a..69491e8e52ba 100644
@@ -218,13 +218,19 @@ void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
        aen->event_info = event_info;
        aen->log_page = log_page;
 
-       mutex_lock(&ctrl->lock);
        list_add_tail(&aen->entry, &ctrl->async_events);
-       mutex_unlock(&ctrl->lock);
 
Quoted 6 lines
Quoted 2 lines
drivers/nvme/target/core.c +0 -0
        queue_work(nvmet_aen_wq, &ctrl->async_event_work);
 }

...

Quoted 1 line

+ lockdep_assert_held(&ctrl->lock);

Is there a reason not to replace the mutex_lock() call with a lockdep_assert_held() call?

No. There is no reason not to assert ctrl->lock mutex is held when the function is called. I updated the function and added the assert.

drivers/nvme/target/core.c +2 -0
@@ -210,6 +210,8 @@ static void nvmet_add_async_event_locked(struct nvmet_ctrl *ctrl, u8 event_type,
 {
        struct nvmet_async_event *aen;
 
+       lockdep_assert_held(&ctrl->lock);
+
        aen = kmalloc_obj(*aen);
        if (!aen)
                return;
#39 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Daniel Wagner
Quoted 65 lines · Daniel Wagner
Quoted 45 lines
drivers/nvme/host/core.c +45 -0
+static int nvme_issue_wait_ccr(struct nvme_ctrl *sctrl, struct nvme_ctrl *ictrl,
+			       unsigned long deadline)
+{
+	struct nvme_ccr_entry ccr = { };
+	union nvme_result res = { 0 };
+	struct nvme_command c = { };
+	unsigned long flags, now, tmo = 0;
+	bool completed = false;
+	int ret = 0;
+	u32 result;
+
+	init_completion(&ccr.complete);
+	ccr.ictrl = ictrl;
+
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_add_tail(&ccr.list, &sctrl->ccr_list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+
+	c.ccr.opcode = nvme_admin_cross_ctrl_reset;
+	c.ccr.ciu = ictrl->ciu;
+	c.ccr.icid = cpu_to_le16(ictrl->cntlid);
+	c.ccr.cirn = cpu_to_le64(ictrl->cirn);
+	ret = __nvme_submit_sync_cmd(sctrl->admin_q, &c, &res,
+				     NULL, 0, NVME_QID_ANY, 0);
+	if (ret) {
+		ret = -EIO;
+		goto out;
+	}
+
+	result = le32_to_cpu(res.u32);
+	if (result & 0x01) /* Immediate Reset Successful */
+		goto out;
+
+	now = jiffies;
+	if (time_before(now, deadline))
+		tmo = min_t(unsigned long,
+			    secs_to_jiffies(ictrl->kato), deadline - now);
+
+	if (!wait_for_completion_timeout(&ccr.complete, tmo)) {
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+
+	completed = true;
+

drivers/nvme/host/core.c void nvme_cancel_admin_tagset(struct nvme_ctrl *ctrl) · line 682

Quoted 10 lines
drivers/nvme/host/core.c +10 -0
+out:
+	spin_lock_irqsave(&sctrl->lock, flags);
+	list_del(&ccr.list);
+	spin_unlock_irqrestore(&sctrl->lock, flags);
+	if (completed) {
+		if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
+			return 0;
+		return -EREMOTEIO;
+	}
+	return ret;

This part could be made a bit simpler and there is no need for the local completed variable:

[...]
if (!wait_for_completion_timeout(&ccr.complete, tmo))
ret = -ETIMEDOUT;

out:

Quoted 10 lines
        spin_lock_irqsave(&sctrl->lock, flags);
        list_del(&ccr.list);
        spin_unlock_irqrestore(&sctrl->lock, flags);

        if (ret || !completion_done(&ccr.complete))
                return ret;
        if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
                return 0;
        return -EREMOTEIO;
}

701
702 if (!wait_for_completion_timeout(&ccr.complete, tmo))
703 ret = -ETIMEDOUT;
704
705 out:

 706         spin_lock_irqsave(&sctrl->lock, flags);
 707         list_del(&ccr.list);
 708         spin_unlock_irqrestore(&sctrl->lock, flags);

709
710 if (ret || !completion_done(&ccr.complete))
711 return ret;
712 if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
713 return 0;
714 return -EREMOTEIO;
715 }

Actually, this code has a bug. It does not correctly handle the case when CCR operation completes with an error. I added line numbers to show that.

- line 701: AEN, omplete(ccr.complete), x->done++, x->done = 1
- line 702: wait_for_completion_timeout(), x->done--, x->done = 0
- line 710: ret == 0, x->done == 0, completion_done() returns false.
- line 711: return 0;

The problem is that wait_for_completion_timeout() consumes the completion. This causes completion_done() to return false leading us to believe CCR operation has not completed.

#65 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Daniel Wagner
Quoted 8 lines · Daniel Wagner
Quoted 4 lines · Mohamed Khalfella

Adding new FCPOP_STATE_HELD will result in a more invasive change and it will result in nvme-fc no using nvme_cancel_tagset(). My intent with this change is to bring nvme-fc closer to other transport and only cancel inflight requests. synchronize_rcu() is a simpler in this case.

I am totally against introducing yet another synchronization method. Fix it properly.

Here is a diff that applies on top of the patches in order to remove synchronize_rcu() and add FCPOP_STATE_HELD as you suggested. I will try to run more testing on this change to see if it holds up. While I do that please take a look and let me know what do you think.

drivers/nvme/host/fc.c +49 -21
index 2bb6cdfbb718..6c260f6b47b2 100644
@@ -83,6 +83,7 @@ enum nvme_fcpop_state {
 	FCPOP_STATE_ACTIVE	= 2,
 	FCPOP_STATE_ABORTED	= 3,
 	FCPOP_STATE_COMPLETE	= 4,
+	FCPOP_STATE_HELD	= 5,
 };
 
 struct nvme_fc_fcp_op {
@@ -2086,9 +2087,6 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 	 * association to be terminated.
 	 */
 
-	/* Pairs with synchronize_rcu() in __nvme_fc_teardown_association() */
-	rcu_read_lock();
-
 	opstate = atomic_xchg(&op->state, FCPOP_STATE_COMPLETE);
 
 	fc_dma_sync_single_for_cpu(ctrl->lport->dev, op->fcp_req.rspdma,
@@ -2201,12 +2199,18 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 
 	/*
 	 * If we are going to terminate associations and the controller is
-	 * LIVE or FENCING, then do not complete this request now. Let error
-	 * recovery cancel this request when it is safe to do so.
+	 * LIVE or FENCING, then do not complete this request now. Move the
+	 * op to HELD and let nvme_fc_cancel_held_requests() cancel it when
+	 * it is safe to do so. HELD is only reachable from COMPLETE through
+	 * this cmpxchg: if the cancellation has already walked the tagset it
+	 * moved the op out of COMPLETE, the cmpxchg fails and the request is
+	 * completed here instead. Either way exactly one path completes it.
 	 */
 	state = nvme_ctrl_state(&ctrl->ctrl);
 	if (terminate_assoc &&
-	    (state == NVME_CTRL_LIVE || state == NVME_CTRL_FENCING))
+	    (state == NVME_CTRL_LIVE || state == NVME_CTRL_FENCING) &&
+	    atomic_cmpxchg(&op->state, FCPOP_STATE_COMPLETE,
+			   FCPOP_STATE_HELD) == FCPOP_STATE_COMPLETE)
 		goto check_op_term;
 
 	if (!nvme_try_complete_req(rq, status, result))
@@ -2218,8 +2222,6 @@ nvme_fc_fcpio_done(struct nvmefc_fcp_req *req)
 check_error:
 	if (terminate_assoc)
 		nvme_fc_start_ioerr_recovery(ctrl, "io error");
-
-	rcu_read_unlock();
 }
 
 static int
@@ -3364,13 +3366,6 @@ __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl)
 	clear_bit(FCCTRL_TERMIO, &ctrl->flags);
 	spin_unlock_irq(&ctrl->lock);
 
-	/*
-	 * Wait for a started nvme_fc_fcpio_done() to complete before we
-	 * proceed with cancelling inflight requests. This guarantees a
-	 * request is completed by one of the two codepaths, not both.
-	 */
-	synchronize_rcu();
-
 	nvme_fc_term_aen_ops(ctrl);
 
 	/*
@@ -3405,18 +3400,51 @@ __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl)
 	nvme_fc_ctlr_inactive_on_rport(ctrl);
 }
 
-/* Complete the requests held in __nvme_fc_teardown_association() */
+static bool
+nvme_fc_cancel_held_request(struct request *req, void *data)
+{
+	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
+	struct nvme_ctrl *ctrl = data;
+
+	/*
+	 * A single cmpxchg decides who completes this request. If the op is
+	 * HELD, nvme_fc_fcpio_done() is done with it and we cancel it. If it
+	 * is still COMPLETE, nvme_fc_fcpio_done() has not decided yet: move
+	 * it to IDLE so its own cmpxchg to HELD fails and it completes the
+	 * request itself (nvme_fc_complete_rq() sets IDLE anyway). Doing this
+	 * as read-then-cmpxchg would let the op become HELD in between and
+	 * leave it with no one to complete it.
+	 */
+	if (atomic_cmpxchg(&op->state, FCPOP_STATE_COMPLETE,
+			   FCPOP_STATE_IDLE) == FCPOP_STATE_HELD)
+		nvme_cancel_request(req, ctrl);
+
+	return true;
+}
+
 static void
-nvme_fc_flush_held_requests(struct nvme_fc_ctrl *ctrl)
+nvme_fc_cancel_held_requests(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
 {
-	nvme_cancel_tagset(&ctrl->ctrl);
-	nvme_cancel_admin_tagset(&ctrl->ctrl);
+	if (set) {
+		blk_mq_tagset_busy_iter(set, nvme_fc_cancel_held_request, ctrl);
+		blk_mq_tagset_wait_completed_request(set);
+	}
+}
+
+/* Complete the requests nvme_fc_fcpio_done() held during the fence */
+static void
+nvme_fc_flush_held_requests(struct nvme_fc_ctrl *fc_ctrl)
+{
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+
+	nvme_fc_cancel_held_requests(ctrl, ctrl->tagset);
+	nvme_fc_cancel_held_requests(ctrl, ctrl->admin_tagset);
 
 	/* re-enable the admin_q so anything new can fast fail */
-	nvme_unquiesce_admin_queue(&ctrl->ctrl);
+	nvme_unquiesce_admin_queue(ctrl);
 
 	/* resume the io queues so that things will fast fail */
-	nvme_unquiesce_io_queues(&ctrl->ctrl);
+	nvme_unquiesce_io_queues(ctrl);
 }
 
 /*
#40 lore raw

Re: [PATCH v6 08/18] nvme: Implement cross-controller reset recovery

In reply to Mohamed Khalfella
Quoted 16 lines · Mohamed Khalfella

701
702 if (!wait_for_completion_timeout(&ccr.complete, tmo))
703 ret = -ETIMEDOUT;
704
705 out:

 706         spin_lock_irqsave(&sctrl->lock, flags);
 707         list_del(&ccr.list);
 708         spin_unlock_irqrestore(&sctrl->lock, flags);

709
710 if (ret || !completion_done(&ccr.complete))
711 return ret;
712 if (ccr.ccrs == NVME_CCR_STATUS_SUCCESS)
713 return 0;
714 return -EREMOTEIO;
715 }

Quoted 3 lines

Actually, this code has a bug. It does not correctly handle the case when CCR operation completes with an error. I added line numbers to show that.

Fair enough, then let's use the local variable.

#66 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

In reply to Mohamed Khalfella
Quoted 20 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +19 -1
-/* Complete the requests held in __nvme_fc_teardown_association() */
+static bool
+nvme_fc_cancel_held_request(struct request *req, void *data)
+{
+	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
+	struct nvme_ctrl *ctrl = data;
+
+	/*
+	 * A single cmpxchg decides who completes this request. If the op is
+	 * HELD, nvme_fc_fcpio_done() is done with it and we cancel it. If it
+	 * is still COMPLETE, nvme_fc_fcpio_done() has not decided yet: move
+	 * it to IDLE so its own cmpxchg to HELD fails and it completes the
+	 * request itself (nvme_fc_complete_rq() sets IDLE anyway). Doing this
+	 * as read-then-cmpxchg would let the op become HELD in between and
+	 * leave it with no one to complete it.
+	 */
+	if (atomic_cmpxchg(&op->state, FCPOP_STATE_COMPLETE,
+			   FCPOP_STATE_IDLE) == FCPOP_STATE_HELD)
+		nvme_cancel_request(req, ctrl);
+

drivers/nvme/host/fc.c __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl) · line 3428

Quoted 10 lines
drivers/nvme/host/fc.c +5 -3
+	return true;
+}
+
 static void
-nvme_fc_flush_held_requests(struct nvme_fc_ctrl *ctrl)
+nvme_fc_cancel_held_requests(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
 {
-	nvme_cancel_tagset(&ctrl->ctrl);
-	nvme_cancel_admin_tagset(&ctrl->ctrl);
+	if (set) {

Do we call flush when we don't have a set?

nitpick: use the early return pattern if needed.

Quoted 14 lines
drivers/nvme/host/fc.c +13 -0
+		blk_mq_tagset_busy_iter(set, nvme_fc_cancel_held_request, ctrl);
+		blk_mq_tagset_wait_completed_request(set);
+	}
+}
+
+/* Complete the requests nvme_fc_fcpio_done() held during the fence */
+static void
+nvme_fc_flush_held_requests(struct nvme_fc_ctrl *fc_ctrl)
+{
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+
+	nvme_fc_cancel_held_requests(ctrl, ctrl->tagset);
+	nvme_fc_cancel_held_requests(ctrl, ctrl->admin_tagset);
 

drivers/nvme/host/fc.c __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl) · line 3450

Quoted 10 lines
drivers/nvme/host/fc.c +2 -2
 	/* re-enable the admin_q so anything new can fast fail */
-	nvme_unquiesce_admin_queue(&ctrl->ctrl);
+	nvme_unquiesce_admin_queue(ctrl);
 
 	/* resume the io queues so that things will fast fail */
-	nvme_unquiesce_io_queues(&ctrl->ctrl);
+	nvme_unquiesce_io_queues(ctrl);
 }
 
 /*

Yes, I'd prefer this over the introduction of RCU in this code path.

#67 lore raw

Re: [PATCH v6 15/18] nvme-fc: Hold inflight requests while in FENCING state

Quoted 25 lines · Daniel Wagner
Quoted 24 lines · Mohamed Khalfella
drivers/nvme/host/fc.c +22 -1
-/* Complete the requests held in __nvme_fc_teardown_association() */
+static bool
+nvme_fc_cancel_held_request(struct request *req, void *data)
+{
+	struct nvme_fc_fcp_op *op = blk_mq_rq_to_pdu(req);
+	struct nvme_ctrl *ctrl = data;
+
+	/*
+	 * A single cmpxchg decides who completes this request. If the op is
+	 * HELD, nvme_fc_fcpio_done() is done with it and we cancel it. If it
+	 * is still COMPLETE, nvme_fc_fcpio_done() has not decided yet: move
+	 * it to IDLE so its own cmpxchg to HELD fails and it completes the
+	 * request itself (nvme_fc_complete_rq() sets IDLE anyway). Doing this
+	 * as read-then-cmpxchg would let the op become HELD in between and
+	 * leave it with no one to complete it.
+	 */
+	if (atomic_cmpxchg(&op->state, FCPOP_STATE_COMPLETE,
+			   FCPOP_STATE_IDLE) == FCPOP_STATE_HELD)
+		nvme_cancel_request(req, ctrl);
+
+	return true;
+}
+
 static void
Quoted 8 lines

drivers/nvme/host/fc.c __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl) · line 3428

Quoted 6 lines
drivers/nvme/host/fc.c +2 -3
-nvme_fc_flush_held_requests(struct nvme_fc_ctrl *ctrl)
+nvme_fc_cancel_held_requests(struct nvme_ctrl *ctrl, struct blk_mq_tag_set *set)
 {
-	nvme_cancel_tagset(&ctrl->ctrl);
-	nvme_cancel_admin_tagset(&ctrl->ctrl);
+	if (set) {

Do we call flush when we don't have a set?

Yes, that is possible in case the controller fail first connect attempt before the IO tagset is created. Commit ee38469f8849 ("nvme-fc: Do not cancel requests in io target before it is initialized") added similar check to __nvme_fc_abort_outstanding_ios() to close this gap.

Quoted 2 lines

nitpick: use the early return pattern if needed.

Done.

Quoted 19 lines
Quoted 19 lines
drivers/nvme/host/fc.c +14 -1
+		blk_mq_tagset_busy_iter(set, nvme_fc_cancel_held_request, ctrl);
+		blk_mq_tagset_wait_completed_request(set);
+	}
+}
+
+/* Complete the requests nvme_fc_fcpio_done() held during the fence */
+static void
+nvme_fc_flush_held_requests(struct nvme_fc_ctrl *fc_ctrl)
+{
+	struct nvme_ctrl *ctrl = &fc_ctrl->ctrl;
+
+	nvme_fc_cancel_held_requests(ctrl, ctrl->tagset);
+	nvme_fc_cancel_held_requests(ctrl, ctrl->admin_tagset);
 
 	/* re-enable the admin_q so anything new can fast fail */
-	nvme_unquiesce_admin_queue(&ctrl->ctrl);
+	nvme_unquiesce_admin_queue(ctrl);
Quoted 9 lines

drivers/nvme/host/fc.c __nvme_fc_teardown_association(struct nvme_fc_ctrl *ctrl) · line 3450

Quoted 6 lines
drivers/nvme/host/fc.c +1 -1
 	/* resume the io queues so that things will fast fail */
-	nvme_unquiesce_io_queues(&ctrl->ctrl);
+	nvme_unquiesce_io_queues(ctrl);
 }
 
 /*

Yes, I'd prefer this over the introduction of RCU in this code path.

Okay. I spent a lot of time thinking about these two atomic_cmpxchg() to make sure they cover all scenarios. That is holding all requests to be held and at the same time not leave a request in a held state and not get to complete it. Let me know if you think I missed a scenario.