Skip to contentSkip to conversation

Drop SCSI core dev->dma_mask check

PATCH Applied John Garry 5 participants 13 messages Jun 29 – Aug 6

Subsystems (2)
lore
Share & tools
lore

John Garry

author
#0 lore raw

cover Drop SCSI core dev->dma_mask check

In commit be8fcd4a8217 ("scsi: sas: Skip opt_sectors when DMA reports no real optimization hint"), the check for dev->dma_mask prior to calling dma_opt_mapping_size() was dropped.

However, it is not safe to do so, as dma_opt_mapping_size() -> dma_max_mapping_size() may try to dereference dev->dma_mask.

We just don't hit such a path as no SCSI HBA driver which has dev->dma_mask unset would use the SAS transport.

Fix that issue by adding a dev->dma_mask check in
dma_max_mapping_size().

The dev->dma_mask check in scsi_add_host_with_dma() can then be also dropped.

John Garry (2):
dma-mapping: make dma_max_mapping_size() return 0 for no DMA
capability
scsi: core: Drop dev->dma_mask check in evaluating max_sectors

 drivers/scsi/hosts.c | 6 ++----
 kernel/dma/mapping.c | 3 +++
 2 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH 1/2 dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

For when a device is not DMA capable, the max mapping size would be 0, so make dma_max_mapping_size() reflect that.

---
 kernel/dma/mapping.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 4fe04669e5e66..7e576e5c6b8be 100644
@@ -979,6 +979,9 @@ size_t dma_max_mapping_size(struct device *dev)
 	const struct dma_map_ops *ops = get_dma_ops(dev);
 	size_t size = SIZE_MAX;
 
+	if (!dev->dma_mask)
+		return 0;
+
 	if (dma_map_direct(dev, ops))
 		size = dma_direct_max_mapping_size(dev);
 	else if (use_dma_iommu(dev))
Signature · 1 line
2.43.7
#2 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] dma_max_mapping_size() returning 0 causes division by zero in amdxdna_cbuf_map() and invalid…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dma_max_mapping_size() returning 0 causes division by zero in amdxdna_cbuf_map() and invalid zero-sized block layer limits in MMC drivers. --

commit b19cb5dc0c3fd9b25263f65c9d042ee616692807
Author: John Garry <[email protected]>

dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

This commit updates dma_max_mapping_size() to return 0 when a device is not DMA capable, reflecting that the maximum mapping size for such devices is 0.

kernel/dma/mapping.c size_t dma_max_mapping_size(struct device *dev) · line 983

Quoted 10 lines
kernel/dma/mapping.c +2 -0
index 4fe04669e5e66..7e576e5c6b8be 100644
@@ -979,6 +979,9 @@ size_t dma_max_mapping_size(struct device *dev)
 	const struct dma_map_ops *ops = get_dma_ops(dev);
 	size_t size = SIZE_MAX;
 
+	if (!dev->dma_mask)
+		return 0;

[Severity: High]
Could this new return value of 0 cause a division by zero in callers that expect a strictly positive maximum mapping size?

For instance, in drivers/accel/amdxdna/amdxdna_cbuf.c:amdxdna_cbuf_map(), the returned size is used directly as a divisor:

        max_seg = min_t(size_t, UINT_MAX, dma_max_mapping_size(dev));
        n_entries = (cbuf->node.size + max_seg - 1) / max_seg;

If a device lacking a dma_mask maps the attachment, max_seg becomes 0, which would result in an immediate kernel panic.

Additionally, could this cause invalid zero-sized block layer queue limits in MMC drivers? In drivers/mmc/host/bcm2835.c:bcm2835_add_host(), the return value clamps max_req_size:

        mmc->max_req_size = min_t(size_t, 524288, dma_max_mapping_size(dev));

If the device lacks DMA support, max_req_size becomes 0, which propagates to the block layer and can trigger panics when processing requests.

