Skip to contentSkip to conversation

fix virt_boundary_mask handling in SCSI v2

PATCH In review Christoph Hellwig 4 participants 7 messages Jun 24, 2025 – Jun 25, 2025

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

Hi all,

this series fixes a corruption when drivers using virt_boundary_mask set a limited max_segment_size by accident, which Red Hat reported as causing data corruption with storvsc. I did audit the tree and also found that this can affect SRP and iSER as well.

Changes since v1:
- improve the srp commit log
- slightly simplify the limits assignment in hosts.c

Diffstat:

 infiniband/ulp/srp/ib_srp.c |    5 +++--
 scsi/hosts.c                |   18 +++++++++++-------
 2 files changed, 14 insertions(+), 9 deletions(-)
#1 lore raw

PATCH 1/2 RDMA/srp: don't set a max_segment_size when virt_boundary_mask is set

virt_boundary_mask implies an unlimited max_segment_size. Setting both can lead to data corruption because __blk_rq_map_sg() can split requests so that the virt_boundary_mask is not respected if max_segment_size is not UINT_MAX.

---
 drivers/infiniband/ulp/srp/ib_srp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/infiniband/ulp/srp/ib_srp.c +3 -2
index 1378651735f6..23ed2fc688f0 100644
@@ -3705,9 +3705,10 @@ static ssize_t add_target_store(struct device *dev,
 	target_host->max_id      = 1;
 	target_host->max_lun     = -1LL;
 	target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
-	target_host->max_segment_size = ib_dma_max_seg_size(ibdev);
 
-	if (!(ibdev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG))
+	if (ibdev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG)
+		target_host->max_segment_size = ib_dma_max_seg_size(ibdev);
+	else
 		target_host->virt_boundary_mask = ~srp_dev->mr_page_mask;
 
 	target = host_to_target(target_host);
Signature · 1 line
2.47.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] RDMA/srp: don't set a max_segment_size when virt_boundary_mask is set

Quoted 7 lines · Christoph Hellwig

virt_boundary_mask implies an unlimited max_segment_size. Setting both can lead to data corruption because __blk_rq_map_sg() can split requests so that the virt_boundary_mask is not respected if max_segment_size is not UINT_MAX.

maintainer Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] RDMA/srp: don't set a max_segment_size when virt_boundary_mask is set

In reply to Christoph Hellwig
Quoted 18 lines · Christoph Hellwig

virt_boundary_mask implies an unlimited max_segment_size. Setting both can lead to data corruption because __blk_rq_map_sg() can split requests so that the virt_boundary_mask is not respected if max_segment_size is not UINT_MAX.

---
  drivers/infiniband/ulp/srp/ib_srp.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
