Skip to contentSkip to conversation

nvmet: prevent redundant enable/disable of nvmet_port

PATCH In review Max Gurtovoy 3 participants 9 messages Sep 20, 2025 – Sep 22, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 nvmet: prevent redundant enable/disable of nvmet_port

Add early return checks in nvmet_enable_port() and nvmet_disable_port() to avoid redundant operations if the port is already enabled or disabled.

---
 drivers/nvme/target/core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/nvme/target/core.c +6 -0
index 0dd7bd99afa3..9f5159f04bbd 100644
@@ -317,6 +317,9 @@ int nvmet_enable_port(struct nvmet_port *port)
 
 	lockdep_assert_held(&nvmet_config_sem);
 
+	if (port->enabled)
+		return 0;
+
 	if (port->disc_addr.trtype == NVMF_TRTYPE_MAX)
 		return -EINVAL;
 
@@ -382,6 +385,9 @@ void nvmet_disable_port(struct nvmet_port *port)
 
 	lockdep_assert_held(&nvmet_config_sem);
 
+	if (!port->enabled)
+		return;
+
 	port->enabled = false;
 	port->tr_ops = NULL;
 
Signature · 1 line
2.18.1
#1 lore raw

PATCH 2/3 nvmet: use port's tr_ops in nvmet_disable_port

Use the port's tr_ops pointer when disabling a port, instead of looking up the ops by the transport type. An enabled port is guaranteed to have a valid reference to its fabrics ops.

---
 drivers/nvme/target/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +2 -1
index 9f5159f04bbd..f31c7fa70740 100644
@@ -388,10 +388,11 @@ void nvmet_disable_port(struct nvmet_port *port)
 	if (!port->enabled)
 		return;
 
+	ops = port->tr_ops;
+
 	port->enabled = false;
 	port->tr_ops = NULL;
 
-	ops = nvmet_transports[port->disc_addr.trtype];
 	ops->remove_port(port);
 	module_put(ops->owner);
 }
Signature · 1 line
2.18.1
#2 lore raw

Re: [PATCH 2/3] nvmet: use port's tr_ops in nvmet_disable_port

Quoted 14 lines · Max Gurtovoy

Use the port's tr_ops pointer when disabling a port, instead of looking up the ops by the transport type. An enabled port is guaranteed to have a valid reference to its fabrics ops.

---
 drivers/nvme/target/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/target/core.c +0 -0
index 9f5159f04bbd..f31c7fa70740 100644

drivers/nvme/target/core.c void nvmet_disable_port(struct nvmet_port *port) · line 392

Quoted 5 lines
drivers/nvme/target/core.c +2 -0
@@ -388,10 +388,11 @@ void nvmet_disable_port(struct nvmet_port *port)
 	if (!port->enabled)
 		return;
 
+	ops = port->tr_ops;
+

Please assign ops in the line declaring it.

Otherwise this looks fine.

#3 lore raw

PATCH 3/3 nvmet: replace transports array with a linked list

In reply to Max Gurtovoy

The nvmet_transports array was sized to 255 (NVMF_TRTYPE_MAX), although only up to 5 transport types can ever be registered, resulting in some unused memory. This change replaces the array with a linked list to store registered transport ops, substantially improving memory efficiency by allocating space only for active transports.

---
 drivers/nvme/target/core.c    | 48 +++++++++++++++++++++++++----------
 drivers/nvme/target/fc.c      |  4 +--
 drivers/nvme/target/loop.c    |  4 +--
 drivers/nvme/target/nvmet.h   |  5 ++--
 drivers/nvme/target/pci-epf.c |  2 +-
 drivers/nvme/target/rdma.c    |  4 +--
 drivers/nvme/target/tcp.c     |  4 +--
 7 files changed, 47 insertions(+), 24 deletions(-)
drivers/nvme/target/core.c +35 -13
index f31c7fa70740..7ee12819e2b5 100644
@@ -21,7 +21,7 @@
 struct kmem_cache *nvmet_bvec_cache;
 struct workqueue_struct *buffered_io_wq;
 struct workqueue_struct *zbd_wq;
-static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
+static LIST_HEAD(nvmet_transports);
 static DEFINE_IDA(cntlid_ida);
 
 struct workqueue_struct *nvmet_wq;
@@ -37,7 +37,7 @@ EXPORT_SYMBOL_GPL(nvmet_wq);
  *  - per-subsystem allowed hosts list
  *  - allow_any_host subsystem attribute
  *  - nvmet_genctr
