Drop SCSI core dev->dma_mask check
Subsystems (2)
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 indma_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
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
@@ -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
[email protected]
AI reviewRe: [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@@ -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
Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability
In reply to John GarryQuoted 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
Signed-off-by John Garry <[email protected]>--- kernel/dma/mapping.c | 3 +++ 1 file changed, 3 insertions(+)kernel/dma/mapping.c +3
-0@@ -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))
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.
Reviewed-by Robin Murphy <[email protected]>
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_maskmaps the attachment,max_segbecomes 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_sizebecomes 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
Signed-off-by John Garry <[email protected]>---
kernel/dma/mapping.c| 3 +++1 file changed, 3 insertions(+)kernel/dma/mapping.c +3
-0@@ -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))
Robin Murphy
reviewerRe: [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.
Reviewed-by Robin Murphy <[email protected]>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 callersthat
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 calldma_map_resource()(->dma_map_phys()) and this would fail and WARN for !dev->dma_mask. Indeedamdxdna_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_maskmaps the attachment,max_segbecomes 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.cis a platform device driver, and platform devices have theirdev->dma_maskset insetup_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_maskis 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_sizebecomes 0, whichpropagates
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
Signed-off-by John Garry <[email protected]>---
kernel/dma/mapping.c| 3 +++1 file changed, 3 insertions(+)kernel/dma/mapping.c +3
-0@@ -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))
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.cis a platform device driver, and platform devices have theirdev->dma_maskset insetup_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_maskis 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 converselydma_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.
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.cis a platform device driver, and platform devices have theirdev->dma_maskset insetup_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_maskis 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 converselydma_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));
+ }
Re: [PATCH 1/2] dma-mapping: make dma_max_mapping_size() return 0 for no DMA capability
In reply to John GarryLooks good:
PATCH 2/2 scsi: core: Drop dev->dma_mask check in evaluating max_sectors
In reply to John GarryWhen 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
@@ -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
Re: [PATCH 2/2] scsi: core: Drop dev->dma_mask check in evaluating max_sectors
Looks good:
Martin K. Petersen
AppliedJohn,
Quoted 3 lines
In commit
be8fcd4a8217("scsi: sas: Skipopt_sectorswhen DMA reports no real optimization hint"), the check fordev->dma_maskprior to callingdma_opt_mapping_size()was dropped.
Applied to 7.3/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen
Martin K. Petersen
AppliedQuoted 8 lines · John Garry
In commit
be8fcd4a8217("scsi: sas: Skipopt_sectorswhen DMA reports no real optimization hint"), the check fordev->dma_maskprior to callingdma_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 dereferencedev->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