Linux-NVME Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES
@ 2020-10-16 22:19 Logan Gunthorpe
  2020-10-16 22:19 ` [PATCH v2 2/2] nvmet-passthru: Cleanup nvmet_passthru_map_sg() Logan Gunthorpe
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Logan Gunthorpe @ 2020-10-16 22:19 UTC (permalink / raw)
  To: linux-nvme
  Cc: Logan Gunthorpe, Christoph Hellwig, Chaitanya Kulkarni, Sagi Grimberg

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")
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Sagi Grimberg <sagi@grimberg.me>
Cc: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
---
 drivers/nvme/target/passthru.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
index dacfa7435d0b..1ab88df3310f 100644
--- a/drivers/nvme/target/passthru.c
+++ b/drivers/nvme/target/passthru.c
@@ -26,7 +26,7 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
 	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);
@@ -48,6 +48,13 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
 	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
 				      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 << (PAGE_SHIFT - 9),
+				      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
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/2] nvmet-passthru: Cleanup nvmet_passthru_map_sg()
  2020-10-16 22:19 [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Logan Gunthorpe
@ 2020-10-16 22:19 ` Logan Gunthorpe
  2020-10-19 11:20 ` [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Max Gurtovoy
  2020-10-22 13:29 ` Christoph Hellwig
  2 siblings, 0 replies; 6+ messages in thread
From: Logan Gunthorpe @ 2020-10-16 22:19 UTC (permalink / raw)
  To: linux-nvme
  Cc: Christoph Hellwig, Logan Gunthorpe, Sagi Grimberg,
	Chaitanya Kulkarni, Douglas Gilbert

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")
Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
Suggested-by: Douglas Gilbert <dgilbert@interlog.com>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
---
 drivers/nvme/target/passthru.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
index 1ab88df3310f..8ea9b173f285 100644
--- a/drivers/nvme/target/passthru.c
+++ b/drivers/nvme/target/passthru.c
@@ -187,18 +187,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;
 
@@ -208,7 +210,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);
-- 
2.20.1


_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES
  2020-10-16 22:19 [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Logan Gunthorpe
  2020-10-16 22:19 ` [PATCH v2 2/2] nvmet-passthru: Cleanup nvmet_passthru_map_sg() Logan Gunthorpe
@ 2020-10-19 11:20 ` Max Gurtovoy
  2020-10-19 15:40   ` Logan Gunthorpe
  2020-10-22 13:29 ` Christoph Hellwig
  2 siblings, 1 reply; 6+ messages in thread
From: Max Gurtovoy @ 2020-10-19 11:20 UTC (permalink / raw)
  To: Logan Gunthorpe, linux-nvme
  Cc: Christoph Hellwig, Chaitanya Kulkarni, Sagi Grimberg


On 10/17/2020 1:19 AM, Logan Gunthorpe wrote:
> 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")
> Suggested-by: Christoph Hellwig <hch@lst.de>
> Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Sagi Grimberg <sagi@grimberg.me>
> Cc: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
> ---
>   drivers/nvme/target/passthru.c | 9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
> index dacfa7435d0b..1ab88df3310f 100644
> --- a/drivers/nvme/target/passthru.c
> +++ b/drivers/nvme/target/passthru.c
> @@ -26,7 +26,7 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
>   	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);
> @@ -48,6 +48,13 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
>   	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
>   				      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 << (PAGE_SHIFT - 9),
> +				      max_hw_sectors);

can 9 be replaced with SECTOR_SHIFT ?


