nvme: fix handling mapping failure
Subsystems (2)
PATCH 1/1 nvme: fix handling mapping failure
This patch ensures that when nvme_map_data() fails to map the addresses in a scatter/gather list:
* The addresses are not incorrectly unmapped. The underlying scatter/gather code unmaps the addresses after detecting a failure. Thus, unmapping them again in the driver is a bug.
* The DMA pool allocations are not deallocated when they were never allocated.
The bug that motivated this patch was the following sequence, which occurred within the NVMe driver, with the kernel flag swiotlb=force.
* NVMe driver calls dma_direct_map_sg()
* dma_direct_map_sg() fails part way through the scatter gather/list
* dma_direct_map_sg() calls dma_direct_unmap_sg() to unmap any entries succeeded.
* NVMe driver calls dma_direct_unmap_sg(), redundantly, leading to a double unmap, which is a bug.
Before this patch, I observed intermittent application- and VM-level failures when running a benchmark, fio, in an AMD SEV guest. This patch resolves the failures.
--- drivers/nvme/host/pci.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/nvme/host/pci.c +12 -6
@@ -543,11 +543,14 @@ static void nvme_unmap_data(struct nvme_dev *dev, struct request *req)WARN_ON_ONCE (!iod->nents); -if (is_pci_p2pdma_page (sg_page (iod->sg))) -pci_p2pdma_unmap_sg (dev->dev, iod->sg, iod->nents, -rq_dma_dir (req)); -else -dma_unmap_sg (dev->dev, iod->sg, iod->nents,rq_dma_dir (req)); +if (!dma_mapping_error (dev->dev, iod->first_dma)) { +if (is_pci_p2pdma_page (sg_page (iod->sg))) +pci_p2pdma_unmap_sg (dev->dev, iod->sg, iod->nents, +rq_dma_dir (req)); +else +dma_unmap_sg (dev->dev, iod->sg, iod->nents, +rq_dma_dir (req)); + }if (iod->npages ==0 )
@@ -836,8 +839,11 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,else nr_mapped =dma_map_sg_attrs (dev->dev, iod->sg, iod->nents,rq_dma_dir (req),DMA_ATTR_NO_WARN ); -if (!nr_mapped) +if (!nr_mapped) { + iod->first_dma =DMA_MAPPING_ERROR ; + iod->npages =-1 ; goto out; + } iod->use_sgl =nvme_pci_use_sgls (dev, req);if (iod->use_sgl)
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Christoph Hellwig
maintainerQuoted 19 lines · Marc Orr
This patch ensures that when
nvme_map_data()fails to map the addresses in a scatter/gather list:* The addresses are not incorrectly unmapped. The underlying scatter/gather code unmaps the addresses after detecting a failure. Thus, unmapping them again in the driver is a bug.
* The DMA pool allocations are not deallocated when they were never allocated.The bug that motivated this patch was the following sequence, which occurred within the NVMe driver, with the kernel flag
swiotlb=force.* NVMe driver calls
dma_direct_map_sg()
*dma_direct_map_sg()fails part way through the scatter gather/list
*dma_direct_map_sg()callsdma_direct_unmap_sg()to unmap any entries succeeded.
* NVMe driver callsdma_direct_unmap_sg(), redundantly, leading to a double unmap, which is a bug.
Quoted 3 lines
Before this patch, I observed intermittent application- and VM-level failures when running a benchmark, fio, in an AMD SEV guest. This patch resolves the failures.
I think the right way to fix this is to just do a proper unwind insted of calling a catchall function. Can you try this patch?
drivers/nvme/host/pci.c +13 -4
@@ -842,7 +842,7 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,sg_init_table (iod->sg,blk_rq_nr_phys_segments (req)); iod->nents =blk_rq_map_sg (req->q, req, iod->sg);if (!iod->nents) - goto out; + goto out_free_sg;if (is_pci_p2pdma_page (sg_page (iod->sg))) nr_mapped = pci_p2pdma_map_sg_attrs(dev->dev, iod->sg,
@@ -851,16 +851,25 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, nr_mapped =dma_map_sg_attrs (dev->dev, iod->sg, iod->nents,rq_dma_dir (req),DMA_ATTR_NO_WARN );if (!nr_mapped) - goto out; + goto out_free_sg; iod->use_sgl =nvme_pci_use_sgls (dev, req);if (iod->use_sgl) ret =nvme_pci_setup_sgls (dev, req, &cmnd->rw, nr_mapped);else ret =nvme_pci_setup_prps (dev, req, &cmnd->rw); -out:if (ret !=BLK_STS_OK ) -nvme_unmap_data (dev, req); + goto out_dma_unmap; +return BLK_STS_OK ; + +out_dma_unmap: +if (is_pci_p2pdma_page (sg_page (iod->sg))) +pci_p2pdma_unmap_sg (dev->dev, iod->sg, iod->nents, +rq_dma_dir (req)); +else +dma_unmap_sg (dev->dev, iod->sg, iod->nents,rq_dma_dir (req)); +out_free_sg: +mempool_free (iod->sg, dev->iod_mempool);return ret; }
Quoted 21 lines · Christoph Hellwig
Quoted 19 lines · Marc Orr
This patch ensures that when
nvme_map_data()fails to map the addresses in a scatter/gather list:* The addresses are not incorrectly unmapped. The underlying scatter/gather code unmaps the addresses after detecting a failure. Thus, unmapping them again in the driver is a bug.
* The DMA pool allocations are not deallocated when they were never allocated.The bug that motivated this patch was the following sequence, which occurred within the NVMe driver, with the kernel flag
swiotlb=force.* NVMe driver calls
dma_direct_map_sg()
*dma_direct_map_sg()fails part way through the scatter gather/list
*dma_direct_map_sg()callsdma_direct_unmap_sg()to unmap any entries succeeded.
* NVMe driver callsdma_direct_unmap_sg(), redundantly, leading to a double unmap, which is a bug.
Quoted 6 lines
Quoted 3 lines
Before this patch, I observed intermittent application- and VM-level failures when running a benchmark, fio, in an AMD SEV guest. This patch resolves the failures.
I think the right way to fix this is to just do a proper unwind insted of calling a catchall function. Can you try this patch?
Done. It works great, thanks! Shall I send out a v2 with what you've proposed?
Quoted 27 lines
drivers/nvme/host/pci.c +2
-3@@ -842,7 +842,7 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req,sg_init_table (iod->sg,blk_rq_nr_phys_segments (req)); iod->nents =blk_rq_map_sg (req->q, req, iod->sg);if (!iod->nents) - goto out; + goto out_free_sg;if (is_pci_p2pdma_page (sg_page (iod->sg))) nr_mapped = pci_p2pdma_map_sg_attrs(dev->dev, iod->sg,@@ -851,16 +851,25 @@ static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, nr_mapped =dma_map_sg_attrs (dev->dev, iod->sg, iod->nents,rq_dma_dir (req),DMA_ATTR_NO_WARN );if (!nr_mapped) - goto out; + goto out_free_sg; iod->use_sgl =nvme_pci_use_sgls (dev, req);if (iod->use_sgl) ret =nvme_pci_setup_sgls (dev, req, &cmnd->rw, nr_mapped);else ret =nvme_pci_setup_prps (dev, req, &cmnd->rw); -out:if (ret !=BLK_STS_OK )
drivers/nvme/host/pci.c static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, · line 870
Quoted 10 lines
drivers/nvme/host/pci.c +9
-1-nvme_unmap_data (dev, req); + goto out_dma_unmap; +return BLK_STS_OK ; + +out_dma_unmap: +if (is_pci_p2pdma_page (sg_page (iod->sg))) +pci_p2pdma_unmap_sg (dev->dev, iod->sg, iod->nents, +rq_dma_dir (req)); +else +dma_unmap_sg (dev->dev, iod->sg, iod->nents,rq_dma_dir (req));
Do you think it's worth hoisting this sg unmap snippet into a helper that can be called from both here, as well as nvme_unmap_data()?
drivers/nvme/host/pci.c static blk_status_t nvme_map_data(struct nvme_dev *dev, struct request *req, · line 875
Quoted 5 lines
drivers/nvme/host/pci.c +2
-0+out_free_sg: +mempool_free (iod->sg, dev->iod_mempool);return ret; }
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme