Skip to contentSkip to conversation

scsi: storvsc: Correct sysfs parameters as per Hyper-V storvsc requirement

PATCH In review Saurabh Sengar 2 participants 6 messages Jun 10, 2022 – Jun 13, 2022

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

PATCH 1/1 scsi: storvsc: Correct sysfs parameters as per Hyper-V storvsc requirement

This patch corrects 3 parameters:
1. Correct the sysfs entry for maximum hardware transfer limit of single transfer (max_hw_sectors_kb) by setting max_sectors, this was set to default value 512kb before.
2. Correct SGL memory offset alignment as per Hyper-V page size.
3. Correct sg_tablesize which accounts for max SGL segments entries in a single SGL.

---
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +24 -4
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >> HV_HYP_PAGE_SHIFT) + 2;
 	/*
 	 * For non-IDE disks, the host supports multiple channels.
 	 * Set the number of HW queues we are supporting.
Signature · 1 line
2.25.1
#1 lore raw
Quoted 63 lines · Saurabh Sengar

This patch corrects 3 parameters:
1. Correct the sysfs entry for maximum hardware transfer limit of single transfer (max_hw_sectors_kb) by setting max_sectors, this was set to default value 512kb before.
2. Correct SGL memory offset alignment as per Hyper-V page size.
3. Correct sg_tablesize which accounts for max SGL segments entries in a single SGL.

---
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +24 -4
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >> HV_HYP_PAGE_SHIFT) + 2;
 	/*
 	 * For non-IDE disks, the host supports multiple channels.
 	 * Set the number of HW queues we are supporting.
Signature · 1 line
2.25.1
#2 lore raw

From: Saurabh Singh Sengar <[email protected]> Sent: Friday, June 10, 2022 9:37 AM

Quoted 11 lines
Quoted 7 lines · Saurabh Sengar

This patch corrects 3 parameters:
1. Correct the sysfs entry for maximum hardware transfer limit of single transfer (max_hw_sectors_kb) by setting max_sectors, this was set to default value 512kb before.
2. Correct SGL memory offset alignment as per Hyper-V page size.
3. Correct sg_tablesize which accounts for max SGL segments entries in a single SGL.

I think a richer explanation in the commit message is warranted. Something like:

Current code is based on the idea that the max number of SGL entries also determines the max size of an I/O request. While this idea was true in older versions of the storvsc driver when SGL entry length was limited to 4 Kbytes, commit 3d9c3dcc58e9 ("scsi: storvsc: Enable scatter list entry lengths > 4Kbytes") removed that limitation. It's now theoretically possible for the block layer to send requests that exceed the maximum size supported by Hyper-V. This problem doesn't currently happen in practice because the block layer defaults to a 512 Kbyte maximum, while Hyper-V in Azure supports 2 Mbyte I/O sizes. But some future configuration of Hyper-V could have a smaller max I/O size, and the block layer could exceed that max.

Fix this by correctly setting max_sectors as well as sg_tablesize to reflect the maximum I/O size that Hyper-V reports. While allowing larger I/O sizes larger than the block layer default of 512 Kbytes doesn't provide any noticeable performance benefit in the tests we ran, it's still appropriate to report the correct underlying Hyper-V capabilities to the Linux block layer.

Also tweak the virt_boundary_mask to reflect that the needed alignment derives from Hyper-V communication using a 4 Kbyte page size, and not on the guest page size, which might be bigger (on ARM64, for example).

I don't think the title of the commit should focus on sysfs. This commit is about correctly reporting Hyper-V I/O size limits; the sysfs entries just provide visibility into the values.

And given that the problem was introduced by the above mentioned commit, it would be appropriate to add a "Fixes:" tag.

Quoted 43 lines
Quoted 43 lines
---
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +18 -3
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
Quoted 8 lines

drivers/scsi/storvsc_drv.c static int storvsc_probe(struct hv_device *device, · line 1996

Quoted 8 lines
drivers/scsi/storvsc_drv.c +6 -1
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >> HV_HYP_PAGE_SHIFT) + 2;

This calculation covers all possible I/O request sizes up to and including the value of max_transfer_bytes, even if max_transfer_bytes is some
weird number that's not a multiple of 512. So I think it works as
intended.

But setting host->max_sectors means that storvsc won't see an I/O request with a weird size, and some of the cases handled by the calculation don't actually occur. You could use a simpler calculation that's a bit easier to understand:

host->sg_tablesize = (stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE) + 1;

The "+1" handles the unaligned case you mention above.

Michael

Quoted 5 lines
Quoted 5 lines
drivers/scsi/storvsc_drv.c +0 -1
 	/*
 	 * For non-IDE disks, the host supports multiple channels.
 	 * Set the number of HW queues we are supporting.
--

2.25.1

#3 lore raw

Thanks Michael for review, please find my comments inline

Quoted 37 lines · Michael Kelley (LINUX)

From: Saurabh Singh Sengar <[email protected]> Sent: Friday, June 10, 2022 9:37 AM

Quoted 11 lines
Quoted 7 lines · Saurabh Sengar

This patch corrects 3 parameters:
1. Correct the sysfs entry for maximum hardware transfer limit of single transfer (max_hw_sectors_kb) by setting max_sectors, this was set to default value 512kb before.
2. Correct SGL memory offset alignment as per Hyper-V page size.
3. Correct sg_tablesize which accounts for max SGL segments entries in a single SGL.

I think a richer explanation in the commit message is warranted. Something like:

Current code is based on the idea that the max number of SGL entries also determines the max size of an I/O request. While this idea was true in older versions of the storvsc driver when SGL entry length was limited to 4 Kbytes, commit 3d9c3dcc58e9 ("scsi: storvsc: Enable scatter list entry lengths > 4Kbytes") removed that limitation. It's now theoretically possible for the block layer to send requests that exceed the maximum size supported by Hyper-V. This problem doesn't currently happen in practice because the block layer defaults to a 512 Kbyte maximum, while Hyper-V in Azure supports 2 Mbyte I/O sizes. But some future configuration of Hyper-V could have a smaller max I/O size, and the block layer could exceed that max.

Fix this by correctly setting max_sectors as well as sg_tablesize to reflect the maximum I/O size that Hyper-V reports. While allowing larger I/O sizes larger than the block layer default of 512 Kbytes doesn't provide any noticeable performance benefit in the tests we ran, it's still appropriate to report the correct underlying Hyper-V capabilities to the Linux block layer.

Also tweak the virt_boundary_mask to reflect that the needed alignment derives from Hyper-V communication using a 4 Kbyte page size, and not on the guest page size, which might be bigger (on ARM64, for example).

Quoted 6 lines

I don't think the title of the commit should focus on sysfs. This commit is about correctly reporting Hyper-V I/O size limits; the sysfs entries just provide visibility into the values.

And given that the problem was introduced by the above mentioned commit, it would be appropriate to add a "Fixes:" tag.

[SS] : Thanks, will fix this

Quoted 58 lines
Quoted 44 lines
Quoted 44 lines
---
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +18 -3
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
Quoted 8 lines

drivers/scsi/storvsc_drv.c static int storvsc_probe(struct hv_device *device, · line 1996

Quoted 8 lines
drivers/scsi/storvsc_drv.c +6 -1
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >> HV_HYP_PAGE_SHIFT) + 2;

This calculation covers all possible I/O request sizes up to and including the value of max_transfer_bytes, even if max_transfer_bytes is some
weird number that's not a multiple of 512. So I think it works as
intended.

Quoted 8 lines

But setting host->max_sectors means that storvsc won't see an I/O request with a weird size, and some of the cases handled by the calculation don't actually occur. You could use a simpler calculation that's a bit easier to understand:

host->sg_tablesize = (stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE) + 1;

The "+1" handles the unaligned case you mention above.

[SS] : As per my understanding this may give incorrect result for unaligned cases. Lets take an example of 6KB, "stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE" will give only 1, and then host->sq_tablesize will get final value as 2. Where as there is a possibility of 3 segments
1. 1KB
2. 4KB
3. 1KB

Please correct me if this scenario is not possible.

- Saurabh

Quoted 8 lines

Michael

Quoted 5 lines
Quoted 5 lines
drivers/scsi/storvsc_drv.c +0 -1
 	/*
 	 * For non-IDE disks, the host supports multiple channels.
 	 * Set the number of HW queues we are supporting.
--

2.25.1

#4 lore raw

From: Saurabh Singh Sengar <[email protected]> Sent: Sunday, June 12, 2022 9:06 PM

Quoted 7 lines

Thanks Michael for review, please find my comments inline

Quoted 1 line · Michael Kelley (LINUX)

From: Saurabh Singh Sengar <[email protected]> Sent: Friday, June 10,

2022 9:37 AM

Quoted 1 line
Quoted 1 line

[snip]

Quoted 73 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +18 -3
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
Quoted 8 lines
Quoted 8 lines

drivers/scsi/storvsc_drv.c static int storvsc_probe(struct hv_device *device,

Quoted 8 lines
drivers/scsi/storvsc_drv.c +6 -1
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >>

HV_HYP_PAGE_SHIFT) + 2;

Quoted 6 lines

This calculation covers all possible I/O request sizes up to and including the value of max_transfer_bytes, even if max_transfer_bytes is some
weird number that's not a multiple of 512. So I think it works as
intended.

Quoted 8 lines

But setting host->max_sectors means that storvsc won't see an I/O request with a weird size, and some of the cases handled by the calculation don't actually occur. You could use a simpler calculation that's a bit easier to understand:

host->sg_tablesize = (stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE) + 1;

The "+1" handles the unaligned case you mention above.

[SS] : As per my understanding this may give incorrect result for unaligned cases. Lets take an
example of 6KB, "stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE" will give only 1, and then
host->sq_tablesize will get final value as 2. Where as there is a possibility of 3 segments
1. 1KB
2. 4KB
3. 1KB

Quoted 1 line

Please correct me if this scenario is not possible.

Ah yes, you are right.

But consider the case where max_transfer_bytes is something like 8292
(i.e., 8K + 100). sg_tablesize will be calculated as 4, but it really only needs to
be 3 because max_sectors is the equivalent of 8K.

Here's an alternate approach that might be simpler. Since any reasonable Hyper-V configuration will provide a max_transfer_bytes value that is a multiple of HV_HYP_PAGE_SIZE, this approach doesn't change anything but still protects against Hyper-V providing a weird value:

	u32 maxbytes;

	maxbytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
	host->max_sectors = maxbytes >> 9;
	host->sg_tablesize = (maxbytes >> HV_HYP_PAGE_SHIFT) + 1;

Michael

#5 lore raw
Quoted 84 lines · Michael Kelley (LINUX)

From: Saurabh Singh Sengar <[email protected]> Sent: Sunday, June 12, 2022 9:06 PM

Quoted 7 lines

Thanks Michael for review, please find my comments inline

Quoted 1 line · Michael Kelley (LINUX)

From: Saurabh Singh Sengar <[email protected]> Sent: Friday, June 10,

2022 9:37 AM

Quoted 1 line
Quoted 1 line

[snip]

Quoted 73 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
 drivers/scsi/storvsc_drv.c | 28 ++++++++++++++++++++++++----
 1 file changed, 24 insertions(+), 4 deletions(-)
drivers/scsi/storvsc_drv.c +18 -3
index ca3530982e52..3e032660ae36 100644
@@ -1844,7 +1844,7 @@ static struct scsi_host_template scsi_driver = {
 	.cmd_per_lun =		2048,
 	.this_id =		-1,
 	/* Ensure there are no gaps in presented sgls */
-	.virt_boundary_mask =	PAGE_SIZE-1,
+	.virt_boundary_mask =	HV_HYP_PAGE_SIZE - 1,
 	.no_write_same =	1,
 	.track_queue_depth =	1,
 	.change_queue_depth =	storvsc_change_queue_depth,
@@ -1969,11 +1969,31 @@ static int storvsc_probe(struct hv_device *device,
 	/* max cmd length */
 	host->max_cmd_len = STORVSC_MAX_CMD_LEN;
 
+	/* max_hw_sectors_kb */
+	host->max_sectors = (stor_device->max_transfer_bytes) >> 9;
 	/*
-	 * set the table size based on the info we got
-	 * from the host.
+	 * There are 2 requirements for Hyper-V storvsc sgl segments,
+	 * based on which the below calculation for max segments is
+	 * done:
+	 *
+	 * 1. Except for the first and last sgl segment, all sgl segments
+	 *    should be align to HV_HYP_PAGE_SIZE, that also means the
+	 *    maximum number of segments in a sgl can be calculated by
+	 *    dividing the total max transfer length by HV_HYP_PAGE_SIZE.
+	 *
+	 * 2. Except for the first and last, each entry in the SGL must
+	 *    have an offset that is a multiple of HV_HYP_PAGE_SIZE,
+	 *    whereas the complete length of transfer may not be aligned
+	 *    to HV_HYP_PAGE_SIZE always. This can result in 2 cases:
+	 *    Example for unaligned case: Let's say the total transfer
+	 *    length is 6 KB, the max segments will be 3 (1,4,1).
Quoted 8 lines
Quoted 8 lines

drivers/scsi/storvsc_drv.c static int storvsc_probe(struct hv_device *device,

Quoted 8 lines
drivers/scsi/storvsc_drv.c +6 -1
+	 *    Example for aligned case: Let's say the total transfer length
+	 *    is 8KB, then max segments will still be 3(2,4,2) and not 4.
+	 *    4 (read next higher value) segments will only be required
+	 *    once the length is at least 2 bytes more then 8KB (read any
+	 *    HV_HYP_PAGE_SIZE aligned length).
 	 */
-	host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT);
+	host->sg_tablesize = ((stor_device->max_transfer_bytes - 2) >>

HV_HYP_PAGE_SHIFT) + 2;