Quoted 4 lines
kernel/dma/mapping.c +1 -0
+
 	if (dma_map_direct(dev, ops))
 		size = dma_direct_max_mapping_size(dev);
 	else if (use_dma_iommu(dev))
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

In reply to John Garry
Quoted 2 lines · John Garry

For when a device is not DMA capable, the max mapping size would be 0, so make dma_max_mapping_size() reflect that.

Seems logical.

Quoted 19 lines
---
  kernel/dma/mapping.c | 3 +++
  1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 4fe04669e5e66..7e576e5c6b8be 100644
@@ -979,6 +979,9 @@ size_t dma_max_mapping_size(struct device *dev)
  	const struct dma_map_ops *ops = get_dma_ops(dev);
  	size_t size = SIZE_MAX;
 
+	if (!dev->dma_mask)
+		return 0;
+
  	if (dma_map_direct(dev, ops))
  		size = dma_direct_max_mapping_size(dev);
  	else if (use_dma_iommu(dev))
author
#4 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

Quoted 7 lines · Robin Murphy
Quoted 2 lines · John Garry

For when a device is not DMA capable, the max mapping size would be 0, so make dma_max_mapping_size() reflect that.

Seems logical.

Thanks, but sashiko has been reviewing this and has a few things to say.

I don't know how it chooses recipients.

Quoted 7 lines · On 29/06/2026 10:10, [email protected] wrote:

kernel/dma/mapping.c size_t dma_max_mapping_size(struct device *dev) · line 983

Quoted 3 lines
kernel/dma/mapping.c +2 -0
+	if (!dev->dma_mask)
+		return 0;

[Severity: High]
Could this new return value of 0 cause a division by zero in callers that expect a strictly positive maximum mapping size?

Quoted 5 lines

For instance, in drivers/accel/amdxdna/amdxdna_cbuf.c:amdxdna_cbuf_map(), the returned size is used directly as a divisor:

         max_seg = min_t(size_t, UINT_MAX, dma_max_mapping_size(dev));
         n_entries = (cbuf->node.size + max_seg - 1) / max_seg;

Later in amdxdna_cbuf_map() we call dma_map_resource() (-> dma_map_phys()) and this would fail and WARN for !dev->dma_mask. Indeed amdxdna_cbuf_map() is used as a callback for .map_dma_buf, so highly unrealistic to have !dev->dma_mask ever.

Quoted 9 lines

If a device lacking a dma_mask maps the attachment, max_seg becomes 0, which would result in an immediate kernel panic.

Additionally, could this cause invalid zero-sized block layer queue limits in MMC drivers? In drivers/mmc/host/bcm2835.c:bcm2835_add_host(), the return value clamps max_req_size:

         mmc->max_req_size = min_t(size_t, 524288, dma_max_mapping_size(dev));

bcm2835.c is a platform device driver, and platform devices have their dev->dma_mask set in setup_pdev_dma_masks()

Indeed, that driver does have a non-DMA mode of operation, but that looks to be selected independent of whether dev->dma_mask is set.

Quoted 3 lines

If the device lacks DMA support, max_req_size becomes 0, which propagates to the block layer and can trigger panics when processing requests.

All other users of dma_max_mapping_size() are drivers for real/virtio HW, so should be no issues.

Quoted 20 lines
Quoted 19 lines

---
  kernel/dma/mapping.c | 3 +++

  1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 4fe04669e5e66..7e576e5c6b8be 100644
@@ -979,6 +979,9 @@ size_t dma_max_mapping_size(struct device *dev)
       const struct dma_map_ops *ops = get_dma_ops(dev);
       size_t size = SIZE_MAX;
+    if (!dev->dma_mask)
+        return 0;
+
       if (dma_map_direct(dev, ops))
           size = dma_direct_max_mapping_size(dev);
       else if (use_dma_iommu(dev))
reviewer
#5 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

Quoted 22 lines · John Garry
Quoted 7 lines · Robin Murphy
Quoted 2 lines · John Garry

For when a device is not DMA capable, the max mapping size would be 0, so make dma_max_mapping_size() reflect that.

