Skip to contentSkip to conversation

nvmet: use kzalloc instead of ZERO_PAGE in nvme_execute_identify_ns_nvm()

PATCH Applied Nilay Shroff 5 participants 5 messages Nov 24, 2024 – Dec 2, 2024

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

PATCH v2 1/1 nvmet: use kzalloc instead of ZERO_PAGE in nvme_execute_identify_ns_nvm()

The nvme_execute_identify_ns_nvm function uses ZERO_PAGE for copying SG list with all zeros. As ZERO_PAGE would not necessarily return the virtual-address of the zero page, we need to first convert the page address to kernel virtual-address and then use it as source address for copying the data to SG list with all zeros. Using return address of ZERO_PAGE(0) as source address for copying data to SG list would fill the target buffer with random/garbage value and causes the undesired side effect.

As other identify implemenations uses kzalloc for allocating a zero filled buffer, we decided use kzalloc for allocating a zero filled buffer in nvme_execute_identify_ns_nvm function and then use this buffer for copying all zeros to SG list buffers. So esentially, we now avoid using ZERO_PAGE.

---
Changes from v1:
- Use kzalloc instead of ZERO_PAGE() for allocating zero filled buffer (Christoph Hellwing, Keith Busch)

 drivers/nvme/target/admin-cmd.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +7 -2
index 934b401fbc2f..f92c5cb1a25b 100644
@@ -901,13 +901,18 @@ static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
 {
 	u16 status;
+	struct nvme_id_ns_nvm *id;
 
 	status = nvmet_req_find_ns(req);
 	if (status)
 		goto out;
 
-	status = nvmet_copy_to_sgl(req, 0, ZERO_PAGE(0),
-				   NVME_IDENTIFY_DATA_SIZE);
+	id = kzalloc(sizeof(*id), GFP_KERNEL);
+	if (!id) {
+		status = NVME_SC_INTERNAL;
+		goto out;
+	}
+	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
 out:
 	nvmet_req_complete(req, status);
 }
Signature · 1 line
2.45.2
Tested-by on v2 1/1
#2 lore raw
In reply to Nilay Shroff

Thanks for the fix.

Quoted 56 lines · Nilay Shroff

The nvme_execute_identify_ns_nvm function uses ZERO_PAGE for copying SG list with all zeros. As ZERO_PAGE would not necessarily return the virtual-address of the zero page, we need to first convert the page address to kernel virtual-address and then use it as source address for copying the data to SG list with all zeros. Using return address of ZERO_PAGE(0) as source address for copying data to SG list would fill the target buffer with random/garbage value and causes the undesired side effect.

As other identify implemenations uses kzalloc for allocating a zero filled buffer, we decided use kzalloc for allocating a zero filled buffer in nvme_execute_identify_ns_nvm function and then use this buffer for copying all zeros to SG list buffers. So esentially, we now avoid using ZERO_PAGE.

---
Changes from v1:
- Use kzalloc instead of ZERO_PAGE() for allocating zero filled buffer (Christoph Hellwing, Keith Busch)

 drivers/nvme/target/admin-cmd.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/nvme/target/admin-cmd.c +7 -3
index 934b401fbc2f..f92c5cb1a25b 100644
@@ -901,13 +901,18 @@ static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
 {
        u16 status;
+       struct nvme_id_ns_nvm *id;
 
        status = nvmet_req_find_ns(req);
        if (status)
                goto out;
 
-       status = nvmet_copy_to_sgl(req, 0, ZERO_PAGE(0),
-                                  NVME_IDENTIFY_DATA_SIZE);
+       id = kzalloc(sizeof(*id), GFP_KERNEL);
+       if (!id) {
+               status = NVME_SC_INTERNAL;
+               goto out;
+       }
+       status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
 out:
        nvmet_req_complete(req, status);
 }
--

2.45.2

Signature · 2 lines
Best Regards,
  Yi Zhang
Reviewed-by on v2 1/1
#3 lore raw
In reply to Nilay Shroff
Quoted 15 lines · Nilay Shroff

The nvme_execute_identify_ns_nvm function uses ZERO_PAGE for copying SG list with all zeros. As ZERO_PAGE would not necessarily return the virtual-address of the zero page, we need to first convert the page address to kernel virtual-address and then use it as source address for copying the data to SG list with all zeros. Using return address of ZERO_PAGE(0) as source address for copying data to SG list would fill the target buffer with random/garbage value and causes the undesired side effect.

As other identify implemenations uses kzalloc for allocating a zero filled buffer, we decided use kzalloc for allocating a zero filled buffer in nvme_execute_identify_ns_nvm function and then use this buffer for copying all zeros to SG list buffers. So esentially, we now avoid using ZERO_PAGE.

Quoted 4 lines

Looks good.

-ck

maintainer Applied
#4 lore raw
In reply to Nilay Shroff
Quoted 9 lines · Nilay Shroff

The nvme_execute_identify_ns_nvm function uses ZERO_PAGE for copying SG list with all zeros. As ZERO_PAGE would not necessarily return the virtual-address of the zero page, we need to first convert the page address to kernel virtual-address and then use it as source address for copying the data to SG list with all zeros. Using return address of ZERO_PAGE(0) as source address for copying data to SG list would fill the target buffer with random/garbage value and causes the undesired side effect.

Quoted 5 lines

As other identify implemenations uses kzalloc for allocating a zero filled buffer, we decided use kzalloc for allocating a zero filled buffer in nvme_execute_identify_ns_nvm function and then use this buffer for copying all zeros to SG list buffers. So esentially, we now avoid using ZERO_PAGE.

Thanks, applied to nvme-6.13.