Skip to content

Commit

Permalink
dma-direct: Fix potential NULL pointer dereference
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
thomastaioracle authored and Christoph Hellwig committed Sep 17, 2020
1 parent a92df4f commit f959dcd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 0 additions & 3 deletions include/linux/dma-direct.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size,
{
dma_addr_t end = addr + size - 1;

if (!dev->dma_mask)
return false;

if (is_ram && !IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT) &&
min(addr, end) < phys_to_dma(dev, PFN_PHYS(min_low_pfn)))
return false;
Expand Down
11 changes: 11 additions & 0 deletions kernel/dma/mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
dma_addr_t addr;

BUG_ON(!valid_dma_direction(dir));

if (WARN_ON_ONCE(!dev->dma_mask))
return DMA_MAPPING_ERROR;

if (dma_map_direct(dev, ops))
addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
else
Expand Down Expand Up @@ -179,6 +183,10 @@ int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents,
int ents;

BUG_ON(!valid_dma_direction(dir));

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

if (dma_map_direct(dev, ops))
ents = dma_direct_map_sg(dev, sg, nents, dir, attrs);
else
Expand Down Expand Up @@ -213,6 +221,9 @@ dma_addr_t dma_map_resource(struct device *dev, phys_addr_t phys_addr,

BUG_ON(!valid_dma_direction(dir));

if (WARN_ON_ONCE(!dev->dma_mask))
return DMA_MAPPING_ERROR;

/* Don't allow RAM to be mapped */
if (WARN_ON_ONCE(pfn_valid(PHYS_PFN(phys_addr))))
return DMA_MAPPING_ERROR;
Expand Down

0 comments on commit f959dcd

Please sign in to comment.