Seems logical.

Thanks, but sashiko has been reviewing this and has a few things to say.

I don't know how it chooses recipients.

Quoted 5 lines · On 29/06/2026 10:10, [email protected] wrote:
Quoted 3 lines

+    if (!dev->dma_mask)
+        return 0;

[Severity: High]
Could this new return value of 0 cause a division by zero in callers

that

Quoted 2 lines

expect a strictly positive maximum mapping size?

Quoted 8 lines
Quoted 1 line

For instance, in drivers/accel/amdxdna/

amdxdna_cbuf.c:amdxdna_cbuf_map(),

Quoted 4 lines

the returned size is used directly as a divisor:

          max_seg = min_t(size_t, UINT_MAX, dma_max_mapping_size(dev)); n_entries = (cbuf->node.size + max_seg - 1) / max_seg;

Later in amdxdna_cbuf_map() we call dma_map_resource() (-> dma_map_phys()) and this would fail and WARN for !dev->dma_mask. Indeed amdxdna_cbuf_map() is used as a callback for .map_dma_buf, so highly unrealistic to have !dev->dma_mask ever.

Moreover, the driver would have failed to probe in the first place if its dma_set_mask_and_coherent() call had failed.

Quoted 9 lines
Quoted 5 lines

If a device lacking a dma_mask maps the attachment, max_seg becomes 0, which would result in an immediate kernel panic.

Additionally, could this cause invalid zero-sized block layer queue

limits

Quoted 3 lines

in MMC drivers? In drivers/mmc/host/bcm2835.c:bcm2835_add_host(), the return value clamps max_req_size:

Quoted 6 lines
Quoted 1 line

          mmc->max_req_size = min_t(size_t, 524288,

dma_max_mapping_size(dev));

bcm2835.c is a platform device driver, and platform devices have their dev->dma_mask set in setup_pdev_dma_masks()

Indeed, that driver does have a non-DMA mode of operation, but that looks to be selected independent of whether dev->dma_mask is set.

That one seems pretty bogus already, given that DMA mode is apparently dependent on an external DMA channel, so "dev" is the wrong device to check (should be dmaengine_get_dma_device()), while conversely dma_max_mapping_size(anything) is a questionably meaningless number for PIO mode... :/

Cheers,
Robin.

Quoted 29 lines
Quoted 3 lines

If the device lacks DMA support, max_req_size becomes 0, which

propagates

Quoted 1 line

to the block layer and can trigger panics when processing requests.

All other users of dma_max_mapping_size() are drivers for real/virtio HW, so should be no issues.

Quoted 20 lines
Quoted 19 lines

---
  kernel/dma/mapping.c | 3 +++

  1 file changed, 3 insertions(+)
kernel/dma/mapping.c +3 -0
index 4fe04669e5e66..7e576e5c6b8be 100644
@@ -979,6 +979,9 @@ size_t dma_max_mapping_size(struct device *dev)
       const struct dma_map_ops *ops = get_dma_ops(dev);
       size_t size = SIZE_MAX;
+    if (!dev->dma_mask)
+        return 0;
+
       if (dma_map_direct(dev, ops))
           size = dma_direct_max_mapping_size(dev);
       else if (use_dma_iommu(dev))
#6 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

Quoted 11 lines · Robin Murphy
Quoted 11 lines
Quoted 1 line

Additionally, could this cause invalid zero-sized block layer queue

limits

Quoted 4 lines

in MMC drivers? In drivers/mmc/host/bcm2835.c:bcm2835_add_host(), the return value clamps max_req_size:

          mmc->max_req_size = min_t(size_t, 524288,

dma_max_mapping_size(dev));

bcm2835.c is a platform device driver, and platform devices have their dev->dma_mask set in setup_pdev_dma_masks()

Quoted 8 lines
Quoted 2 lines

Indeed, that driver does have a non-DMA mode of operation, but that looks to be selected independent of whether dev->dma_mask is set.