Quoted 6 lines

This calculation covers all possible I/O request sizes up to and including the value of max_transfer_bytes, even if max_transfer_bytes is some
weird number that's not a multiple of 512. So I think it works as
intended.

Quoted 8 lines

But setting host->max_sectors means that storvsc won't see an I/O request with a weird size, and some of the cases handled by the calculation don't actually occur. You could use a simpler calculation that's a bit easier to understand:

host->sg_tablesize = (stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE) + 1;

The "+1" handles the unaligned case you mention above.

[SS] : As per my understanding this may give incorrect result for unaligned cases. Lets take an
example of 6KB, "stor_device->max_transfer_bytes >> HV_HYP_PAGE_SIZE" will give only 1, and then
host->sq_tablesize will get final value as 2. Where as there is a possibility of 3 segments
1. 1KB
2. 4KB
3. 1KB

Quoted 8 lines
Quoted 1 line

Please correct me if this scenario is not possible.

Ah yes, you are right.

But consider the case where max_transfer_bytes is something like 8292
(i.e., 8K + 100). sg_tablesize will be calculated as 4, but it really only needs to
be 3 because max_sectors is the equivalent of 8K.

[SS]: I agree, ultimately max transfer bytes will be calculated based on max_sectors, and that value will always be aligned to 512b.

Quoted 5 lines

Here's an alternate approach that might be simpler. Since any reasonable Hyper-V configuration will provide a max_transfer_bytes value that is a multiple of HV_HYP_PAGE_SIZE, this approach doesn't change anything but still protects against Hyper-V providing a weird value:

Quoted 7 lines
	u32 maxbytes;

	maxbytes = round_down(stor_device->max_transfer_bytes, HV_HYP_PAGE_SIZE);
	host->max_sectors = maxbytes >> 9;
	host->sg_tablesize = (maxbytes >> HV_HYP_PAGE_SHIFT) + 1;

Michael

[SS] : Rounding down to HV_HYP_PAGE_SIZE, make perfect sense, thanks !!

- Saurabh