generic device DMA (dma_pool update)
Subsystems (6)
Quoted 4 lines · David Brownell
struct
dma_pool*dma_pool_create(char *, struct device *,size_t) voiddma_pool_destroy(structdma_pool*pool)
void *dma_pool_alloc(structdma_pool*, intmem_flags,dma_addr_t*) voiddma_pool_free(struct dma_pool *, void *, dma_addr_t)
I would like to be able to have failure-free, deadlock-free
blocking memory allocation, such as we have with the non-DMA mempool library so that we can guarantee that drivers that have been successfully initialized will continue to work regardless of memory pressure, and reduce error branches that drivers have to deal with.
Such a facility could be layered on top of your interface
perhaps by extending the mempool code to pass an extra parameter around. If so, then you should think about arranging your interface so that it could be driven with as little glue as possible by mempool.
I think that the term "pool" is more descriptively used by
mempool and more misleadningly used by the pci_pool code, as there is no guaranteed pool being reserved in the pci_pool code. Alas, I don't have a good alternative term to suggest at the moment.
Adam J. Richter __ ______________ 575 Oroville Road [email protected] \ / Milpitas, California 95035 +1 408 309-6081 | g g d r a s i l United States of America
"Free Software For The Rest Of Us."
Quoted 14 lines · Adam J. Richter
Quoted 4 lines · David Brownell
struct
dma_pool*dma_pool_create(char *, struct device *,size_t) voiddma_pool_destroy(structdma_pool*pool)
void *dma_pool_alloc(structdma_pool*, intmem_flags,dma_addr_t*) voiddma_pool_free(struct dma_pool *, void *, dma_addr_t)I would like to be able to have failure-free, deadlock-free
blocking memory allocation, such as we have with the non-DMA mempool library so that we can guarantee that drivers that have been successfully initialized will continue to work regardless of memory pressure, and reduce error branches that drivers have to deal with.
Quoted 5 lines
Such a facility could be layered on top of your interface
perhaps by extending the mempool code to pass an extra parameter around. If so, then you should think about arranging your interface so that it could be driven with as little glue as possible by mempool.
What is that parameter? The size, I assume. To do that you'd
need to create different pools for different allocation sizes.
Bear in mind that mempool only makes sense with memory objects which have the special characteristic that "if you wait long enough,
someone will free one". ie: BIOs, nfs requests, etc. Probably,
DMA buffers fit into that picture as well.
If anything comes out of this discussion, please let it be the removal of the hard-wired GFP_ATOMIC in dma_alloc_coherent. That's quite broken - the interface should (always) be designed so that the caller can pass in the gfp flags (__GFP_WAIT,__GFP_IO,__GFP_FS, at least)
Quoted 10 lines
Quoted 6 lines
Such a facility could be layered on top of your interface
perhaps by extending the mempool code to pass an extra parameter around. If so, then you should think about arranging your interface so that it could be driven with as little glue as possible by mempool.What is that parameter? The size, I assume. To do that you'd
need to create different pools for different allocation sizes.
In the other allocators it'd be the dma_addr_t for the memory being returned ...
I don't think the mempool stuff needs that, see the fragments below.
Quoted 4 lines
Bear in mind that mempool only makes sense with memory objects
which have the special characteristic that "if you wait long enough,
someone will free one". ie: BIOs, nfs requests, etc. Probably,
DMA buffers fit into that picture as well.Inside the USB host controller drivers I think that mostly applies
to transfer descriptors (tds), which are freed when some other request completes. An 8K buffer takes 1 (ehci), 2 (ohci) or 128 (uhci) of those, and as you know scatterlists can queue quite a few pages.
I'd imagine mempool based td allocation might go like this; it should be easy enough to slip into most of the HCDs:
void *mempool_alloc_td (int mem_flags, void *pool)
{
struct td *td;
dma_addr_t dma;
td = dma_pool_alloc (pool, mem_flags, &dma);
if (!td)
return td;
td->td_dma = dma; /* feed to the hardware */
... plus other init
return td;
}
void mempool_free_td (void *_td, void *pool)
{
struct td *td = _td;
dma_pool_free (pool, td, td->dma);
}USB device drivers tend to either allocate and reuse one dma buffer (kmalloc/kfree usage pattern) or use dma mapping ... so far.
- Dave
Quoted 13 lines · David Brownell
void *mempool_alloc_td (int mem_flags, void *pool)
{
struct td *td;
dma_addr_t dma;
td = dma_pool_alloc (pool, mem_flags, &dma);
if (!td)
return td;
td->td_dma = dma; /* feed to the hardware */
... plus other init
return td;
}The existing mempool code can be used to implement this, I believe. The pool->alloc callback is passed an opaque void *, and it returns a void * which can point at any old composite caller-defined blob.
Quoted 4 lines · Adam J. Richter
I think that the term "pool" is more descriptively used by
mempool and more misleadningly used by thepci_poolcode, as there is no guaranteed pool being reserved in thepci_poolcode. Alas, I don't have a good alternative term to suggest at the moment.
FWIW pci_pool predates mempool by quite a bit (2.4.early vs 2.5.later), and I don't think I've noticed any correlation between allocation using the "pool" word and reserving memory ... so I thought it was "mempool" that clashed. No big deal IMO, "all the good words are taken".
I seem to recall it was a portability issue that made pci_pool never release pages once it allocates them ... some platform couldn't cope with pci_free_consistent() being called in_interrupt(). In practice that seems to have been a good enough reservation scheme so far.
- Dave
Yes, that was exactly what I was demonstrating ... those are the callbacks that'd be supplied to mempool_create(), showing that it doesn't need to change for that kind of usage. (Which isn't allocating DMA buffers, note!)
But we still need lower level allocators that are reasonable for use with 1/Nth page allocations ... which isn't a problem that mempool even attempts to solve. Hence dma_pool, in any of its implementations, would go underneath mempool to achieve what Adam was describing (for drivers that need it).
- Dave
Quoted 20 lines · Andrew Morton
Quoted 5 lines · David Brownell
void *mempool_alloc_td (int mem_flags, void *pool) { struct td *td; dma_addr_t dma;Quoted 7 lines
td = dma_pool_alloc (pool, mem_flags, &dma); if (!td) return td; td->td_dma = dma; /* feed to the hardware */ ... plus other init return td; }The existing mempool code can be used to implement this, I believe. The
pool->alloccallback is passed an opaque void *, and it returns a void * which can point at any old composite caller-defined blob.
Quoted 2 lines · [email protected] said:
USB device drivers tend to either allocate and reuse one dma buffer (kmalloc/kfree usage pattern) or use dma mapping ... so far.
Please be careful with this in drivers. Coherent memory can be phenomenally expensive to obtain on some hardware. Sometimes it has to be implemented by turning off caching and globally flushing the tlb. Thus it really makes sense most of the time for drivers to allocate all the coherent memory they need initially and not have it go through the usual memory allocate/free cycle except under extreme conditions. That's sort really what both pci_pool and mempool aim for.
James
Quoted 5 lines · [email protected] said:
If anything comes out of this discussion, please let it be the removal of the hard-wired
GFP_ATOMICindma_alloc_coherent. That's quite broken - the interface should (always) be designed so that the caller can pass in the gfp flags (__GFP_WAIT,__GFP_IO,__GFP_FS, at least)
How about the attached? I'll also make the changes for parisc (any arch maintainers who just implemented the dma_ API are going to be annoyed about this change, though).
James
# This is a BitKeeper generated patch for the following project:
# Project Name: Linux kernel tree
# This patch format is intended for GNU patch command version 2.5 or higher. # This patch includes the following deltas:
# ChangeSet 1.930 -> 1.934
# drivers/scsi/53c700.c 1.21 -> 1.22
# include/asm-generic/pci-dma-compat.h 1.2 -> 1.3
# arch/i386/kernel/pci-dma.c 1.9 -> 1.11
# Documentation/DMA-API.txt 1.1 -> 1.3
# include/asm-generic/dma-mapping.h 1.2 -> 1.3
# include/asm-i386/dma-mapping.h 1.1 -> 1.3
#
# The following is the BitKeeper ChangeSet Log
# --------------------------------------------
# 02/12/31 [email protected] 1.931
# add GFP_ flag to dma_alloc_[non]coherent # -------------------------------------------- # 03/01/01 [email protected] 1.932 # update noncoherent #define for flag # -------------------------------------------- # 03/01/01 [email protected] 1.933 # tidy up docs and flags # -------------------------------------------- # 03/01/01 [email protected] 1.934 # update generic prototype for gfp flag # -------------------------------------------- # diff -Nru a/Documentation/DMA-API.txt b/Documentation/DMA-API.txt --- a/Documentation/DMA-API.txt Wed Jan 1 11:08:55 2003 +++ b/Documentation/DMA-API.txt Wed Jan 1 11:08:55 2003 @@ -22,7 +22,7 @@
void *
dma_alloc_coherent(struct device *dev, size_t size,
- dma_addr_t *dma_handle)
+ dma_addr_t *dma_handle, int flag)
void *
pci_alloc_consistent(struct pci_dev *dev, size_t size,
dma_addr_t *dma_handle)
@@ -43,6 +43,12 @@
minimum allocation length may be as big as a page, so you should consolidate your requests for consistent memory as much as possible.
+The flag parameter (dma_alloc_coherent only) allows the caller to +specify the GFP_ flags (see kmalloc) for the allocation (the +implementation may chose to ignore flags that affect the location of +the returned memory, like GFP_DMA). For pci_alloc_consistent, you +must assume GFP_ATOMIC behaviour.
+
void
dma_free_coherent(struct device *dev, size_t size, void *cpu_addr
dma_addr_t dma_handle)
@@ -261,7 +267,7 @@
void *
dma_alloc_noncoherent(struct device *dev, size_t size,
- dma_addr_t *dma_handle)
+ dma_addr_t *dma_handle, int flag)
Identical to dma_alloc_coherent() except that the platform will choose to return either consistent or non-consistent memory as it sees
arch/i386/kernel/pci-dma.c +4 -2
@@ -14,10 +14,12 @@#include <asm/io.h> void *dma_alloc_coherent (struct device *dev,size_t size, -dma_addr_t *dma_handle) +dma_addr_t *dma_handle,int gfp) {void *ret; -int gfp =GFP_ATOMIC ; + +/* ignore region specifiers */ + gfp &= ~(__GFP_DMA | __GFP_HIGHMEM);if (dev ==NULL || ((u32)*dev->dma_mask !=0xffffffff )) gfp |=GFP_DMA ;
drivers/scsi/53c700.c +1 -1
@@ -246,7 +246,7 @@int j; memory =dma_alloc_noncoherent (hostdata->dev,TOTAL_MEM_SIZE , - &pScript); + &pScript,GFP_KERNEL );if (memory ==NULL ) {printk (KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n" );return NULL ;
include/asm-generic/dma-mapping.h +4 -3
@@ -30,9 +30,10 @@ }static inline void * -dma_alloc_coherent (struct device *dev,size_t size,dma_addr_t *dma_handle) +dma_alloc_coherent (struct device *dev,size_t size,dma_addr_t *dma_handle, +int flag) { -BUG_ON (dev->bus != &pci_bus_type); +BUG_ON (dev->bus != &pci_bus_type || (flag &GFP_ATOMIC ) !=GFP_ATOMIC );return pci_alloc_consistent (to_pci_dev (dev), size, dma_handle); }
@@ -121,7 +122,7 @@/* Now for the API extensions over the pci_ one */ -#define dma_alloc_noncoherent (d, s, h) dma_alloc_coherent(d, s, h) +#define dma_alloc_noncoherent (d, s, h, f) dma_alloc_coherent(d, s, h, f)#define dma_free_noncoherent (d, s, v, h) dma_free_coherent(d, s, v, h)#define dma_is_consistent (d) (1)
include/asm-generic/pci-dma-compat.h +1 -1
@@ -19,7 +19,7 @@ pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle) { - return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle); + return dma_alloc_coherent(hwdev == NULL ? NULL : &hwdev->dev, size, dma_handle, GFP_ATOMIC); } static inline void
include/asm-i386/dma-mapping.h +2 -2
@@ -3,11 +3,11 @@#include <asm/cache.h> -#define dma_alloc_noncoherent (d, s, h) dma_alloc_coherent(d, s, h) +#define dma_alloc_noncoherent (d, s, h, f) dma_alloc_coherent(d, s, h, f)#define dma_free_noncoherent (d, s, v, h) dma_free_coherent(d, s, v, h)void *dma_alloc_coherent (struct device *dev,size_t size, -dma_addr_t *dma_handle); +dma_addr_t *dma_handle,int flag);void dma_free_coherent (struct device *dev,size_t size,void *vaddr,dma_addr_t dma_handle);