That one seems pretty bogus already, given that DMA mode is apparently dependent on an external DMA channel, so "dev" is the wrong device to check (should be dmaengine_get_dma_device()), while conversely dma_max_mapping_size(anything) is a questionably meaningless number for PIO mode... :/

Yeah. It would be good to get this fixed before this change hits mainline, though.

author
#7 lore raw

Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

Quoted 15 lines · Christoph Hellwig
Quoted 14 lines · Robin Murphy
Quoted 13 lines
Quoted 1 line

Additionally, could this cause invalid zero-sized block layer queue

limits

Quoted 4 lines

in MMC drivers? In drivers/mmc/host/bcm2835.c:bcm2835_add_host(), the return value clamps max_req_size:

          mmc->max_req_size = min_t(size_t, 524288,

dma_max_mapping_size(dev));

bcm2835.c is a platform device driver, and platform devices have their dev->dma_mask set in setup_pdev_dma_masks()

Indeed, that driver does have a non-DMA mode of operation, but that looks to be selected independent of whether dev->dma_mask is set.

Quoted 9 lines
Quoted 5 lines

That one seems pretty bogus already, given that DMA mode is apparently dependent on an external DMA channel, so "dev" is the wrong device to check (should be dmaengine_get_dma_device()), while conversely dma_max_mapping_size(anything) is a questionably meaningless number for PIO mode... :/

Yeah. It would be good to get this fixed before this change hits mainline, though.

ok, so then I'm thinking of something like the following:

@@ -1305,7 +1305,13 @@ static int bcm2835_add_host(struct bcm2835_host *host)

        }

        mmc->max_segs = 128;
-       mmc->max_req_size = min_t(size_t, 524288, dma_max_mapping_size(dev));
+       mmc->max_req_size = 524288;
+       if (host->use_dma) {

+ struct device *dma_dev =
dmaengine_get_dma_device(host->dma_chan_rxtx);
+
+ mmc->max_req_size = min_t(size_t, mmc->max_req_size,
+ dma_max_mapping_size(dma_dev));
+ }

author
#9 lore raw

PATCH 2/2 scsi: core: Drop dev->dma_mask check in evaluating max_sectors

In reply to John Garry

When evaluating shost->max_sectors, we currently check dma_dev->dma_mask is non-NULL, as dma_max_mapping_size(dma_dev) could previously not handle unset dma_dev->dma_mask - this is no longer the case.

---
 drivers/scsi/hosts.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/scsi/hosts.c +2 -4
index e047747d4ecf8..d512080268afe 100644
@@ -252,10 +252,8 @@ int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
 
 	shost->dma_dev = dma_dev;
 
-	if (dma_dev->dma_mask) {
-		shost->max_sectors = min_t(unsigned int, shost->max_sectors,
-				dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
-	}
+	shost->max_sectors = min_not_zero(shost->max_sectors,
+			dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
 
 	error = scsi_mq_setup_tags(shost);
 	if (error)
Signature · 1 line
2.43.7
#11 lore raw
In reply to John Garry

John,

Quoted 3 lines

In commit be8fcd4a8217 ("scsi: sas: Skip opt_sectors when DMA reports no real optimization hint"), the check for dev->dma_mask prior to calling dma_opt_mapping_size() was dropped.

Applied to 7.3/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen
#12 lore raw
In reply to John Garry
Quoted 8 lines · John Garry

In commit be8fcd4a8217 ("scsi: sas: Skip opt_sectors when DMA reports no real optimization hint"), the check for dev->dma_mask prior to calling dma_opt_mapping_size() was dropped.

However, it is not safe to do so, as dma_opt_mapping_size() -> dma_max_mapping_size() may try to dereference dev->dma_mask.

[...]

Applied to 7.3/scsi-queue, thanks!

[1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability

      https://git.kernel.org/mkp/scsi/c/81d273096f12
[2/2] scsi: core: Drop dev->dma_mask check in evaluating max_sectors
      https://git.kernel.org/mkp/scsi/c/6e468644fc17
Signature · 1 line
Martin K. Petersen