Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: comedi: gsc_hpdi: check dma_alloc_coherent() return value

The "auto-attach" handler function `gsc_hpdi_auto_attach()` calls
`dma_alloc_coherent()` in a loop to allocate some DMA data buffers, and
also calls it to allocate a buffer for a DMA descriptor chain. However,
it does not check the return value of any of these calls. Change
`gsc_hpdi_auto_attach()` to return `-ENOMEM` if any of these
`dma_alloc_coherent()` calls fail. This will result in the comedi core
calling the "detach" handler `gsc_hpdi_detach()` as part of the
clean-up, which will call `gsc_hpdi_free_dma()` to free any allocated
DMA coherent memory buffers.

Cc: <stable@vger.kernel.org> #4.6+
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Link: https://lore.kernel.org/r/20191216110823.216237-1-abbotti@mev.co.uk
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Ian Abbott and committed by
Greg Kroah-Hartman
ab42b48f d1eef1c6

+10
+10
drivers/staging/comedi/drivers/gsc_hpdi.c
··· 623 623 dma_alloc_coherent(&pcidev->dev, DMA_BUFFER_SIZE, 624 624 &devpriv->dio_buffer_phys_addr[i], 625 625 GFP_KERNEL); 626 + if (!devpriv->dio_buffer[i]) { 627 + dev_warn(dev->class_dev, 628 + "failed to allocate DMA buffer\n"); 629 + return -ENOMEM; 630 + } 626 631 } 627 632 /* allocate dma descriptors */ 628 633 devpriv->dma_desc = dma_alloc_coherent(&pcidev->dev, ··· 635 630 NUM_DMA_DESCRIPTORS, 636 631 &devpriv->dma_desc_phys_addr, 637 632 GFP_KERNEL); 633 + if (!devpriv->dma_desc) { 634 + dev_warn(dev->class_dev, 635 + "failed to allocate DMA descriptors\n"); 636 + return -ENOMEM; 637 + } 638 638 if (devpriv->dma_desc_phys_addr & 0xf) { 639 639 dev_warn(dev->class_dev, 640 640 " dma descriptors not quad-word aligned (bug)\n");