Skip to contentSkip to conversation

nvme: validate maxcmd before use it

PATCH In review Guixin Liu 4 participants 4 messages Dec 15, 2023 – Dec 18, 2023

Subsystems (2)
lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH 1/1 nvme: validate maxcmd before use it

According to the NVMe specification, if the 'maximum outstanding commands' (maxcmd) field is not utilized, it must be set to 0h. When a host connects to a target that does not employ the maxcmd field, the queue size (sqsize) gets set to an excessively large value due to the calculation (u16)(0 - 1) = 65535.
As a result, such a misconfiguration leads to the failure of the connection establishment.

---
 drivers/nvme/host/fc.c   | 3 ++-
 drivers/nvme/host/rdma.c | 3 ++-
 drivers/nvme/host/tcp.c  | 3 ++-
 3 files changed, 6 insertions(+), 3 deletions(-)
drivers/nvme/host/fc.c +2 -1
index fb22976..8ddbeef 100644
@@ -3156,7 +3156,8 @@ static void nvme_fc_map_queues(struct blk_mq_tag_set *set)
 		goto out_stop_keep_alive;
 	}
 
-	if (opts->queue_size > ctrl->ctrl.maxcmd) {
+	if (ctrl->ctrl.maxcmd &&
+	    opts->queue_size > ctrl->ctrl.maxcmd) {
 		/* warn if maxcmd is lower than queue_size */
 		dev_warn(ctrl->ctrl.device,
 			"queue_size %zu > ctrl maxcmd %u, reducing "
drivers/nvme/host/rdma.c +2 -1
index 81e2621..597df7c 100644
@@ -1037,7 +1037,8 @@ static int nvme_rdma_setup_ctrl(struct nvme_rdma_ctrl *ctrl, bool new)
 		ctrl->ctrl.sqsize = NVME_RDMA_MAX_QUEUE_SIZE - 1;
 	}
 
-	if (ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
+	if (ctrl->ctrl.maxcmd &&
+	    ctrl->ctrl.sqsize + 1 > ctrl->ctrl.maxcmd) {
 		dev_warn(ctrl->ctrl.device,
 			"sqsize %u > ctrl maxcmd %u, clamping down\n",
 			ctrl->ctrl.sqsize + 1, ctrl->ctrl.maxcmd);
drivers/nvme/host/tcp.c +2 -1
index 08805f0..12b00ad 100644
@@ -2197,7 +2197,8 @@ static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
 			"queue_size %zu > ctrl sqsize %u, clamping down\n",
 			opts->queue_size, ctrl->sqsize + 1);
 
-	if (ctrl->sqsize + 1 > ctrl->maxcmd) {
+	if (ctrl->maxcmd &&
+	    ctrl->sqsize + 1 > ctrl->maxcmd) {
 		dev_warn(ctrl->device,
 			"sqsize %u > ctrl maxcmd %u, clamping down\n",
 			ctrl->sqsize + 1, ctrl->maxcmd);
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Guixin Liu

According to the NVMe specification, if the 'maximum outstanding commands' (maxcmd) field is not utilized, it must be set to 0h. When a host connects to a target that does not employ the maxcmd field, the queue size (sqsize) gets set to an excessively large value due to the calculation (u16)(0 - 1) = 65535.
As a result, such a misconfiguration leads to the failure of the connection establishment.

Quoted 7 lines
---
  drivers/nvme/host/fc.c   | 3 ++-
  drivers/nvme/host/rdma.c | 3 ++-
  drivers/nvme/host/tcp.c  | 3 ++-
  3 files changed, 6 insertions(+), 3 deletions(-)

Cheers,

Hannes

#2 lore raw
In reply to Guixin Liu
Quoted 7 lines · Guixin Liu

According to the NVMe specification, if the 'maximum outstanding commands' (maxcmd) field is not utilized, it must be set to 0h. When a host connects to a target that does not employ the maxcmd field, the queue size (sqsize) gets set to an excessively large value due to the calculation (u16)(0 - 1) = 65535.
As a result, such a misconfiguration leads to the failure of the connection establishment.

MAXCMD is mandatory for Fabrics.

maintainer
#3 lore raw
Quoted 11 lines
Quoted 8 lines

According to the NVMe specification, if the 'maximum outstanding commands' (maxcmd) field is not utilized, it must be set to 0h. When a host connects to a target that does not employ the maxcmd field, the queue size (sqsize) gets set to an excessively large value due to the calculation (u16)(0 - 1) = 65535.
As a result, such a misconfiguration leads to the failure of the connection establishment.

MAXCMD is mandatory for Fabrics.

Indeed.