From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from hermes.suse.de (hermes-ext.suse.de [195.135.221.8]) (using TLSv1 with cipher EDH-RSA-DES-CBC3-SHA (168/168 bits)) (No client certificate requested) by Cantor.suse.de (Postfix) with ESMTP id BD31DB1AB8C for ; Fri, 27 Aug 2004 18:52:58 +0200 (CEST) Date: Fri, 27 Aug 2004 18:52:58 +0200 Message-ID: From: Takashi Iwai Subject: [PATCH] DMA allocation improvement MIME-Version: 1.0 (generated by SEMI 1.14.5 - "Awara-Onsen") Content-Type: text/plain; charset=US-ASCII Sender: owner-linux-mm@kvack.org Return-Path: To: linux-mm@kvack.org List-ID: 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 --- 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: aart@kvack.org