From: Takashi Iwai <tiwai@suse.de>
To: linux-mm@kvack.org
Subject: [PATCH] DMA allocation improvement
Date: Fri, 27 Aug 2004 18:52:58 +0200 [thread overview]
Message-ID: <s5hr7psjzf9.wl@alsa2.suse.de> (raw)
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>
next reply other threads:[~2004-08-27 16:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-08-27 16:52 Takashi Iwai [this message]
2004-08-27 16:55 ` [PATCH] ALSA hack clean up (DMA allocation improvement) Takashi Iwai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=s5hr7psjzf9.wl@alsa2.suse.de \
--to=tiwai@suse.de \
--cc=linux-mm@kvack.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox