Skip to contentSkip to conversation

lib/genalloc: add a helper function for DMA buffer allocation

PATCH RESEND In review Nicolin Chen 2 participants 3 messages Oct 31, 2013 – Nov 1, 2013 Library code

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 lib/genalloc: add a helper function for DMA buffer allocation

When using pool space for DMA buffer, there might be duplicated calling of gen_pool_alloc() and gen_pool_virt_to_phys() in each implementation.

Thus it's better to add a simple helper function, a compatible one to the common dma_alloc_coherent(), to save some code.

---
 include/linux/genalloc.h |  2 ++
 lib/genalloc.c           | 28 ++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)
include/linux/genalloc.h +2 -0
index f8d41cb..1eda33d 100644
@@ -94,6 +94,8 @@ static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
 }
 extern void gen_pool_destroy(struct gen_pool *);
 extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
+extern void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size,
+		dma_addr_t *dma);
 extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
 extern void gen_pool_for_each_chunk(struct gen_pool *,
 	void (*)(struct gen_pool *, struct gen_pool_chunk *, void *), void *);
lib/genalloc.c +28 -0
index 26cf20b..dda3116 100644
@@ -313,6 +313,34 @@ retry:
 EXPORT_SYMBOL(gen_pool_alloc);
 
 /**
+ * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
+ * @pool: pool to allocate from
+ * @size: number of bytes to allocate from the pool
+ * @dma: dma-view physical address
+ *
+ * Allocate the requested number of bytes from the specified pool.
+ * Uses the pool allocation function (with first-fit algorithm by default).
+ * Can not be used in NMI handler on architectures without
+ * NMI-safe cmpxchg implementation.
+ */
+void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
+{
+	unsigned long vaddr;
+
+	if (!pool)
+		return NULL;
+
+	vaddr = gen_pool_alloc(pool, size);
+	if (!vaddr)
+		return NULL;
+
+	*dma = gen_pool_virt_to_phys(pool, vaddr);
+
+	return (void *)vaddr;
+}
+EXPORT_SYMBOL(gen_pool_dma_alloc);
+
+/**
  * gen_pool_free - free allocated special memory back to the pool
  * @pool: pool to free to
  * @addr: starting address of memory to free back to pool
Signature · 1 line
1.8.4
maintainer
#1 lore raw
Quoted 6 lines · On Thu, 31 Oct 2013 17:16:07 +0800 Nicolin Chen <[email protected]> wrote:

When using pool space for DMA buffer, there might be duplicated calling of gen_pool_alloc() and gen_pool_virt_to_phys() in each implementation.

Thus it's better to add a simple helper function, a compatible one to the common dma_alloc_coherent(), to save some code.

Well OK, but this new function doesn't have any callers. So it increases kernel size and is untestable.

Do you have any conversion patches which we can merge to address these problems?

#2 lore raw

Sir,

Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Thu, 31 Oct 2013 17:16:07 +0800 Nicolin Chen <[email protected]> wrote:

When using pool space for DMA buffer, there might be duplicated calling of gen_pool_alloc() and gen_pool_virt_to_phys() in each implementation.

Thus it's better to add a simple helper function, a compatible one to the common dma_alloc_coherent(), to save some code.

Quoted 7 lines

Well OK, but this new function doesn't have any callers. So it increases kernel size and is untestable.

Do you have any conversion patches which we can merge to address these problems?

I'll later send a patch to convert all the combinations of gen_pool_alloc() and gen_pool_virt_to_phys(), within the entire kernel, to this helper func.

This would need quite a lot people engaged to test it.

If anything unexpected happens during the test, we can refine and fix it. Surely, if some owners of modified driver don't want their code be altered, I can drop those modification to their drivers.

Is this okay?

Thank you,
Nicolin Chen