Skip to contentSkip to conversation

nvme-pci: add missing condition check for existence of mapped data

PATCH Applied Leon Romanovsky 4 participants 8 messages Jul 24, 2024 – Jul 30, 2024 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-pci: add missing condition check for existence of mapped data

From: Leon Romanovsky <[email protected]>

nvme_map_data() is called when request has physical segments, hence the nvme_unmap_data() should have same condition to avoid dereference.

Fixes 4aedb705437f ("nvme-pci: split metadata handling from nvme_map_data / nvme_unmap_data")
---
 drivers/nvme/host/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/host/pci.c +2 -1
index 102a9fb0c65f..cdc0b25091e9 100644
@@ -863,7 +863,8 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req)
 	nvme_start_request(req);
 	return BLK_STS_OK;
 out_unmap_data:
-	nvme_unmap_data(dev, req);
+	if (blk_rq_nr_phys_segments(req))
+		nvme_unmap_data(dev, req);
 out_free_cmd:
 	nvme_cleanup_cmd(req);
 	return ret;
Signature · 1 line
2.45.2
maintainer Applied
#3 lore raw
In reply to Leon Romanovsky
Quoted 4 lines · Leon Romanovsky

From: Leon Romanovsky <[email protected]>

nvme_map_data() is called when request has physical segments, hence the nvme_unmap_data() should have same condition to avoid dereference.

Thanks, applied to nvme-6.11.

maintainer
#4 lore raw
In reply to Leon Romanovsky

drivers/nvme/host/pci.c static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) · line 867

Quoted 11 lines · Leon Romanovsky
drivers/nvme/host/pci.c +2 -1
index 102a9fb0c65f..cdc0b25091e9 100644
@@ -863,7 +863,8 @@ static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req)
 	nvme_start_request(req);
 	return BLK_STS_OK;
 out_unmap_data:
-	nvme_unmap_data(dev, req);
+	if (blk_rq_nr_phys_segments(req))
+		nvme_unmap_data(dev, req);

This is already applied, but it is kind of strange. We get here only if metadata mapping fails. Is there actually a command that has metadata without data?

#5 lore raw
Quoted 6 lines · Keith Busch

drivers/nvme/host/pci.c static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) · line 867

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+	if (blk_rq_nr_phys_segments(req))
+		nvme_unmap_data(dev, req);

This is already applied, but it is kind of strange. We get here only if metadata mapping fails. Is there actually a command that has metadata without data?

Well, passthrough can always set metadata to map without data even if there is no NVMe defined command that works that way, so we should handle the error.

But I suspect this is due to Leon's dma-mapping work, and it probably points to a bug in that :)

maintainer
#6 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 6 lines · Keith Busch

drivers/nvme/host/pci.c static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) · line 867

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+	if (blk_rq_nr_phys_segments(req))
+		nvme_unmap_data(dev, req);

This is already applied, but it is kind of strange. We get here only if metadata mapping fails. Is there actually a command that has metadata without data?

Well, passthrough can always set metadata to map without data even if there is no NVMe defined command that works that way, so we should handle the error.

That's what I initially thought too, but nvme passthrough maps metadata only if there's also user data. It doesn't look like you can even build a request to have metadata if it doesn't have user data: where does the bio come from in that case?

And generic block stack maps it only for READ or WRITE commands, which must have payloads too, so I didn't find a path to reach this condition.

Not a big deal, the patch is fine, but I was wondering if we need to change something else to allow the conditions it proposes to fix.

#7 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Keith Busch

drivers/nvme/host/pci.c static blk_status_t nvme_prep_rq(struct nvme_dev *dev, struct request *req) · line 867

Quoted 2 lines
drivers/nvme/host/pci.c +2 -0
+	if (blk_rq_nr_phys_segments(req))
+		nvme_unmap_data(dev, req);

This is already applied, but it is kind of strange. We get here only if metadata mapping fails. Is there actually a command that has metadata without data?

Quoted 6 lines

Well, passthrough can always set metadata to map without data even if there is no NVMe defined command that works that way, so we should handle the error.

But I suspect this is due to Leon's dma-mapping work, and it probably points to a bug in that :)

Yeah, something like that. I had a bug in my code and saw this asymmetry while reviewed all paths which lead to nvme_unmap_data().

Thanks