Skip to contentSkip to conversation

Nvme: Fix dma_map_sg error check

PATCH New Jack Wang 1 participant 3 messages Aug 25, 2022

Subsystems (3)
lore
Share & tools
lore

Jack Wang

author
#0 lore raw

cover Nvme: Fix dma_map_sg error check

Hi, all,

While working on a bugfix on RTRS[1], I noticed there are quite a few other drivers have the same problem, due to the fact dma_map_sg return 0 on error, not like most of the cases, return negative value for error.

I "grep -A 5 dma_map_sg' in kernel tree, and audit/fix the one I feel is buggy, hence this patchset. As suggested by Christoph, this is for Nvme subsystem.

Thanks!

[1] https://lore.kernel.org/linux-rdma/[email protected]/T/#t

Jack Wang (2):
nvme-rdma: Fix error check for ib_dma_map_sg
nvme-fc: Fix the error check for dma_map_sg

 drivers/nvme/host/fc.c   | 2 +-
 drivers/nvme/host/rdma.c | 4 ++--
 drivers/nvme/target/fc.c | 5 +++++
 3 files changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/2 nvme-rdma: Fix error check for ib_dma_map_sg

ib_dma_map_sg return 0 on error.

Cc Keith Busch <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Fixes 711023071960 ("nvme-rdma: add a NVMe over Fabrics RDMA host driver")
---
 drivers/nvme/host/rdma.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/rdma.c +2 -2
index 3100643be299..d70bccbcba3e 100644
@@ -1551,7 +1551,7 @@ static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
 
 	*count = ib_dma_map_sg(ibdev, req->data_sgl.sg_table.sgl,
 			       req->data_sgl.nents, rq_dma_dir(rq));
-	if (unlikely(*count <= 0)) {
+	if (unlikely(!*count)) {
 		ret = -EIO;
 		goto out_free_table;
 	}
@@ -1574,7 +1574,7 @@ static int nvme_rdma_dma_map_req(struct ib_device *ibdev, struct request *rq,
 					  req->metadata_sgl->sg_table.sgl,
 					  req->metadata_sgl->nents,
 					  rq_dma_dir(rq));
-		if (unlikely(*pi_count <= 0)) {
+		if (unlikely(!*pi_count)) {
 			ret = -EIO;
 			goto out_free_pi_table;
 		}
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 2/2 nvme-fc: Fix the error check for dma_map_sg

In reply to Jack Wang

dma_map_sg return 0 on error, add missing check.

Cc James Smart <[email protected]>
Cc Keith Busch <[email protected]>
Cc Jens Axboe <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Fixes e399441de911 ("nvme-fabrics: Add host support for FC transport")
---
 drivers/nvme/host/fc.c   | 2 +-
 drivers/nvme/target/fc.c | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
drivers/nvme/host/fc.c +1 -1
index 127abaf9ba5d..95050f2e0a06 100644
@@ -2616,7 +2616,7 @@ nvme_fc_map_data(struct nvme_fc_ctrl *ctrl, struct request *rq,
 	WARN_ON(op->nents > blk_rq_nr_phys_segments(rq));
 	freq->sg_cnt = fc_dma_map_sg(ctrl->lport->dev, freq->sg_table.sgl,
 				op->nents, rq_dma_dir(rq));
-	if (unlikely(freq->sg_cnt <= 0)) {
+	if (unlikely(!freq->sg_cnt)) {
 		sg_free_table_chained(&freq->sg_table, NVME_INLINE_SG_CNT);
 		freq->sg_cnt = 0;
 		return -EFAULT;
drivers/nvme/target/fc.c +5 -0
index ab2627e17bb9..3749ca28860b 100644
@@ -2089,6 +2089,11 @@ nvmet_fc_alloc_tgt_pgs(struct nvmet_fc_fcp_iod *fod)
 				((fod->io_dir == NVMET_FCP_WRITE) ?
 					DMA_FROM_DEVICE : DMA_TO_DEVICE));
 				/* note: write from initiator perspective */
+	if (!fod->data_sg_cnt) {
+		sgl_free(fod->data_sg);
+		fod->data_sg = NULL;
+		goto out;
+	}
 	fod->next_sg = fod->data_sg;
 
 	return 0;
Signature · 1 line
2.34.1