Skip to contentSkip to conversation

nvmet: parametrize maximum number of namespaces

PATCH In review Aleksandr Dyadyushkin 4 participants 5 messages Feb 22, 2022

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

PATCH 1/1 nvmet: parametrize maximum number of namespaces

From: Aleksandr Dyadyushkin <[email protected]>

Allow the end user (i.e. the administrator) to set maximum number of namespaces (and thus NN and MNAN implicitly) via module loading-time parameter, without a need to modify and rebuild source code.

Most of all this can be helpful for mock-up configurations and possibly really sparse NSID assignments.

For backwards compatibility, default values are left as is.

While permitted values are choosen as high as possible for the current code architecture and standard limitations, it is worth noting that allocations at 'nvmet_execute_get_log_page_ana' might yield a failure in runtime on systems with high parameter value and low free memory.

---
 drivers/nvme/target/admin-cmd.c |  6 +++---
 drivers/nvme/target/core.c      | 28 +++++++++++++++++++++++++++-
 drivers/nvme/target/nvmet.h     |  2 ++
 3 files changed, 32 insertions(+), 4 deletions(-)
drivers/nvme/target/admin-cmd.c +3 -3
index 6fb24746de06..940297233986 100644
@@ -278,7 +278,7 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
 	u16 status;
 
 	status = NVME_SC_INTERNAL;
-	desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+	desc = kmalloc(struct_size(desc, nsids, nvmet_max_namespaces),
 		       GFP_KERNEL);
 	if (!desc)
 		goto out;
@@ -428,8 +428,8 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
 	/* no enforcement soft-limit for maxcmd - pick arbitrary high value */
 	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
 
-	id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
-	id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
+	id->nn = cpu_to_le32(nvmet_max_namespaces);
+	id->mnan = cpu_to_le32(nvmet_max_namespaces);
 	id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
 			NVME_CTRL_ONCS_WRITE_ZEROES);
 
drivers/nvme/target/core.c +27 -1
index 5119c687de68..1b925b40074d 100644
@@ -15,6 +15,32 @@
 
 #include "nvmet.h"
 
+static int max_namespaces_set(const char *val, const struct kernel_param *kp);
+static const struct kernel_param_ops max_namespaces_ops = {
+	.set = max_namespaces_set,
+	.get = param_get_uint,
+};
+
+/* Account for 0h NSID */
+#define NVMET_NSID_MAX (NVME_NSID_ALL - 1)
+
+u32 nvmet_max_namespaces = NVMET_MAX_NAMESPACES;
+module_param_cb(max_namespaces, &max_namespaces_ops, &nvmet_max_namespaces, 0444);
+MODULE_PARM_DESC(max_namespaces, "Set maximum number of namespaces per subsystem (default: 1024)");
+
+static int max_namespaces_set(const char *val, const struct kernel_param *kp) {
+	int ret;
+	u32 n;
+
+	ret = kstrtouint(val, 10, &n);
+	if (ret != 0)
+		return -EINVAL;
+	if (n == 0 || n >= NVMET_NSID_MAX)
+		return -EINVAL;
+
+	return param_set_uint(val, kp);
+}
+
 struct workqueue_struct *buffered_io_wq;
 struct workqueue_struct *zbd_wq;
 static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
@@ -562,7 +588,7 @@ int nvmet_ns_enable(struct nvmet_ns *ns)
 		goto out_unlock;
 
 	ret = -EMFILE;
-	if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
+	if (subsys->nr_namespaces == nvmet_max_namespaces)
 		goto out_unlock;
 
 	ret = nvmet_bdev_ns_enable(ns);
drivers/nvme/target/nvmet.h +2 -0
index af193423c10b..ed4873963cc3 100644
@@ -506,6 +506,8 @@ void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
  */
 #define NVMET_MAX_NAMESPACES	1024
 
+extern u32 nvmet_max_namespaces;
+
 /*
  * 0 is not a valid ANA group ID, so we start numbering at 1.
  *
Signature · 1 line
2.35.1
#1 lore raw
Quoted 98 lines · Aleksandr Dyadyushkin

From: Aleksandr Dyadyushkin <[email protected]>

Allow the end user (i.e. the administrator) to set maximum number of namespaces (and thus NN and MNAN implicitly) via module loading-time parameter, without a need to modify and rebuild source code.

Most of all this can be helpful for mock-up configurations and possibly really sparse NSID assignments.

For backwards compatibility, default values are left as is.

While permitted values are choosen as high as possible for the current code architecture and standard limitations, it is worth noting that allocations at 'nvmet_execute_get_log_page_ana' might yield a failure in runtime on systems with high parameter value and low free memory.

---
  drivers/nvme/target/admin-cmd.c |  6 +++---
  drivers/nvme/target/core.c      | 28 +++++++++++++++++++++++++++-
  drivers/nvme/target/nvmet.h     |  2 ++
  3 files changed, 32 insertions(+), 4 deletions(-)
drivers/nvme/target/admin-cmd.c +3 -3
index 6fb24746de06..940297233986 100644
@@ -278,7 +278,7 @@ static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
  	u16 status;
 
  	status = NVME_SC_INTERNAL;
-	desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
+	desc = kmalloc(struct_size(desc, nsids, nvmet_max_namespaces),
  		       GFP_KERNEL);
  	if (!desc)
  		goto out;
@@ -428,8 +428,8 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
  	/* no enforcement soft-limit for maxcmd - pick arbitrary high value */
  	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
 
