• Benjamin Gaignard's avatar
    genalloc: make it possible to use a custom allocation algorithm · ca279cf1
    Benjamin Gaignard authored
    Premit use of another algorithm than the default first-fit one.  For
    example a custom algorithm could be used to manage alignment requirements.
    
    As I can't predict all the possible requirements/needs for all allocation
    uses cases, I add a "free" field 'void *data' to pass any needed
    information to the allocation function.  For example 'data' could be used
    to handle a structure where you store the alignment, the expected memory
    bank, the requester device, or any information that could influence the
    allocation algorithm.
    
    An usage example may look like this:
    struct my_pool_constraints {
    	int align;
    	int bank;
    	...
    };
    
    unsigned long my_custom_algo(unsigned long *map, unsigned long size,
    		unsigned long start, unsigned int nr, void *data)
    {
    	struct my_pool_constraints *constraints = data;
    	...
    	deal with allocation contraints
    	...
    	return the index in bitmap where perform the allocation
    }
    
    void create_my_pool()
    {
    	struct my_pool_constraints c;
    	struct gen_pool *pool = gen_pool_create(...);
    	gen_pool_add(pool, ...);
    	gen_pool_set_algo(pool, my_custom_algo, &c);
    }
    
    Add of best-fit algorithm function:
    most of the time best-fit is slower then first-fit but memory fragmentation
    is lower. The random buffer allocation/free tests don't show any arithmetic
    relation between the allocation time and fragmentation but the
    best-fit algorithm
    is sometime able to perform the allocation when the first-fit can't.
    
    This new algorithm help to remove static allocations on ESRAM, a small but
    fast on-chip RAM of few KB, used for high-performance uses cases like DMA
    linked lists, graphic accelerators, encoders/decoders. On the Ux500
    (in the ARM tree) we have define 5 ESRAM banks of 128 KB each and use of
    static allocations becomes unmaintainable:
    cd arch/arm/mach-ux500 && grep -r ESRAM .
    ./include/mach/db8500-regs.h:/* Base address and bank offsets for ESRAM */
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BASE   0x40000000
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK_SIZE      0x00020000
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK0  U8500_ESRAM_BASE
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK1       (U8500_ESRAM_BASE + U8500_ESRAM_BANK_SIZE)
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK2       (U8500_ESRAM_BANK1 + U8500_ESRAM_BANK_SIZE)
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK3       (U8500_ESRAM_BANK2 + U8500_ESRAM_BANK_SIZE)
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_BANK4       (U8500_ESRAM_BANK3 + U8500_ESRAM_BANK_SIZE)
    ./include/mach/db8500-regs.h:#define U8500_ESRAM_DMA_LCPA_OFFSET     0x10000
    ./include/mach/db8500-regs.h:#define U8500_DMA_LCPA_BASE
    (U8500_ESRAM_BANK0 + U8500_ESRAM_DMA_LCPA_OFFSET)
    ./include/mach/db8500-regs.h:#define U8500_DMA_LCLA_BASE U8500_ESRAM_BANK4
    
    I want to use genalloc to do dynamic allocations but I need to be able to
    fine tune the allocation algorithm. I my case best-fit algorithm give
    better results than first-fit, but it will not be true for every use case.
    Signed-off-by: default avatarBenjamin Gaignard <benjamin.gaignard@stericsson.com>
    Cc: Huang Ying <ying.huang@intel.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    ca279cf1
genalloc.c 13.3 KB