Skip to content

Commit 5b2322e

Browse files
lsgunthbjorn-helgaas
authored andcommitted
nvmet: Introduce helper functions to allocate and free request SGLs
Add helpers to allocate and free the SGL in a struct nvmet_req: int nvmet_req_alloc_sgl(struct nvmet_req *req) void nvmet_req_free_sgl(struct nvmet_req *req) This will be expanded in a future patch to implement peer-to-peer memory DMAs and should be common with all target drivers. The new helpers are used in nvmet-rdma. Seeing we use req.transfer_len as the length of the SGL it is set earlier and cleared on any error. It also seems to be unnecessary to accumulate the length as the map_sgl functions should only ever be called once per request. Signed-off-by: Logan Gunthorpe <logang@deltatee.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Acked-by: Sagi Grimberg <sagi@grimberg.me>
1 parent e0596ab commit 5b2322e

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

drivers/nvme/target/core.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,24 @@ void nvmet_req_execute(struct nvmet_req *req)
725725
}
726726
EXPORT_SYMBOL_GPL(nvmet_req_execute);
727727

728+
int nvmet_req_alloc_sgl(struct nvmet_req *req)
729+
{
730+
req->sg = sgl_alloc(req->transfer_len, GFP_KERNEL, &req->sg_cnt);
731+
if (!req->sg)
732+
return -ENOMEM;
733+
734+
return 0;
735+
}
736+
EXPORT_SYMBOL_GPL(nvmet_req_alloc_sgl);
737+
738+
void nvmet_req_free_sgl(struct nvmet_req *req)
739+
{
740+
sgl_free(req->sg);
741+
req->sg = NULL;
742+
req->sg_cnt = 0;
743+
}
744+
EXPORT_SYMBOL_GPL(nvmet_req_free_sgl);
745+
728746
static inline bool nvmet_cc_en(u32 cc)
729747
{
730748
return (cc >> NVME_CC_EN_SHIFT) & 0x1;

drivers/nvme/target/nvmet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ bool nvmet_req_init(struct nvmet_req *req, struct nvmet_cq *cq,
336336
void nvmet_req_uninit(struct nvmet_req *req);
337337
void nvmet_req_execute(struct nvmet_req *req);
338338
void nvmet_req_complete(struct nvmet_req *req, u16 status);
339+
int nvmet_req_alloc_sgl(struct nvmet_req *req);
340+
void nvmet_req_free_sgl(struct nvmet_req *req);
339341

340342
void nvmet_cq_setup(struct nvmet_ctrl *ctrl, struct nvmet_cq *cq, u16 qid,
341343
u16 size);

drivers/nvme/target/rdma.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ static void nvmet_rdma_release_rsp(struct nvmet_rdma_rsp *rsp)
503503
}
504504

505505
if (rsp->req.sg != rsp->cmd->inline_sg)
506-
sgl_free(rsp->req.sg);
506+
nvmet_req_free_sgl(&rsp->req);
507507

508508
if (unlikely(!list_empty_careful(&queue->rsp_wr_wait_list)))
509509
nvmet_rdma_process_wr_wait_list(queue);
@@ -652,24 +652,24 @@ static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
652652
{
653653
struct rdma_cm_id *cm_id = rsp->queue->cm_id;
654654
u64 addr = le64_to_cpu(sgl->addr);
655-
u32 len = get_unaligned_le24(sgl->length);
656655
u32 key = get_unaligned_le32(sgl->key);
657656
int ret;
658657

658+
rsp->req.transfer_len = get_unaligned_le24(sgl->length);
659+
659660
/* no data command? */
660-
if (!len)
661+
if (!rsp->req.transfer_len)
661662
return 0;
662663

663-
rsp->req.sg = sgl_alloc(len, GFP_KERNEL, &rsp->req.sg_cnt);
664-
if (!rsp->req.sg)
665-
return NVME_SC_INTERNAL;
664+
ret = nvmet_req_alloc_sgl(&rsp->req);
665+
if (ret < 0)
666+
goto error_out;
666667

667668
ret = rdma_rw_ctx_init(&rsp->rw, cm_id->qp, cm_id->port_num,
668669
rsp->req.sg, rsp->req.sg_cnt, 0, addr, key,
669670
nvmet_data_dir(&rsp->req));
670671
if (ret < 0)
671-
return NVME_SC_INTERNAL;
672-
rsp->req.transfer_len += len;
672+
goto error_out;
673673
rsp->n_rdma += ret;
674674

675675
if (invalidate) {
@@ -678,6 +678,10 @@ static u16 nvmet_rdma_map_sgl_keyed(struct nvmet_rdma_rsp *rsp,
678678
}
679679

680680
return 0;
681+
682+
error_out:
683+
rsp->req.transfer_len = 0;
684+
return NVME_SC_INTERNAL;
681685
}
682686

683687
static u16 nvmet_rdma_map_sgl(struct nvmet_rdma_rsp *rsp)

0 commit comments

Comments
 (0)