-	id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
-	id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
+	id->nn = cpu_to_le32(nvmet_max_namespaces);
+	id->mnan = cpu_to_le32(nvmet_max_namespaces);
  	id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
  			NVME_CTRL_ONCS_WRITE_ZEROES);
 
drivers/nvme/target/core.c +27 -1
index 5119c687de68..1b925b40074d 100644
@@ -15,6 +15,32 @@
 
  #include "nvmet.h"
 
+static int max_namespaces_set(const char *val, const struct kernel_param *kp);
+static const struct kernel_param_ops max_namespaces_ops = {
+	.set = max_namespaces_set,
+	.get = param_get_uint,
+};
+
+/* Account for 0h NSID */
+#define NVMET_NSID_MAX (NVME_NSID_ALL - 1)
+
+u32 nvmet_max_namespaces = NVMET_MAX_NAMESPACES;
+module_param_cb(max_namespaces, &max_namespaces_ops, &nvmet_max_namespaces, 0444);
+MODULE_PARM_DESC(max_namespaces, "Set maximum number of namespaces per subsystem (default: 1024)");
+
+static int max_namespaces_set(const char *val, const struct kernel_param *kp) {
+	int ret;
+	u32 n;
+
+	ret = kstrtouint(val, 10, &n);
+	if (ret != 0)
+		return -EINVAL;
+	if (n == 0 || n >= NVMET_NSID_MAX)
+		return -EINVAL;
+
+	return param_set_uint(val, kp);
+}
+
  struct workqueue_struct *buffered_io_wq;
  struct workqueue_struct *zbd_wq;
  static const struct nvmet_fabrics_ops *nvmet_transports[NVMF_TRTYPE_MAX];
@@ -562,7 +588,7 @@ int nvmet_ns_enable(struct nvmet_ns *ns)
  		goto out_unlock;
 
  	ret = -EMFILE;
-	if (subsys->nr_namespaces == NVMET_MAX_NAMESPACES)
+	if (subsys->nr_namespaces == nvmet_max_namespaces)
  		goto out_unlock;
 
  	ret = nvmet_bdev_ns_enable(ns);
drivers/nvme/target/nvmet.h +0 -0
index af193423c10b..ed4873963cc3 100644

drivers/nvme/target/nvmet.h void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type, · line 513

Quoted 8 lines
drivers/nvme/target/nvmet.h +2 -0
@@ -506,6 +506,8 @@ void nvmet_add_async_event(struct nvmet_ctrl *ctrl, u8 event_type,
   */
  #define NVMET_MAX_NAMESPACES	1024
 
+extern u32 nvmet_max_namespaces;
+
  /*
   * 0 is not a valid ANA group ID, so we start numbering at 1.
   *

Please make this a subsystem attribute.
That's precisely why we have configfs :-)

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                Kernel Storage Architect
[email protected]                              +49 911 74053 688
SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), Geschäftsführer: Felix Imendörffer
#2 lore raw
Quoted 11 lines
Quoted 7 lines

  #define NVMET_MAX_NAMESPACES    1024

drivers/nvme/target/nvmet.h +2 -0
+extern u32 nvmet_max_namespaces;
+

  /*
   * 0 is not a valid ANA group ID, so we start numbering at 1. *

Please make this a subsystem attribute.
That's precisely why we have configfs :-)

Please no module param for this, right place is configfs subsys attr.

-ck

#3 lore raw
Quoted 2 lines

Please no module param for this, right place is configfs subsys attr.

That makes sense, I agree but I mainly aimed to start off with a simple change(replacing macro with load-time value, unchangeable at runtime).

So if I'd add this to /sys/kernel/configfs/nvmet/subsystems/<NQN> folder as, say attr_max_namespaces,
what would you propose on:
1. Storing the value? Would it make sense to specify it directly as NN or MNAN value or the like?
2. What to do if set value is lower than the current value? Abort if there's greater number of enabled namespaces than the specified value?

Best regards,
Alex

#4 lore raw
Quoted 6 lines · Alex Talker
Quoted 2 lines

Please no module param for this, right place is configfs subsys attr.

That makes sense, I agree but I mainly aimed to start off with a simple change(replacing macro with load-time value, unchangeable at runtime).

Quoted 8 lines

So if I'd add this to /sys/kernel/configfs/nvmet/subsystems/<NQN> folder as, say attr_max_namespaces,
what would you propose on:
1. Storing the value? Would it make sense to specify it directly as NN or MNAN value or the like?
2. What to do if set value is lower than the current value? Abort if there's greater number of enabled namespaces than the specified value?

Relatively easy, and actually driven by the nvme controller model. Turns out that the values in 'identify controller' are static, and cannot be changed during the lifetime of a controller. Consequently you can only modify them if there is no active controller connected to the subsystem.
Similarly it doesn't make sense to have NN lower than the maximum ANA grpid of the enabled namespaces, so I would inhibit that. And yes, the 'attr_max_namespaces' should be set to the 'MNAN' value (as we're supporting ANA in the target).

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		        Kernel Storage Architect
[email protected]			               +49 911 74053 688
SUSE Software Solutions Germany GmbH, 90409 Nürnberg
GF: F. Imendörffer, HRB 36809 (AG Nürnberg)