Skip to contentSkip to conversation

nvmet-passthru: Limit mdts by BIO_MAX_PAGES

PATCH In review Logan Gunthorpe 2 participants 3 messages Oct 19, 2020 – Oct 20, 2020

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

PATCH v3 1/2 nvmet-passthru: Limit mdts by BIO_MAX_PAGES

nvmet_passthru_map_sg() only supports mapping a single BIO, not a chain so the effective maximum transfer should also be limitted by BIO_MAX_PAGES (presently this works out to 1MB).

For PCI passthru devices the max_sectors would typically be more limitting than BIO_MAX_PAGES, but this may not be true for all passthru devices.

Fixes c1fef73f793b ("nvmet: add passthru code to process commands")
Cc Christoph Hellwig <[email protected]>
Cc Sagi Grimberg <[email protected]>
Cc Chaitanya Kulkarni <[email protected]>
Cc Max Gurtovoy <[email protected]>

---

v3: Added PG_TO_SECTOR constant which uses SECTOR_SHIFT (per Max)

 drivers/nvme/target/passthru.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/nvme/target/passthru.c +10 -2
index dacfa7435d0b..ed6b0586173e 100644
@@ -22,11 +22,12 @@ static DEFINE_XARRAY(passthru_subsystems);
 
 static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
 {
+	const int PG_TO_SECTOR = PAGE_SHIFT - SECTOR_SHIFT;
 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
 	struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
 	u16 status = NVME_SC_SUCCESS;
 	struct nvme_id_ctrl *id;
-	u32 max_hw_sectors;
+	int max_hw_sectors;
 	int page_shift;
 
 	id = kzalloc(sizeof(*id), GFP_KERNEL);
@@ -45,9 +46,16 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
 	 * which depends on the host's memory fragementation. To solve this,
 	 * ensure mdts is limited to the pages equal to the number of segments.
 	 */
-	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
+	max_hw_sectors = min_not_zero(pctrl->max_segments << PG_TO_SECTOR,
 				      pctrl->max_hw_sectors);
 
+	/*
+	 * nvmet_passthru_map_sg is limitted to using a single bio so limit
+	 * the mdts based on BIO_MAX_PAGES as well
+	 */
+	max_hw_sectors = min_not_zero(BIO_MAX_PAGES << PG_TO_SECTOR,
+				      max_hw_sectors);
+
 	page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
 
 	id->mdts = ilog2(max_hw_sectors) + 9 - page_shift;

base-commit: bbf5c979011a099af5dc76498918ed7df445635b --
2.20.1

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#1 lore raw

PATCH v3 2/2 nvmet-passthru: Cleanup nvmet_passthru_map_sg()

Clean up some confusing elements of nvmet_passthru_map_sg() by returning early if the request is greater than the maximum bio size. This allows us to drop the sg_cnt variable.

This should not result in any functional change but makes the code clearer and more understandable. The original code allocated a truncated bio then would return EINVAL when bio_add_pc_page() filled that bio. The new code just returns EINVAL early if this would happen.

Fixes c1fef73f793b ("nvmet: add passthru code to process commands")
Cc Christoph Hellwig <[email protected]>
Cc Chaitanya Kulkarni <[email protected]>
---
 drivers/nvme/target/passthru.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/nvme/target/passthru.c +4 -3
index ed6b0586173e..0e891a286fd8 100644
@@ -188,18 +188,20 @@ static void nvmet_passthru_req_done(struct request *rq,
 
 static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
 {
-	int sg_cnt = req->sg_cnt;
 	struct scatterlist *sg;
 	int op_flags = 0;
 	struct bio *bio;
 	int i, ret;
 
+	if (req->sg_cnt > BIO_MAX_PAGES)
+		return -EINVAL;
+
 	if (req->cmd->common.opcode == nvme_cmd_flush)
 		op_flags = REQ_FUA;
 	else if (nvme_is_write(req->cmd))
 		op_flags = REQ_SYNC | REQ_IDLE;
 
-	bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
+	bio = bio_alloc(GFP_KERNEL, req->sg_cnt);
 	bio->bi_end_io = bio_put;
 	bio->bi_opf = req_op(rq) | op_flags;
 
@@ -209,7 +211,6 @@ static int nvmet_passthru_map_sg(struct nvmet_req *req, struct request *rq)
 			bio_put(bio);
 			return -EINVAL;
 		}
-		sg_cnt--;
 	}
 
 	ret = blk_rq_append_bio(rq, &bio);
Signature · 7 lines
2.20.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer
#2 lore raw
In reply to Logan Gunthorpe
Quoted 20 lines
drivers/nvme/target/passthru.c +2 -1
index dacfa7435d0b..ed6b0586173e 100644
@@ -22,11 +22,12 @@ static DEFINE_XARRAY(passthru_subsystems);
 
  static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
  {
+	const int PG_TO_SECTOR = PAGE_SHIFT - SECTOR_SHIFT;
  	struct nvmet_ctrl *ctrl = req->sq->ctrl;
  	struct nvme_ctrl *pctrl = ctrl->subsys->passthru_ctrl;
  	u16 status = NVME_SC_SUCCESS;
  	struct nvme_id_ctrl *id;
-	u32 max_hw_sectors;
+	int max_hw_sectors;
  	int page_shift;
 
  	id = kzalloc(sizeof(*id), GFP_KERNEL);

drivers/nvme/target/passthru.c static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req) · line 50

Quoted 6 lines
drivers/nvme/target/passthru.c +1 -1
@@ -45,9 +46,16 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
  	 * which depends on the host's memory fragementation. To solve this,
  	 * ensure mdts is limited to the pages equal to the number of segments.
  	 */
-	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
+	max_hw_sectors = min_not_zero(pctrl->max_segments << PG_TO_SECTOR,
  				      pctrl->max_hw_sectors);

I also liked the previous one better...

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme