Skip to contentSkip to conversation

nvmet: convert from kmap to nvmet_copy_from_sgl

PATCH In review Logan Gunthorpe 5 participants 7 messages Apr 18, 2017 – Apr 20, 2017

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

PATCH 1/1 nvmet: convert from kmap to nvmet_copy_from_sgl

This is safer as it doesn't rely on the data being stored in a single page in an sgl.

It also aids our effort to start phasing out users of sg_page. See [1].

For this we kmalloc some memory, copy to it and free at the end. Note:
we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

[1] https://lwn.net/Articles/720053/

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
---
 drivers/nvme/target/fabrics-cmd.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
drivers/nvme/target/fabrics-cmd.c +25 -7
index 8bd022af..2e0ab10 100644
@@ -122,7 +122,15 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	struct nvmet_ctrl *ctrl = NULL;
 	u16 status = 0;
 
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);
+	if (!d) {
+		status = NVME_SC_INTERNAL;
+		goto complete;
+	}
+
+	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
+	if (status)
+		goto out;
 
 	/* zero out initial completion result, assign values as needed */
 	req->rsp->result.u32 = 0;
@@ -143,7 +151,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	}
 
 	status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
-			le32_to_cpu(c->kato), &ctrl);
+				  le32_to_cpu(c->kato), &ctrl);
 	if (status)
 		goto out;
 
@@ -158,7 +166,8 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
 
 out:
-	kunmap(sg_page(req->sg));
+	kfree(d);
+complete:
 	nvmet_req_complete(req, status);
 }
 
@@ -170,7 +179,15 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	u16 qid = le16_to_cpu(c->qid);
 	u16 status = 0;
 
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);
+	if (!d) {
+		status = NVME_SC_INTERNAL;
+		goto complete;
+	}
+
+	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
+	if (status)
+		goto out;
 
 	/* zero out initial completion result, assign values as needed */
 	req->rsp->result.u32 = 0;
@@ -183,8 +200,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	}
 
 	status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
-			le16_to_cpu(d->cntlid),
-			req, &ctrl);
+				     le16_to_cpu(d->cntlid),
+				     req, &ctrl);
 	if (status)
 		goto out;
 
@@ -205,7 +222,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	pr_info("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
 
 out:
-	kunmap(sg_page(req->sg));
+	kfree(d);
+complete:
 	nvmet_req_complete(req, status);
 	return;
 
Signature · 1 line
2.1.4
#1 lore raw

Hi Logan,

Quoted 23 lines · Logan Gunthorpe

This is safer as it doesn't rely on the data being stored in a single page in an sgl.

It also aids our effort to start phasing out users of sg_page. See [1].

For this we kmalloc some memory, copy to it and free at the end. Note:
we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

[1] https://lwn.net/Articles/720053/

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>
---
 drivers/nvme/target/fabrics-cmd.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
drivers/nvme/target/fabrics-cmd.c +0 -0
index 8bd022af..2e0ab10 100644

drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_admin_connect(struct nvmet_req *req) · line 125

Quoted 5 lines
drivers/nvme/target/fabrics-cmd.c +1 -1
@@ -122,7 +122,15 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	struct nvmet_ctrl *ctrl = NULL;
 	u16 status = 0;
 
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);

I'd prefer removing the dynamic allocation and use d on the stack to simplify the code.
Any thoughts ?

Quoted 31 lines
drivers/nvme/target/fabrics-cmd.c +11 -2
+	if (!d) {
+		status = NVME_SC_INTERNAL;
+		goto complete;
+	}
+
+	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
+	if (status)
+		goto out;
 
 	/* zero out initial completion result, assign values as needed */
 	req->rsp->result.u32 = 0;
@@ -143,7 +151,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	}
 
 	status = nvmet_alloc_ctrl(d->subsysnqn, d->hostnqn, req,
-			le32_to_cpu(c->kato), &ctrl);
+				  le32_to_cpu(c->kato), &ctrl);
 	if (status)
 		goto out;
 
@@ -158,7 +166,8 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	req->rsp->result.u16 = cpu_to_le16(ctrl->cntlid);
 
 out:
-	kunmap(sg_page(req->sg));
+	kfree(d);
+complete:
 	nvmet_req_complete(req, status);
 }
 

drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_io_connect(struct nvmet_req *req) · line 182

Quoted 5 lines
drivers/nvme/target/fabrics-cmd.c +1 -1
@@ -170,7 +179,15 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	u16 qid = le16_to_cpu(c->qid);
 	u16 status = 0;
 
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);

here too.