- *  - the nvmet_transports array
+ *  - the nvmet_transports list
  *
  * When updating any of those lists/structures write lock should be obtained,
  * while when reading (popolating discovery log page or checking host-subsystem
@@ -275,25 +275,46 @@ void nvmet_port_send_ana_event(struct nvmet_port *port)
 	up_read(&nvmet_config_sem);
 }
 
-int nvmet_register_transport(const struct nvmet_fabrics_ops *ops)
+static struct nvmet_fabrics_ops *nvmet_lookup_transport(unsigned int type)
 {
-	int ret = 0;
+	struct nvmet_fabrics_ops *ops;
 
+	lockdep_assert_held(&nvmet_config_sem);
+
+	list_for_each_entry(ops, &nvmet_transports, entry) {
+		if (ops->type == type)
+			return ops;
+	}
+
+	return NULL;
+}
+
+int nvmet_register_transport(struct nvmet_fabrics_ops *ops)
+{
 	down_write(&nvmet_config_sem);
-	if (nvmet_transports[ops->type])
-		ret = -EINVAL;
-	else
-		nvmet_transports[ops->type] = ops;
+	if (nvmet_lookup_transport(ops->type)) {
+		up_write(&nvmet_config_sem);
+		return -EEXIST;
+	}
+
+	list_add_tail(&ops->entry, &nvmet_transports);
 	up_write(&nvmet_config_sem);
 
-	return ret;
+	return 0;
 }
 EXPORT_SYMBOL_GPL(nvmet_register_transport);
 
-void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops)
+void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops)
 {
+	struct nvmet_fabrics_ops *fops, *tmp;
+
 	down_write(&nvmet_config_sem);
-	nvmet_transports[ops->type] = NULL;
+	list_for_each_entry_safe(fops, tmp, &nvmet_transports, entry) {
+		if (fops == ops) {
+			list_del(&ops->entry);
+			break;
+		}
+	}
 	up_write(&nvmet_config_sem);
 }
 EXPORT_SYMBOL_GPL(nvmet_unregister_transport);
@@ -323,12 +344,12 @@ int nvmet_enable_port(struct nvmet_port *port)
 	if (port->disc_addr.trtype == NVMF_TRTYPE_MAX)
 		return -EINVAL;
 
-	ops = nvmet_transports[port->disc_addr.trtype];
+	ops = nvmet_lookup_transport(port->disc_addr.trtype);
 	if (!ops) {
 		up_write(&nvmet_config_sem);
 		request_module("nvmet-transport-%d", port->disc_addr.trtype);
 		down_write(&nvmet_config_sem);
-		ops = nvmet_transports[port->disc_addr.trtype];
+		ops = nvmet_lookup_transport(port->disc_addr.trtype);
 		if (!ops) {
 			pr_err("transport type %d not supported\n",
 				port->disc_addr.trtype);
@@ -2006,6 +2027,7 @@ static void __exit nvmet_exit(void)
 	destroy_workqueue(buffered_io_wq);
 	destroy_workqueue(zbd_wq);
 	kmem_cache_destroy(nvmet_bvec_cache);
+	WARN_ON_ONCE(!list_empty(&nvmet_transports));
 
 	BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_entry) != 1024);
 	BUILD_BUG_ON(sizeof(struct nvmf_disc_rsp_page_hdr) != 1024);
