Skip to contentSkip to conversation

pci_alloc_consistent() and/or dma_free_coherent() bug?

PATCH Krzysztof Halasa 1 participant 2 messages Aug 5, 2003

lore
Share & tools
lore
#0 lore raw

Hi,

I'm doing something like:

        pci_set_dma_mask(pdev, 0xFFFFFFFF) /* 32 bits */
	pci_set_consistent_dma_mask(pdev, 0x0FFFFFFF); /* 28 bits only */

as this card is able to do 32-bit PCI (bus master) addressing on memory returned by pci_map_single() but it needs "consistent" region in the first 256 MB of RAM.

On i386, pci_alloc_consistent() calls dma_alloc_coherent().

Now:
void *dma_alloc_coherent(struct device *dev, size_t size,
dma_addr_t *dma_handle, int gfp)

{
        void *ret;
        /* ignore region specifiers */
        gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);

        if (dev == NULL || (*dev->dma_mask < 0xffffffff))
                gfp |= GFP_DMA;
        ret = (void *)__get_free_pages(gfp, get_order(size));

        if (ret != NULL) {
                memset(ret, 0, size);
                *dma_handle = virt_to_phys(ret);
        }
        return ret;
}

while:

pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)

{
        if (!pci_dma_supported(dev, mask))
                return -EIO;

        dev->consistent_dma_mask = mask;

        return 0;
}

pci_set_consistent_dma_mask() sets dev->consistent_dma_mask while dma_alloc_coherent() (and thus pci_alloc_consistent()) expects the mask to be in dev->dma_mask.

The "obvious" fix seems to be:

i386/kernel/pci-dma.c renamed from i386/kernel/pci-dma.c.orig +1 -1
@@ -20,7 +20,7 @@
 	/* ignore region specifiers */
 	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
 
-	if (dev == NULL || (*dev->dma_mask < 0xffffffff))
+	if (dev == NULL || (*dev->consistent_dma_mask < 0xffffffff))
 		gfp |= GFP_DMA;
 	ret = (void *)__get_free_pages(gfp, get_order(size));
 

but I'm not sure if we need to adjust dev->consistent_dma_mask in pci_set_dma_mask() (not only in pci_set_consistent_dma_mask()), in case a driver only calls pci_set_dma_mask() and assumes it will work for pci_alloc_consistent() as well.

Signature · 2 lines
Krzysztof Halasa
Network Administrator
#1 lore raw

Actually it would probably be better to do something like that:

i386/kernel/pci-dma.c renamed from i386/kernel/pci-dma.c.orig +1 -1
@@ -20,7 +20,7 @@
 	/* ignore region specifiers */
 	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
 
-	if (dev == NULL || (*dev->dma_mask < 0xffffffff))
+	if (dev == NULL || ((*dev->consistent_dma_mask & *dev->dma_mask) < 0xffffffff))
 		gfp |= GFP_DMA;
 	ret = (void *)__get_free_pages(gfp, get_order(size));
 

So we don't need to touch pci_set*dma_mask and we don't need to call them in specific order.

Signature · 2 lines
Krzysztof Halasa
Network Administrator