linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] DMA allocation improvement
@ 2004-08-27 16:52 Takashi Iwai
  2004-08-27 16:55 ` [PATCH] ALSA hack clean up (DMA allocation improvement) Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Takashi Iwai @ 2004-08-27 16:52 UTC (permalink / raw)
  To: linux-mm

Hi,

When coherent_dma_mask is set to less than 32bit, dma_alloc_coherent()
(on i386 and ppc) invokes __get_free_pages() always with __GFP_DMA.
This results in the exhaust of zone DMA when too many DMA buffers are
allocated.  The attached patch is an improvement for such cases.

This won't fix the memory allocation problems of such devices
perfectly, but at least, it should work better.
(And they are anyway likely old, put on the machines with the RAM
 covered in 24bit mask :)

x86-64 pci-nommu.c has already a similar workaround.

The patch is to 2.6.9-rc1-mm1.  It's applied fine to 2.6.8.1-mm, too.


Takashi

================================================================

The patch improves the allocation of DMA pages via
dma_alloc_coherent() with less-than-32bit coherent_dma_mask.
It tries pages with GFP_KERNEL, if possible, with the check of the
validity of the obtained address.

Signed-off-by: Takashi Iwai <tiwai@suse.de>

--- linux-2.6.9-rc1-mm1/arch/i386/kernel/pci-dma.c-dist	2004-08-27 18:35:21.005516509 +0200
+++ linux-2.6.9-rc1-mm1/arch/i386/kernel/pci-dma.c	2004-08-27 18:35:50.961770169 +0200
@@ -25,10 +25,9 @@ void *dma_alloc_coherent(struct device *
 			   dma_addr_t *dma_handle, int gfp)
 {
 	void *ret;
+	unsigned long mask;
 	struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
 	int order = get_order(size);
-	/* ignore region specifiers */
-	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
 
 	if (mem) {
 		int page = bitmap_find_free_region(mem->bitmap, mem->size,
@@ -43,15 +42,27 @@ void *dma_alloc_coherent(struct device *
 			return NULL;
 	}
 
-	if (dev == NULL || (dev->coherent_dma_mask < 0xffffffff))
-		gfp |= GFP_DMA;
-
-	ret = (void *)__get_free_pages(gfp, order);
+	gfp &= ~__GFP_HIGHMEM;
+	if (dev)
+		mask = dev->coherent_dma_mask;
+	else {
+		mask = ~0UL; /* doesn't matter */
+		gfp |= __GFP_DMA;
+	}
 
-	if (ret != NULL) {
-		memset(ret, 0, size);
-		*dma_handle = virt_to_phys(ret);
+	for (;;) {
+		ret = (void *)__get_free_pages(gfp, order);
+		if (ret) {
+			*dma_handle = virt_to_phys(ret);
+			if (! (((unsigned long)*dma_handle + size - 1) & ~mask))
+				break;
+			free_pages((unsigned long)ret, order);
+		}
+		if (gfp & __GFP_DMA)
+			return NULL;
+		gfp |= __GFP_DMA;
 	}
+	memset(ret, 0, size);
 	return ret;
 }
 
--- linux-2.6.9-rc1-mm1/include/asm-ppc/dma-mapping.h-dist	2004-08-24 09:03:30.000000000 +0200
+++ linux-2.6.9-rc1-mm1/include/asm-ppc/dma-mapping.h	2004-08-27 18:35:54.258027801 +0200
@@ -67,19 +67,30 @@ static inline void *dma_alloc_coherent(s
 	return __dma_alloc_coherent(size, dma_handle, gfp);
 #else
 	void *ret;
-	/* ignore region specifiers */
-	gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);
+	unsigned long mask;
+ 	int order = get_order(size);
 
-	if (dev == NULL || dev->coherent_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_bus(ret);
+	gfp &= ~__GFP_HIGHMEM;
+	if (dev)
+		mask = dev->coherent_dma_mask;
+	else {
+		mask = ~0UL; /* doesn't matter */
+		gfp |= __GFP_DMA;
 	}
 
+	for (;;) {
+		ret = (void *)__get_free_pages(gfp, order);
+		if (ret) {
+			*dma_handle = virt_to_bus(ret);
+			if (! (((unsigned long)*dma_handle + size - 1) & ~mask))
+				break;
+			free_pages((unsigned long)ret, order);
+		}
+		if (gfp & __GFP_DMA)
+			return NULL;
+		gfp |= __GFP_DMA;
+	}
+	memset(ret, 0, size);
 	return ret;
 #endif
 }
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"aart@kvack.org"> aart@kvack.org </a>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-08-27 16:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-27 16:52 [PATCH] DMA allocation improvement Takashi Iwai
2004-08-27 16:55 ` [PATCH] ALSA hack clean up (DMA allocation improvement) Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox