genalloc for 2.6.12-rc-mm3
PATCH 1/1 genalloc for 2.6.12-rc-mm3
Hi Andrew,
This patch provides the generic allocator needed for the ia64 mspec driver. Any chance you could add it to the mm tree?
Thanks,
Jes
Generic allocator that can be used by device driver to manage special memory etc. in particular it's used to manage uncached memory on ia64 for the mspec driver. The allocator is based on the allocator from the sym53c8xx_2 driver.
include/linux/genalloc.h +46 -0
@@ -0,0 +1,46 @@ +/* +* Basic general purpose allocator for managing special purpose memory +* not managed by the regular kmalloc/kfree interface. +* Uses for this includes on-device special memory, uncached memory +* etc. +* +* This code is based on the buddy allocator found in the sym53c8xx_2 +* driver, adapted for general purpose use. +* +* This source code is licensed under the GNU General Public License, +* Version 2. See the file COPYING for more details. +*/ + +#include <linux/spinlock.h> + +#define ALLOC_MIN_SHIFT 5/* 32 bytes minimum */ +/* +* Link between free memory chunks of a given size. +*/ +struct gen_pool_link { +struct gen_pool_link *next; +}; + +/* +* Memory pool of a given kind. +* Ideally, we want to use: +* 1) 1 pool for memory we donnot need to involve in DMA. +* 2) The same pool for controllers that require same DMA +* constraints and features. +* The OS specific m_pool_id_t thing and the gen_pool_match() +* method are expected to tell the driver about. +*/ +struct gen_pool { +spinlock_t lock; +unsigned long (*get_new_chunk)(struct gen_pool *); +struct gen_pool *next; +struct gen_pool_link *h; +unsigned long private; +int max_chunk_shift; +}; + +unsigned long gen_pool_alloc (struct gen_pool *poolp,int size); +void gen_pool_free (struct gen_pool *mp,unsigned long ptr,int size); +struct gen_pool *alloc_gen_pool (int nr_chunks,int max_chunk_shift, +unsigned long (*fp)(struct gen_pool *), +unsigned long data);
init/main.c +4 -0
@@ -78,6 +78,7 @@static int init (void *); +extern void gen_pool_init (void );extern void init_IRQ (void );extern void sock_init (void );extern void fork_init (unsigned long );
@@ -489,6 +490,9 @@#endif vfs_caches_init_early ();mem_init (); +#ifdef CONFIG_GENERIC_ALLOCATOR +gen_pool_init (); +#endif kmem_cache_init ();numa_policy_init ();if (late_time_init)
lib/Kconfig +6 -0
@@ -40,6 +40,12 @@ tristate # +# Generic allocator support is selected if needed +# +config GENERIC_ALLOCATOR + boolean + +# # reed solomon support is select'ed if needed # config REED_SOLOMON
lib/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_CRC32) += crc32.o obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_GENERIC_IOMAP) += iomap.o +obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/ obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
lib/genalloc.c +220 -0
220 lines not on this page · raw message
Quoted 5 lines · Jes Sorensen
Hi Andrew,
This patch provides the generic allocator needed for the ia64 mspec driver. Any chance you could add it to the mm tree?
Quoted 7 lines
Thanks,
JesGeneric allocator that can be used by device driver to manage special memory etc. in particular it's used to manage uncached memory on ia64 for the mspec driver. The allocator is based on the allocator from the
sym53c8xx_2driver.
So maybe as an example that your driver is usefull and not just additional bloat you could convert sym53c8xx_2 (and ncr53c8xxx) to use it?
Quoted 9 lines
+/*
+ * Memory pool of a given kind. + * Ideally, we want to use: + * 1) 1 pool for memory we donnot need to involve in DMA. + * 2) The same pool for controllers that require same DMA + * constraints and features. + * The OS specific m_pool_id_t thing and the gen_pool_match() + * method are expected to tell the driver about.+ */
these comments don't make any sense.
include/linux/genalloc.h line 46
Quoted 5 lines
include/linux/genalloc.h +5
-0+unsigned long gen_pool_alloc (struct gen_pool *poolp,int size); +void gen_pool_free (struct gen_pool *mp,unsigned long ptr,int size); +struct gen_pool *alloc_gen_pool (int nr_chunks,int max_chunk_shift, +unsigned long (*fp)(struct gen_pool *), +unsigned long data);
shouldn't there be a way to release the pool again? Also we usu�lly call these _create/_destroy
init/main.c line 495
Quoted 3 lines
init/main.c +3
-0+#ifdef CONFIG_GENERIC_ALLOCATOR +gen_pool_init (); +#endif
please avoid hardcoded initcalls.
lib/Kconfig line 46
Quoted 2 lines
lib/Kconfig +2
-0+config GENERIC_ALLOCATOR + boolean
bool
Quoted 1 line
+#include <
linux/config.h>
not needed.
Quoted 9 lines
lib/genalloc.c +9
-0+#include <linux/module.h> +#include <linux/stddef.h> +#include <linux/kernel.h> +#include <linux/string.h> +#include <linux/slab.h> +#include <linux/init.h> +#include <linux/mm.h> +#include <linux/spinlock.h> +#include <linux/genalloc.h>
lib/genalloc.c line 45
Quoted 10 lines
lib/genalloc.c +9
-0+ +#include <asm/page.h> +#include <asm/pal.h> + + +/* +* This is really an arbitrary limit, +10 is enough for +* IA64_GRANULE_SHIFT. +*/
What's IA64_GRANULE_SHIFT and why do we care?
lib/genalloc.c line 109
Quoted 3 lines
lib/genalloc.c +3
-0+#if DEBUG +printk (KERN_DEBUG "gen_pool_alloc: s %02x, i %i, h %p\n" , s, i, h); +#endif
please avoid ifdefs in the middle of the code. if you think keeping this trivial debug code in is so valueable add a helper that gets defined away for the non-debug case.
lib/genalloc.c line 216
Quoted 5 lines
lib/genalloc.c +5
-0+int __init gen_pool_init (void ) +{ +printk (KERN_INFO "Generic memory pool allocator v1.0\n" ); +return 0 ; +}
no need to print a init message for a set of trivial library function
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Christoph" == Christoph Hellwig <[email protected]> writes:
Christoph> On Tue, Apr 12, 2005 at 05:55:01AM -0400, Jes Sorensen
Quoted 4 lines · Christoph> wrote:
Quoted 4 lines
Generic allocator that can be used by device driver to manage special memory etc. in particular it's used to manage uncached memory on ia64 for the mspec driver. The allocator is based on the allocator from the
sym53c8xx_2driver.
Thanks for the comments, I'm working my way through them. I'll post a patch in response to Andrew's comments with all the changes.
Christoph> So maybe as an example that your driver is usefull and not Christoph> just additional bloat you could convert sym53c8xx_2 (and Christoph> ncr53c8xxx) to use it?
I know Matthew has been making a lot of changes to the sym2 driver and I don't really want to get in his way at the moment.
Quoted 6 lines
Quoted 6 lines
+/* + * Memory pool of a given kind. + * Ideally, we want to use:
+ * 1) 1 pool for memory we donnot need to involve in DMA. + * 2) The same pool for controllers that require same DMA + * constraints and features. + * The OS specificm_pool_id_tthing and thegen_pool_match()+ * method are expected to tell the driver about.
+ */
Christoph> these comments don't make any sense.
They're now gone ;)
Quoted 5 lines
Quoted 5 lines
(quoted diff) +2
-0+unsigned long gen_pool_alloc(struct gen_pool *poolp, int size); +void gen_pool_free(struct gen_pool *mp, unsigned long ptr, intsize); +struct
gen_pool*alloc_gen_pool(intnr_chunks, intmax_chunk_shift, + unsigned long (*fp)(structgen_pool*), + unsigned long data);
Christoph> shouldn't there be a way to release the pool again? Also Christoph> we usu�lly call these _create/_destroy
Changed it to _create. Doing a _destroy doesn't make all that much sense, in most cases the memory in use wouldn't come from the kmalloc pool but either be device special memory or memory converted from cached to uncached on ia64 which isn't trivial to take back and forth. It could be done but the practical use for it would be very limited imho.
Quoted 1 line
Quoted 1 line
+#ifdef
CONFIG_GENERIC_ALLOCATOR+gen_pool_init(); +#endif
Christoph> please avoid hardcoded initcalls.
It has to be done prior to potential users.
Quoted 1 line
Quoted 1 line
+#include <
linux/config.h>
Christoph> not needed.
Gone
Quoted 8 lines
Quoted 8 lines
+#include <
linux/module.h> +#include <linux/stddef.h> +#include <linux/kernel.h> +#include <linux/string.h> +#include <linux/slab.h> +#include <linux/init.h> +#include <linux/mm.h>(quoted diff) +3
-0+#include <linux/spinlock.h> +#include <linux/genalloc.h> + +#include <asm/page.h> +#include <asm/pal.h> + + + /* + * This is really an arbitrary limit, +10 is enough for + *
IA64_GRANULE_SHIFT. + */
Christoph> What's IA64_GRANULE_SHIFT and why do we care?
IT's just an arbitrary upper limit of what the allocator allows, ie. 16MB chunks. I added a better comment.
Quoted 2 lines
Quoted 2 lines
+#if DEBUG +
printk(KERN_DEBUG "gen_pool_alloc: s %02x, i %i, h %p\n", s, i, h); +#endif
Christoph> please avoid ifdefs in the middle of the code. if you Christoph> think keeping this trivial debug code in is so valueable Christoph> add a helper that gets defined away for the non-debug case.
Gone
Quoted 2 lines
Quoted 2 lines
+int
__initgen_pool_init(void)+{ +printk(KERN_INFO "Generic memory pool allocator v1.0\n"); + return 0; +}
Christoph> no need to print a init message for a set of trivial Christoph> library function
Gone, I put it in initially for debugging.
Cheers,
Jes
Quoted 5 lines · [email protected] (Jes Sorensen) wrote:
Hi Andrew,
This patch provides the generic allocator needed for the ia64 mspec driver. Any chance you could add it to the mm tree?
spose so. Glad it's Kconfigurable.
init/main.c line 495
Quoted 3 lines
init/main.c +3
-0+#ifdef CONFIG_GENERIC_ALLOCATOR +gen_pool_init (); +#endif
Suggest you put a !CONFIG_GENERIC_ALLOCATOR stub in genpool.h, remove these ifdefs.
lib/Kconfig line 46
Quoted 4 lines
lib/Kconfig +4
-0+# Generic allocator support is selected if needed +# +config GENERIC_ALLOCATOR + boolean
This will be turned on by some later patch, yes?
So will this code even be compiled in -mm? I guess allyesconfig will enable it.
lib/genalloc.c line 36
Quoted 4 lines
lib/genalloc.c +4
-0+ +struct gen_pool *alloc_gen_pool (int nr_chunks,int max_chunk_shift, +unsigned long (*fp)(struct gen_pool *), +unsigned long data)
Some API kerneldocs would be useful.
lib/genalloc.c line 48
Quoted 7 lines
lib/genalloc.c +7
-0+/* +* This is really an arbitrary limit, +10 is enough for +* IA64_GRANULE_SHIFT. +*/ +if ((max_chunk_shift > (PAGE_SHIFT +10 )) || + ((max_chunk_shift <ALLOC_MIN_SHIFT ) && max_chunk_shift)) +return NULL ;
Does this ia64ism restrict the usefulness of genalloc in any way, or is the comment stale?
Quoted 10 lines
+ * Simple power of two buddy-like generic allocator.
+ * Provides naturally aligned memory chunks.
+ */lib/genalloc.c +7
-0+unsigned long gen_pool_alloc (struct gen_pool *poolp,int size) +{ +int j, i, s, max_chunk_size; +unsigned long a, flags; +struct gen_pool_link *h = poolp->h; + + max_chunk_size =1 << poolp->max_chunk_shift;
lib/genalloc.c line 105
Quoted 10 lines
lib/genalloc.c +10
-0+ +if (size > max_chunk_size) +return 0 ; + + i =0 ; + s = (1 <<ALLOC_MIN_SHIFT ); +while (size > s) { + s <<=1 ; + i++; + }
roundup_pow_of_two()?
lib/genalloc.c line 109
Quoted 3 lines
lib/genalloc.c +3
-0+#if DEBUG +printk (KERN_DEBUG "gen_pool_alloc: s %02x, i %i, h %p\n" , s, i, h); +#endif
dprintk?
lib/genalloc.c line 118
Quoted 8 lines
lib/genalloc.c +8
-0+ j = i; + +spin_lock_irqsave (&poolp->lock, flags); +while (!h[j].next) { +if (s == max_chunk_size) { +struct gen_pool_link *ptr; +spin_unlock_irqrestore (&poolp->lock, flags); + ptr = (struct gen_pool_link *)poolp->get_new_chunk (poolp);
mabe get_new_chunk() should return void*, avoid the casting?
lib/genalloc.c line 142
Quoted 3 lines
lib/genalloc.c +3
-0+#if DEBUG +printk (KERN_DEBUG "gen_pool_alloc() splitting i %i j %i %x a %02lx\n" , i, j, s, a); +#endif
You once sent me a rude email for putting a line >80 cols into acenic.c
lib/genalloc.c line 180
Quoted 7 lines
lib/genalloc.c +7
-0+return ; + + i =0 ; +while (size > s) { + s <<=1 ; + i++; + }
roundup_pow_of_two()?
lib/genalloc.c line 196
Quoted 3 lines
lib/genalloc.c +3
-0+while (q->next && q->next != (struct gen_pool_link *)b) { + q = q->next; + }
braces?
lib/genalloc.c line 215
Quoted 4 lines
lib/genalloc.c +4
-0+int __init gen_pool_init (void ) +{ +printk (KERN_INFO "Generic memory pool allocator v1.0\n" ); +return 0 ;
Do we need the printk?
lib/genalloc.c line 220
Quoted 4 lines
lib/genalloc.c +4
-0+ +EXPORT_SYMBOL (alloc_gen_pool); +EXPORT_SYMBOL (gen_pool_alloc); +EXPORT_SYMBOL (gen_pool_free);
Current style is usually to put the exports at the line after the function's closing brace. I prefer that personally - it's easier to locate.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Andrew" == Andrew Morton <[email protected]> writes:
Andrew> [email protected] (Jes Sorensen) wrote:
Andrew> Suggest you put a !CONFIG_GENERIC_ALLOCATOR stub in genpool.h, Andrew> remove these ifdefs.
Gone, used to be a time when I thought it was needed to have the init function ... oh well.
Quoted 2 lines
Quoted 2 lines
+# Generic allocator support is selected if needed +# +config
GENERIC_ALLOCATOR+ boolean
Andrew> This will be turned on by some later patch, yes?
The mspec patch enables it. I don't think this should be a make config visible option, but rather be enabled by other drivers etc that needs it.
Andrew> So will this code even be compiled in -mm? I guess Andrew> allyesconfig will enable it.
If you enable the mspec driver and compile for ia64 it will.
Andrew> Some API kerneldocs would be useful.
Documentation, you're asking a lot ;-) I'll look into adding something in a seperate patch - that'll take a bit longer.
Quoted 1 line
Quoted 1 line
IA64_GRANULE_SHIFT. + */ + if ((max_chunk_shift> (PAGE_SHIFT+
Andrew> Does this ia64ism restrict the usefulness of genalloc in any Andrew> way, or is the comment stale?
Nope, it was just an arbitrary way to set a maximum upper limit for the chunks. We can always crank it up if someone needs to be able to allocate larger chunks (all it costs is a few extra entries in the table).
Andrew> roundup_pow_of_two()?
Didn't know that one, fixed.
Andrew> dprintk?
Leftover code from the sym allocator, removed.
Andrew> mabe get_new_chunk() should return void*, avoid the casting?
I went back and reviewed this and it's sorta a win-one lose-one situation. If I make it return void * there's other places where it'll need to be cast the other way. I can change it but I don't think there's a real win by doing so.
Andrew> You once sent me a rude email for putting a line >80 cols into Andrew> acenic.c
Me? Never! ;-) Will fix. Ok, this dogfood of mine is quite tasty ....
Quoted 2 lines
Quoted 2 lines
+ +EXPORT_SYMBOL(alloc_gen_pool); +EXPORT_SYMBOL(gen_pool_alloc); +EXPORT_SYMBOL(gen_pool_free);
Andrew> Current style is usually to put the exports at the line after Andrew> the function's closing brace. I prefer that personally - it's Andrew> easier to locate.
Hmmm I always thought was the other way. I kinda prefer it that way, but I am not overly biased. Fixed.
How does this version look?
Cheers,
Jes
Generic allocator that can be used by device driver to manage special memory etc. in particular it's used to manage uncached memory on ia64 for the mspec driver. The allocator is based on the allocator from the sym53c8xx_2 driver.
include/linux/genalloc.h +40 -0
@@ -0,0 +1,40 @@ +/* +* Basic general purpose allocator for managing special purpose memory +* not managed by the regular kmalloc/kfree interface. +* Uses for this includes on-device special memory, uncached memory +* etc. +* +* This code is based on the buddy allocator found in the sym53c8xx_2 +* driver, adapted for general purpose use. +* +* This source code is licensed under the GNU General Public License, +* Version 2. See the file COPYING for more details. +*/ + +#include <linux/spinlock.h> + +#define ALLOC_MIN_SHIFT 5/* 32 bytes minimum */ +/* +* Link between free memory chunks of a given size. +*/ +struct gen_pool_link { +struct gen_pool_link *next; +}; + +/* +* Memory pool descriptor. +*/ +struct gen_pool { +spinlock_t lock; +unsigned long (*get_new_chunk)(struct gen_pool *); +struct gen_pool *next; +struct gen_pool_link *h; +unsigned long private; +int max_chunk_shift; +}; + +unsigned long gen_pool_alloc (struct gen_pool *poolp,int size); +void gen_pool_free (struct gen_pool *mp,unsigned long ptr,int size); +struct gen_pool *gen_pool_create (int nr_chunks,int max_chunk_shift, +unsigned long (*fp)(struct gen_pool *), +unsigned long data);
lib/Kconfig +6 -0
@@ -40,6 +40,12 @@ tristate # +# Generic allocator support is selected if needed +# +config GENERIC_ALLOCATOR + boolean + +# # reed solomon support is select'ed if needed # config REED_SOLOMON
lib/Makefile +1 -0
@@ -29,6 +29,7 @@ obj-$(CONFIG_CRC32) += crc32.o obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_GENERIC_IOMAP) += iomap.o +obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o obj-$(CONFIG_ZLIB_INFLATE) += zlib_inflate/ obj-$(CONFIG_ZLIB_DEFLATE) += zlib_deflate/
lib/genalloc.c +189 -0
189 lines not on this page · raw message
Quoted 1 line
+#include <
asm/pal.h>
this will break on all plattforms except alpha and ia64.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Christoph" == Christoph Hellwig <[email protected]> writes:
+#include <
asm/pal.h>
Christoph> this will break on all plattforms except alpha and ia64.
The driver is located in arch/ia64/kernel/ ;-)
Cheers,
Jes
Quoted 6 lines · Jes Sorensen
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Christoph" == Christoph Hellwig <[email protected]> writes:
+#include <
asm/pal.h>Christoph> this will break on all plattforms except alpha and ia64.
The driver is located in
arch/ia64/kernel/;-)
Above hunk is from lib/genalloc.c
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Christoph" == Christoph Hellwig <[email protected]> writes:
Christoph> On Tue, Apr 12, 2005 at 10:51:20AM -0400, Jes Sorensen
Quoted 3 lines · Christoph> wrote:
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"Christoph" == Christoph Hellwig <[email protected]> writes:
+#include <
asm/pal.h>
Christoph> this will break on all plattforms except alpha and ia64.
Quoted 1 line
Quoted 1 line
The driver is located in
arch/ia64/kernel/;-)
Christoph> Above hunk is from lib/genalloc.c
DOH! I'll send Andrew an updated patch with just this change to keep l-k size down.
Thanks,
Jes