Quoted 33 lines
drivers/nvme/target/fabrics-cmd.c +12 -3
+	if (!d) {
+		status = NVME_SC_INTERNAL;
+		goto complete;
+	}
+
+	status = nvmet_copy_from_sgl(req, 0, d, sizeof(*d));
+	if (status)
+		goto out;
 
 	/* zero out initial completion result, assign values as needed */
 	req->rsp->result.u32 = 0;
@@ -183,8 +200,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	}
 
 	status = nvmet_ctrl_find_get(d->subsysnqn, d->hostnqn,
-			le16_to_cpu(d->cntlid),
-			req, &ctrl);
+				     le16_to_cpu(d->cntlid),
+				     req, &ctrl);
 	if (status)
 		goto out;
 
@@ -205,7 +222,8 @@ static void nvmet_execute_io_connect(struct nvmet_req *req)
 	pr_info("adding queue %d to ctrl %d.\n", qid, ctrl->cntlid);
 
 out:
-	kunmap(sg_page(req->sg));
+	kfree(d);
+complete:
 	nvmet_req_complete(req, status);
 	return;
 
#2 lore raw
Quoted 29 lines · Max Gurtovoy

Hi Logan,

Quoted 26 lines · Logan Gunthorpe

This is safer as it doesn't rely on the data being stored in a single page in an sgl.

It also aids our effort to start phasing out users of sg_page. See [1].

For this we kmalloc some memory, copy to it and free at the end. Note:
we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

[1] https://lwn.net/Articles/720053/

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>

---
drivers/nvme/target/fabrics-cmd.c | 32 +++++++++++++++++++++++++-------

1 file changed, 25 insertions(+), 7 deletions(-)
drivers/nvme/target/fabrics-cmd.c +0 -0
index 8bd022af..2e0ab10 100644
@@ -122,7 +122,15 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	struct nvmet_ctrl *ctrl = NULL;
 	u16 status = 0;
 
Quoted 6 lines

drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_admin_connect(struct nvmet_req *req) · line 125

Quoted 2 lines
drivers/nvme/target/fabrics-cmd.c +1 -1
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);

I'd prefer removing the dynamic allocation and use d on the stack to simplify the code.
Any thoughts ?

Hi Max,

Pasting from above:

Quoted 2 lines
Quoted 2 lines

we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

Signature · 6 lines
Johannes Thumshirn                                          Storage
jthumshirn at suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 N?rnberg
GF: Felix Imend?rffer, Jane Smithard, Graham Norton
HRB 21284 (AG N?rnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by
#3 lore raw
Quoted 37 lines · Johannes Thumshirn
Quoted 29 lines · Max Gurtovoy

Hi Logan,

Quoted 26 lines · Logan Gunthorpe

This is safer as it doesn't rely on the data being stored in a single page in an sgl.

It also aids our effort to start phasing out users of sg_page. See [1].

For this we kmalloc some memory, copy to it and free at the end. Note:
we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

[1] https://lwn.net/Articles/720053/

Signed-off-by Logan Gunthorpe <logang at deltatee.com>
Cc Christoph Hellwig <hch at lst.de>
Cc Sagi Grimberg <sagi at grimberg.me>

---
drivers/nvme/target/fabrics-cmd.c | 32 +++++++++++++++++++++++++-------

1 file changed, 25 insertions(+), 7 deletions(-)
drivers/nvme/target/fabrics-cmd.c +0 -0
index 8bd022af..2e0ab10 100644
@@ -122,7 +122,15 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
 	struct nvmet_ctrl *ctrl = NULL;
 	u16 status = 0;
 
Quoted 6 lines

drivers/nvme/target/fabrics-cmd.c static void nvmet_execute_admin_connect(struct nvmet_req *req) · line 125

Quoted 2 lines
drivers/nvme/target/fabrics-cmd.c +1 -1
-	d = kmap(sg_page(req->sg)) + req->sg->offset;
+	d = kmalloc(sizeof(*d), GFP_KERNEL);

I'd prefer removing the dynamic allocation and use d on the stack to simplify the code.
Any thoughts ?

Quoted 6 lines

Hi Max,

Pasting from above:

Quoted 2 lines
Quoted 2 lines

we can't allocate this memory on the stack as the kbuild test robot reports some frame size overflows on i386.

Thanks Johannes, I missed that comment.

Looks good,

Reviewed-by Max Gurtovoy <maxg at mellanox.com>
Quoted 2 lines
#4 lore raw

Thanks Max!

Quoted 3 lines · Max Gurtovoy

Looks good,

Reviewed-by Max Gurtovoy <maxg at mellanox.com>