drivers/nvme/target/fc.c +2 -2
index a9b18c051f5b..58dfb658453c 100644
@@ -1932,7 +1932,7 @@ nvmet_fc_ls_disconnect(struct nvmet_fc_tgtport *tgtport,
 
 static void nvmet_fc_fcp_nvme_cmd_done(struct nvmet_req *nvme_req);
 
-static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
+static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops;
 
 static void
 nvmet_fc_xmt_ls_rsp_done(struct nvmefc_ls_rsp *lsrsp)
@@ -2994,7 +2994,7 @@ nvmet_fc_host_traddr(struct nvmet_ctrl *ctrl,
 	return ret;
 }
 
-static const struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
+static struct nvmet_fabrics_ops nvmet_fc_tgt_fcp_ops = {
 	.owner			= THIS_MODULE,
 	.type			= NVMF_TRTYPE_FC,
 	.msdbd			= 1,
drivers/nvme/target/loop.c +2 -2
index f85a8441bcc6..72c126b95fd2 100644
@@ -66,7 +66,7 @@ static DEFINE_MUTEX(nvme_loop_ctrl_mutex);
 static void nvme_loop_queue_response(struct nvmet_req *nvme_req);
 static void nvme_loop_delete_ctrl(struct nvmet_ctrl *ctrl);
 
-static const struct nvmet_fabrics_ops nvme_loop_ops;
+static struct nvmet_fabrics_ops nvme_loop_ops;
 
 static inline int nvme_loop_queue_idx(struct nvme_loop_queue *queue)
 {
@@ -670,7 +670,7 @@ static void nvme_loop_remove_port(struct nvmet_port *port)
 	flush_workqueue(nvme_delete_wq);
 }
 
-static const struct nvmet_fabrics_ops nvme_loop_ops = {
+static struct nvmet_fabrics_ops nvme_loop_ops = {
 	.owner		= THIS_MODULE,
 	.type		= NVMF_TRTYPE_LOOP,
 	.add_port	= nvme_loop_add_port,
drivers/nvme/target/nvmet.h +3 -2
index 51df72f5e89b..2d29df0f66eb 100644
@@ -409,6 +409,7 @@ struct nvmet_subsys_link {
 struct nvmet_req;
 struct nvmet_fabrics_ops {
 	struct module *owner;
+	struct list_head entry;
 	unsigned int type;
 	unsigned int msdbd;
 	unsigned int flags;
@@ -637,8 +638,8 @@ void nvmet_send_ana_event(struct nvmet_subsys *subsys,
 		struct nvmet_port *port);
 void nvmet_port_send_ana_event(struct nvmet_port *port);
 
-int nvmet_register_transport(const struct nvmet_fabrics_ops *ops);
-void nvmet_unregister_transport(const struct nvmet_fabrics_ops *ops);
+int nvmet_register_transport(struct nvmet_fabrics_ops *ops);
+void nvmet_unregister_transport(struct nvmet_fabrics_ops *ops);
 
 void nvmet_port_del_ctrls(struct nvmet_port *port,
 			  struct nvmet_subsys *subsys);
drivers/nvme/target/pci-epf.c +1 -1
index 2e78397a7373..6be4e7586a98 100644
@@ -1523,7 +1523,7 @@ static u16 nvmet_pci_epf_set_feat(const struct nvmet_ctrl *tctrl,
 	}
 }
 
-static const struct nvmet_fabrics_ops nvmet_pci_epf_fabrics_ops = {
+static struct nvmet_fabrics_ops nvmet_pci_epf_fabrics_ops = {
 	.owner		= THIS_MODULE,
 	.type		= NVMF_TRTYPE_PCI,
 	.add_port	= nvmet_pci_epf_add_port,
drivers/nvme/target/rdma.c +2 -2
index 0485e25ab797..9336cb7e25bb 100644
@@ -176,7 +176,7 @@ static int nvmet_rdma_alloc_rsp(struct nvmet_rdma_device *ndev,
 				struct nvmet_rdma_rsp *r,
 				int tag);
 
-static const struct nvmet_fabrics_ops nvmet_rdma_ops;
+static struct nvmet_fabrics_ops nvmet_rdma_ops;
 
 static int srq_size_set(const char *val, const struct kernel_param *kp)
 {
@@ -2038,7 +2038,7 @@ static u16 nvmet_rdma_get_max_queue_size(const struct nvmet_ctrl *ctrl)
 	return NVME_RDMA_MAX_QUEUE_SIZE;
 }
 
-static const struct nvmet_fabrics_ops nvmet_rdma_ops = {
+static struct nvmet_fabrics_ops nvmet_rdma_ops = {
 	.owner			= THIS_MODULE,
 	.type			= NVMF_TRTYPE_RDMA,
 	.msdbd			= 1,
drivers/nvme/target/tcp.c +2 -2
index 470bf37e5a63..88e728cb4779 100644
@@ -211,7 +211,7 @@ static LIST_HEAD(nvmet_tcp_queue_list);
 static DEFINE_MUTEX(nvmet_tcp_queue_mutex);
 
 static struct workqueue_struct *nvmet_tcp_wq;
-static const struct nvmet_fabrics_ops nvmet_tcp_ops;
+static struct nvmet_fabrics_ops nvmet_tcp_ops;
 static void nvmet_tcp_free_cmd(struct nvmet_tcp_cmd *c);
 static void nvmet_tcp_free_cmd_buffers(struct nvmet_tcp_cmd *cmd);
 
@@ -2180,7 +2180,7 @@ static ssize_t nvmet_tcp_host_port_addr(struct nvmet_ctrl *ctrl,
 			(struct sockaddr *)&queue->sockaddr_peer);
 }
 
-static const struct nvmet_fabrics_ops nvmet_tcp_ops = {
+static struct nvmet_fabrics_ops nvmet_tcp_ops = {
 	.owner			= THIS_MODULE,
 	.type			= NVMF_TRTYPE_TCP,
 	.msdbd			= 1,
Signature · 1 line
2.18.1
#4 lore raw

Re: [PATCH 3/3] nvmet: replace transports array with a linked list

Quoted 5 lines · Max Gurtovoy

The nvmet_transports array was sized to 255 (NVMF_TRTYPE_MAX), although only up to 5 transport types can ever be registered, resulting in some unused memory. This change replaces the array with a linked list to store registered transport ops, substantially improving memory efficiency by allocating space only for active transports.

This adds extra code and makes previously const data structures mutable to save a little bit of space, which feels like the wrong tradeoff.

What is your motivation here?

#5 lore raw

Re: [PATCH 3/3] nvmet: replace transports array with a linked list

Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Max Gurtovoy

The nvmet_transports array was sized to 255 (NVMF_TRTYPE_MAX), although only up to 5 transport types can ever be registered, resulting in some unused memory. This change replaces the array with a linked list to store registered transport ops, substantially improving memory efficiency by allocating space only for active transports.

This adds extra code and makes previously const data structures mutable to save a little bit of space, which feels like the wrong tradeoff.

What is your motivation here?

The main goal is to save the wasted memory (even though it is no huge).

maybe we can do something like this instead:

include/linux/nvme.h +0 -0
index 655d194f8e72..8e076960875c 100644
@@ -68,7 +68,7 @@ enum {

        NVMF_TRTYPE_RDMA        = 1,    /* RDMA */ NVMF_TRTYPE_FC          = 2,    /* Fibre Channel */ NVMF_TRTYPE_TCP         = 3,    /* TCP/IP */
-       NVMF_TRTYPE_LOOP        = 254,  /* Reserved for host usage */ +       NVMF_TRTYPE_LOOP        = 15,   /* Reserved for host usage */ NVMF_TRTYPE_MAX,
 };

maintainer
#6 lore raw

Re: [PATCH 3/3] nvmet: replace transports array with a linked list

Quoted 8 lines · Max Gurtovoy

maybe we can do something like this instead:

include/linux/nvme.h +0 -0
index 655d194f8e72..8e076960875c 100644
@@ -68,7 +68,7 @@ enum {
Quoted 7 lines

        NVMF_TRTYPE_RDMA        = 1,    /* RDMA */ NVMF_TRTYPE_FC          = 2,    /* Fibre Channel */ NVMF_TRTYPE_TCP         = 3,    /* TCP/IP */
-       NVMF_TRTYPE_LOOP        = 254,  /* Reserved for host usage */ +       NVMF_TRTYPE_LOOP        = 15,   /* Reserved for host usage */ NVMF_TRTYPE_MAX,
 };

The 254 value for the host loopback transport type is in defined that way in the fabrics spec, so we can't readily change in the driver.

#7 lore raw

Re: [PATCH 3/3] nvmet: replace transports array with a linked list

Quoted 17 lines · Keith Busch
Quoted 7 lines · Max Gurtovoy

maybe we can do something like this instead:

include/linux/nvme.h +0 -0
index 655d194f8e72..8e076960875c 100644

include/linux/nvme.h enum { · line 73

Quoted 7 lines
include/linux/nvme.h +1 -1
@@ -68,7 +68,7 @@ enum {
         NVMF_TRTYPE_RDMA        = 1,    /* RDMA */
         NVMF_TRTYPE_FC          = 2,    /* Fibre Channel */
         NVMF_TRTYPE_TCP         = 3,    /* TCP/IP */
-       NVMF_TRTYPE_LOOP        = 254,  /* Reserved for host usage */
+       NVMF_TRTYPE_LOOP        = 15,   /* Reserved for host usage */
         NVMF_TRTYPE_MAX,
  };

The 254 value for the host loopback transport type is in defined that way in the fabrics spec, so we can't readily change in the driver.

ohh, I was sure it's out of spec but I guess you're right.

Anyway, we use a list in the host fabrics driver for the same purpose so we probably need to align having a single design: an array with const structures or a list with non-const structures