drivers/infiniband/ulp/srp/ib_srp.c +0 -0
index 1378651735f6..23ed2fc688f0 100644
@@ -3705,9 +3705,10 @@ static ssize_t add_target_store(struct device *dev,
  	target_host->max_id      = 1;
  	target_host->max_lun     = -1LL;

drivers/infiniband/ulp/srp/ib_srp.c static ssize_t add_target_store(struct device *dev, · line 3714

Quoted 10 lines
drivers/infiniband/ulp/srp/ib_srp.c +3 -2
  	target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
-	target_host->max_segment_size = ib_dma_max_seg_size(ibdev);
 
-	if (!(ibdev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG))
+	if (ibdev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG)
+		target_host->max_segment_size = ib_dma_max_seg_size(ibdev);
+	else
  		target_host->virt_boundary_mask = ~srp_dev->mr_page_mask;
 
  	target = host_to_target(target_host);
#4 lore raw

PATCH 2/2 scsi: enforce unlimited max_segment_size when virt_boundary_mask is set

In reply to Christoph Hellwig

The virt_boundary_mask limit requires an unlimited max_segment_size for bio splitting to not corrupt data. Historically, the block layer tried to validate this, although the check was half-hearted until the addition of the atomic queue limits API. The full blown check than triggered issues with stacked devices incorrectly inheriting limits such as the virt boundary and got disabled in commit b561ea56a264 ("block: allow device to have both virt_boundary_mask and max segment size") instead of fixing the issue properly.

Ensure that the SCSI mid layer doesn't set the default low max_segment_size limit for this case, and check for invalid max_segment_size values in the host template, similar to the original block layer check given that SCSI devices can't be stacked.

This fixes reported data corruption on storvsc, although as far as I can tell storvsc always failed to properly set the max_segment_size limit as the SCSI APIs historically applied that when setting up the host, while storvsc only set the virt_boundary_mask when configuring the scsi_device.

Fixes 81988a0e6b03 ("storvsc: get rid of bounce buffer")
Fixes b561ea56a264 ("block: allow device to have both virt_boundary_mask and max segment size")
---
 drivers/scsi/hosts.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
drivers/scsi/hosts.c +11 -7
index e021f1106bea..cc5d05dc395c 100644
@@ -473,10 +473,17 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv
 	else
 		shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
 
-	if (sht->max_segment_size)
-		shost->max_segment_size = sht->max_segment_size;
-	else
-		shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
+	shost->virt_boundary_mask = sht->virt_boundary_mask;
+	if (shost->virt_boundary_mask) {
+		WARN_ON_ONCE(sht->max_segment_size &&
+			     sht->max_segment_size != UINT_MAX);
+		shost->max_segment_size = UINT_MAX;
+	} else {
+		if (sht->max_segment_size)
+			shost->max_segment_size = sht->max_segment_size;
+		else
+			shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
+	}
 
 	/* 32-byte (dword) is a common minimum for HBAs. */
 	if (sht->dma_alignment)
@@ -492,9 +499,6 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv
 	else
 		shost->dma_boundary = 0xffffffff;
 
-	if (sht->virt_boundary_mask)
-		shost->virt_boundary_mask = sht->virt_boundary_mask;
-
 	device_initialize(&shost->shost_gendev);
 	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
 	shost->shost_gendev.bus = &scsi_bus_type;
Signature · 1 line
2.47.2
maintainer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] scsi: enforce unlimited max_segment_size when virt_boundary_mask is set

Quoted 27 lines · Christoph Hellwig
drivers/scsi/hosts.c +11 -4
index e021f1106bea..cc5d05dc395c 100644
@@ -473,10 +473,17 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv
  	else
  		shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
 
-	if (sht->max_segment_size)
-		shost->max_segment_size = sht->max_segment_size;
-	else
-		shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
+	shost->virt_boundary_mask = sht->virt_boundary_mask;
+	if (shost->virt_boundary_mask) {
+		WARN_ON_ONCE(sht->max_segment_size &&
+			     sht->max_segment_size != UINT_MAX);
+		shost->max_segment_size = UINT_MAX;
+	} else {
+		if (sht->max_segment_size)
+			shost->max_segment_size = sht->max_segment_size;
+		else
+			shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
+	}
 
  	/* 32-byte (dword) is a common minimum for HBAs. */
  	if (sht->dma_alignment)

drivers/scsi/hosts.c struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv · line 504

Quoted 9 lines
drivers/scsi/hosts.c +0 -3
@@ -492,9 +499,6 @@ struct Scsi_Host *scsi_host_alloc(const struct scsi_host_template *sht, int priv
  	else
  		shost->dma_boundary = 0xffffffff;
 
-	if (sht->virt_boundary_mask)
-		shost->virt_boundary_mask = sht->virt_boundary_mask;
-
  	device_initialize(&shost->shost_gendev);
  	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
  	shost->shost_gendev.bus = &scsi_bus_type;
#6 lore raw
In reply to Christoph Hellwig
Quoted 10 lines · Christoph Hellwig

this series fixes a corruption when drivers using virt_boundary_mask set a limited max_segment_size by accident, which Red Hat reported as causing data corruption with storvsc. I did audit the tree and also found that this can affect SRP and iSER as well.

Changes since v1:
- improve the srp commit log
- slightly simplify the limits assignment in hosts.c

[...]

Applied to 6.16/scsi-fixes, thanks!

[1/2] RDMA/srp: don't set a max_segment_size when virt_boundary_mask is set

      https://git.kernel.org/mkp/scsi/c/844c6a160e69
[2/2] scsi: enforce unlimited max_segment_size when virt_boundary_mask is set
      https://git.kernel.org/mkp/scsi/c/4937e604ca24
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering