Skip to content

Commit 4e57482

Browse files
thomastaioraclegregkh
authored andcommitted
dma-direct: Fix potential NULL pointer dereference
[ Upstream commit f959dcd ] When booting the kernel v5.9-rc4 on a VM, the kernel would panic when printing a warning message in swiotlb_map(). The dev->dma_mask must not be a NULL pointer when calling the dma mapping layer. A NULL pointer check can potentially avoid the panic. Signed-off-by: Thomas Tai <thomas.tai@oracle.com> Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 48464c1 commit 4e57482

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

include/linux/dma-direct.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
6262
{
6363
dma_addr_t end = addr + size - 1;
6464

65-
if (!dev->dma_mask)
66-
return false;
67-
6865
if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
6966
min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn)))
7067
return false;

kernel/dma/mapping.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
144144
dma_addr_t addr;
145145

146146
BUG_ON(!valid_dma_direction(dir));
147+
148+
if (WARN_ON_ONCE(!dev->dma_mask))
149+
return DMA_MAPPING_ERROR;
150+
147151
if (dma_map_direct(dev, ops))
148152
addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
149153
else
@@ -179,6 +183,10 @@ int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
179183
int ents;
180184

181185
BUG_ON(!valid_dma_direction(dir));
186+
187+
if (WARN_ON_ONCE(!dev->dma_mask))
188+
return 0;
189+
182190
if (dma_map_direct(dev, ops))
183191
ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
184192
else
@@ -213,6 +221,9 @@ dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,
213221

214222
BUG_ON(!valid_dma_direction(dir));
215223

224+
if (WARN_ON_ONCE(!dev->dma_mask))
225+
return DMA_MAPPING_ERROR;
226+
216227
/* Don't allow RAM to be mapped */
217228
if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
218229
return DMA_MAPPING_ERROR;

0 commit comments

Comments
 (0)