> +
>   	page_shift = NVME_CAP_MPSMIN(ctrl->cap) + 12;
>   
>   	id->mdts = ilog2(max_hw_sectors) + 9 - page_shift;
>
> base-commit: bbf5c979011a099af5dc76498918ed7df445635b

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES
  2020-10-19 11:20 ` [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Max Gurtovoy
@ 2020-10-19 15:40   ` Logan Gunthorpe
       [not found]     ` <BYAPR04MB4965B1360F5F6A1D13FF1DD0861F0@BYAPR04MB4965.namprd04.prod.outlook.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Logan Gunthorpe @ 2020-10-19 15:40 UTC (permalink / raw)
  To: Max Gurtovoy, linux-nvme
  Cc: Christoph Hellwig, Chaitanya Kulkarni, Sagi Grimberg



On 2020-10-19 5:20 a.m., Max Gurtovoy wrote:
> 
> On 10/17/2020 1:19 AM, Logan Gunthorpe wrote:
>> 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")
>> Suggested-by: Christoph Hellwig <hch@lst.de>
>> Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
>> Cc: Christoph Hellwig <hch@lst.de>
>> Cc: Sagi Grimberg <sagi@grimberg.me>
>> Cc: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
>> ---
>>   drivers/nvme/target/passthru.c | 9 ++++++++-
>>   1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
>> index dacfa7435d0b..1ab88df3310f 100644
>> --- a/drivers/nvme/target/passthru.c
>> +++ b/drivers/nvme/target/passthru.c
>> @@ -26,7 +26,7 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
>>   	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);
>> @@ -48,6 +48,13 @@ static u16 nvmet_passthru_override_id_ctrl(struct nvmet_req *req)
>>   	max_hw_sectors = min_not_zero(pctrl->max_segments << (PAGE_SHIFT - 9),
>>   				      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 << (PAGE_SHIFT - 9),
>> +				      max_hw_sectors);
> 
> can 9 be replaced with SECTOR_SHIFT ?

Oh, yes, that does sound better. Thanks.

Logan


_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES
       [not found]     ` <BYAPR04MB4965B1360F5F6A1D13FF1DD0861F0@BYAPR04MB4965.namprd04.prod.outlook.com>
@ 2020-10-20  2:05       ` Logan Gunthorpe
  0 siblings, 0 replies; 6+ messages in thread
From: Logan Gunthorpe @ 2020-10-20  2:05 UTC (permalink / raw)
  To: Chaitanya Kulkarni, Max Gurtovoy, linux-nvme
  Cc: Christoph Hellwig, Sagi Grimberg



On 2020-10-19 8:03 p.m., Chaitanya Kulkarni wrote:
> On 10/19/20 08:40, Logan Gunthorpe wrote:
>>>> +	max_hw_sectors = min_not_zero(BIO_MAX_PAGES << (PAGE_SHIFT - 9),
>>>> +				      max_hw_sectors);
>>> can 9 be replaced with SECTOR_SHIFT ?
>> Oh, yes, that does sound better. Thanks.
>>
>> Logan
>>
>>
> Although it is a good suggestion we are not using the SECTOR_SIZE in
> 
> the target code, I don't think we should change that for passthru. Also,
> 
> I remember that on the block layer mailing list SECTOR_SIZE macro been
> 
> discouraged over use of 9.

Well in that case v2 or v3 can be merged depending on maintainer preference.

Logan

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES
  2020-10-16 22:19 [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Logan Gunthorpe
  2020-10-16 22:19 ` [PATCH v2 2/2] nvmet-passthru: Cleanup nvmet_passthru_map_sg() Logan Gunthorpe
  2020-10-19 11:20 ` [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Max Gurtovoy
@ 2020-10-22 13:29 ` Christoph Hellwig
  2 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2020-10-22 13:29 UTC (permalink / raw)
  To: Logan Gunthorpe
  Cc: Chaitanya Kulkarni, Christoph Hellwig, linux-nvme, Sagi Grimberg

Thanks,

applied 1 and 2 to nvme-5.10.

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16 22:19 [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Logan Gunthorpe
2020-10-16 22:19 ` [PATCH v2 2/2] nvmet-passthru: Cleanup nvmet_passthru_map_sg() Logan Gunthorpe
2020-10-19 11:20 ` [PATCH v2 1/2] nvmet-passthru: Limit mdts by BIO_MAX_PAGES Max Gurtovoy
2020-10-19 15:40   ` Logan Gunthorpe
     [not found]     ` <BYAPR04MB4965B1360F5F6A1D13FF1DD0861F0@BYAPR04MB4965.namprd04.prod.outlook.com>
2020-10-20  2:05       ` Logan Gunthorpe
2020-10-22 13:29 ` Christoph Hellwig

Linux-NVME Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-nvme/0 linux-nvme/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-nvme linux-nvme/ https://lore.kernel.org/linux-nvme \
		linux-nvme@lists.infradead.org
	public-inbox-index linux-nvme

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.infradead.lists.linux-nvme


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git