* [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* [PATCH] ALSA hack clean up (DMA allocation improvement)
2004-08-27 16:52 [PATCH] DMA allocation improvement Takashi Iwai
@ 2004-08-27 16:55 ` Takashi Iwai
0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2004-08-27 16:55 UTC (permalink / raw)
To: linux-mm
Hi,
The patch below removes the hack in ALSA memory allocator,
which does basically the same thing with my last patch for DMA
allocation improvement.
Signed-off-by: Takashi Iwai <tiwai@suse.de>
--- linux-2.6.9-rc1-mm1/sound/core/memalloc.c-dist 2004-08-27 18:36:06.073366755 +0200
+++ linux-2.6.9-rc1-mm1/sound/core/memalloc.c 2004-08-27 18:36:39.700792778 +0200
@@ -90,67 +90,6 @@ struct snd_mem_list {
#define snd_assert(expr, args...) /**/
#endif
-/*
- * Hacks
- */
-
-#if defined(__i386__) || defined(__ppc__) || defined(__x86_64__)
-/*
- * A hack to allocate large buffers via dma_alloc_coherent()
- *
- * since dma_alloc_coherent always tries GFP_DMA when the requested
- * pci memory region is below 32bit, it happens quite often that even
- * 2 order of pages cannot be allocated.
- *
- * so in the following, we allocate at first without dma_mask, so that
- * allocation will be done without GFP_DMA. if the area doesn't match
- * with the requested region, then realloate with the original dma_mask
- * again.
- *
- * Really, we want to move this type of thing into dma_alloc_coherent()
- * so dma_mask doesn't have to be messed with.
- */
-
-static void *snd_dma_hack_alloc_coherent(struct device *dev, size_t size,
- dma_addr_t *dma_handle, int flags)
-{
- void *ret;
- u64 dma_mask, coherent_dma_mask;
-
- if (dev == NULL || !dev->dma_mask)
- return dma_alloc_coherent(dev, size, dma_handle, flags);
- dma_mask = *dev->dma_mask;
- coherent_dma_mask = dev->coherent_dma_mask;
- *dev->dma_mask = 0xffffffff; /* do without masking */
- dev->coherent_dma_mask = 0xffffffff; /* do without masking */
- ret = dma_alloc_coherent(dev, size, dma_handle, flags);
- *dev->dma_mask = dma_mask; /* restore */
- dev->coherent_dma_mask = coherent_dma_mask; /* restore */
- if (ret) {
- /* obtained address is out of range? */
- if (((unsigned long)*dma_handle + size - 1) & ~dma_mask) {
- /* reallocate with the proper mask */
- dma_free_coherent(dev, size, ret, *dma_handle);
- ret = dma_alloc_coherent(dev, size, dma_handle, flags);
- }
- } else {
- /* wish to success now with the proper mask... */
- if (dma_mask != 0xffffffffUL) {
- /* allocation with GFP_ATOMIC to avoid the long stall */
- flags &= ~GFP_KERNEL;
- flags |= GFP_ATOMIC;
- ret = dma_alloc_coherent(dev, size, dma_handle, flags);
- }
- }
- return ret;
-}
-
-/* redefine dma_alloc_coherent for some architectures */
-#undef dma_alloc_coherent
-#define dma_alloc_coherent snd_dma_hack_alloc_coherent
-
-#endif /* arch */
-
#if ! defined(__arm__)
#define NEED_RESERVE_PAGES
#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