2019-06-03 05:44:46 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-06-22 00:15:02 +00:00
|
|
|
/*
|
2011-07-13 05:14:24 +00:00
|
|
|
* Basic general purpose allocator for managing special purpose
|
|
|
|
* memory, for example, memory that is not managed by the regular
|
|
|
|
* kmalloc/kfree interface. Uses for this includes on-device special
|
|
|
|
* memory, uncached memory etc.
|
|
|
|
*
|
|
|
|
* It is safe to use the allocator in NMI handlers and other special
|
|
|
|
* unblockable contexts that could otherwise deadlock on locks. This
|
|
|
|
* is implemented by using atomic operations and retries on any
|
|
|
|
* conflicts. The disadvantage is that there may be livelocks in
|
|
|
|
* extreme cases. For better scalability, one allocator can be used
|
|
|
|
* for each CPU.
|
|
|
|
*
|
|
|
|
* The lockless operation only works if there is enough memory
|
|
|
|
* available. If new memory is added to the pool a lock has to be
|
|
|
|
* still taken. So any user relying on locklessness has to ensure
|
|
|
|
* that sufficient memory is preallocated.
|
|
|
|
*
|
|
|
|
* The basic atomic operation of this allocator is cmpxchg on long.
|
|
|
|
* On architectures that don't have NMI-safe cmpxchg implementation,
|
|
|
|
* the allocator can NOT be used in NMI handler. So code uses the
|
|
|
|
* allocator in NMI handler should depend on
|
|
|
|
* CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
|
2005-06-22 00:15:02 +00:00
|
|
|
*
|
|
|
|
* Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
|
|
|
|
*/
|
|
|
|
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2011-11-17 02:29:17 +00:00
|
|
|
#include <linux/export.h>
|
2009-12-16 00:48:31 +00:00
|
|
|
#include <linux/bitmap.h>
|
2011-07-13 05:14:24 +00:00
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include <linux/interrupt.h>
|
2005-06-22 00:15:02 +00:00
|
|
|
#include <linux/genalloc.h>
|
2013-04-29 23:17:10 +00:00
|
|
|
#include <linux/of_device.h>
|
2019-01-05 21:21:18 +00:00
|
|
|
#include <linux/vmalloc.h>
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2013-09-11 21:21:43 +00:00
|
|
|
static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
|
|
|
|
{
|
|
|
|
return chunk->end_addr - chunk->start_addr + 1;
|
|
|
|
}
|
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
|
|
|
|
{
|
|
|
|
unsigned long val, nval;
|
|
|
|
|
|
|
|
nval = *addr;
|
|
|
|
do {
|
|
|
|
val = nval;
|
|
|
|
if (val & mask_to_set)
|
|
|
|
return -EBUSY;
|
|
|
|
cpu_relax();
|
|
|
|
} while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
|
|
|
|
{
|
|
|
|
unsigned long val, nval;
|
|
|
|
|
|
|
|
nval = *addr;
|
|
|
|
do {
|
|
|
|
val = nval;
|
|
|
|
if ((val & mask_to_clear) != mask_to_clear)
|
|
|
|
return -EBUSY;
|
|
|
|
cpu_relax();
|
|
|
|
} while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bitmap_set_ll - set the specified number of bits at the specified position
|
|
|
|
* @map: pointer to a bitmap
|
|
|
|
* @start: a bit position in @map
|
|
|
|
* @nr: number of bits to set
|
|
|
|
*
|
|
|
|
* Set @nr bits start from @start in @map lock-lessly. Several users
|
|
|
|
* can set/clear the same bitmap simultaneously without lock. If two
|
|
|
|
* users set the same bit, one user will return remain bits, otherwise
|
|
|
|
* return 0.
|
|
|
|
*/
|
|
|
|
static int bitmap_set_ll(unsigned long *map, int start, int nr)
|
|
|
|
{
|
|
|
|
unsigned long *p = map + BIT_WORD(start);
|
|
|
|
const int size = start + nr;
|
|
|
|
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
|
|
|
|
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
|
|
|
|
while (nr - bits_to_set >= 0) {
|
|
|
|
if (set_bits_ll(p, mask_to_set))
|
|
|
|
return nr;
|
|
|
|
nr -= bits_to_set;
|
|
|
|
bits_to_set = BITS_PER_LONG;
|
|
|
|
mask_to_set = ~0UL;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (nr) {
|
|
|
|
mask_to_set &= BITMAP_LAST_WORD_MASK(size);
|
|
|
|
if (set_bits_ll(p, mask_to_set))
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bitmap_clear_ll - clear the specified number of bits at the specified position
|
|
|
|
* @map: pointer to a bitmap
|
|
|
|
* @start: a bit position in @map
|
|
|
|
* @nr: number of bits to set
|
|
|
|
*
|
|
|
|
* Clear @nr bits start from @start in @map lock-lessly. Several users
|
|
|
|
* can set/clear the same bitmap simultaneously without lock. If two
|
|
|
|
* users clear the same bit, one user will return remain bits,
|
|
|
|
* otherwise return 0.
|
|
|
|
*/
|
|
|
|
static int bitmap_clear_ll(unsigned long *map, int start, int nr)
|
|
|
|
{
|
|
|
|
unsigned long *p = map + BIT_WORD(start);
|
|
|
|
const int size = start + nr;
|
|
|
|
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
|
|
|
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
|
|
|
|
|
|
|
while (nr - bits_to_clear >= 0) {
|
|
|
|
if (clear_bits_ll(p, mask_to_clear))
|
|
|
|
return nr;
|
|
|
|
nr -= bits_to_clear;
|
|
|
|
bits_to_clear = BITS_PER_LONG;
|
|
|
|
mask_to_clear = ~0UL;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (nr) {
|
|
|
|
mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
|
|
|
|
if (clear_bits_ll(p, mask_to_clear))
|
|
|
|
return nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-10-02 09:17:01 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_create - create a new special memory pool
|
2006-06-23 09:03:21 +00:00
|
|
|
* @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
|
|
|
|
* @nid: node id of the node the pool structure should be allocated on, or -1
|
2006-10-02 09:17:01 +00:00
|
|
|
*
|
|
|
|
* Create a new special memory pool that can be used to manage special purpose
|
|
|
|
* memory not managed by the regular kmalloc/kfree interface.
|
2006-06-23 09:03:21 +00:00
|
|
|
*/
|
|
|
|
struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
struct gen_pool *pool;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
|
|
|
|
if (pool != NULL) {
|
2011-07-13 05:14:24 +00:00
|
|
|
spin_lock_init(&pool->lock);
|
2006-06-23 09:03:21 +00:00
|
|
|
INIT_LIST_HEAD(&pool->chunks);
|
|
|
|
pool->min_alloc_order = min_alloc_order;
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
pool->algo = gen_pool_first_fit;
|
|
|
|
pool->data = NULL;
|
2015-09-04 22:47:47 +00:00
|
|
|
pool->name = NULL;
|
2006-06-23 09:03:21 +00:00
|
|
|
}
|
|
|
|
return pool;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_create);
|
|
|
|
|
2006-10-02 09:17:01 +00:00
|
|
|
/**
|
2019-06-13 22:56:27 +00:00
|
|
|
* gen_pool_add_owner- add a new chunk of special memory to the pool
|
2006-06-23 09:03:21 +00:00
|
|
|
* @pool: pool to add new memory chunk to
|
2011-05-25 00:13:34 +00:00
|
|
|
* @virt: virtual starting address of memory chunk to add to pool
|
|
|
|
* @phys: physical starting address of memory chunk to add to pool
|
2006-06-23 09:03:21 +00:00
|
|
|
* @size: size in bytes of the memory chunk to add to pool
|
|
|
|
* @nid: node id of the node the chunk structure and bitmap should be
|
|
|
|
* allocated on, or -1
|
2019-06-13 22:56:27 +00:00
|
|
|
* @owner: private data the publisher would like to recall at alloc time
|
2006-10-02 09:17:01 +00:00
|
|
|
*
|
|
|
|
* Add a new chunk of special memory to the specified pool.
|
2011-05-25 00:13:34 +00:00
|
|
|
*
|
|
|
|
* Returns 0 on success or a -ve errno on failure.
|
2005-06-22 00:15:02 +00:00
|
|
|
*/
|
2019-06-13 22:56:27 +00:00
|
|
|
int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t phys,
|
|
|
|
size_t size, int nid, void *owner)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
int nbits = size >> pool->min_alloc_order;
|
|
|
|
int nbytes = sizeof(struct gen_pool_chunk) +
|
genalloc: stop crashing the system when destroying a pool
The genalloc code uses the bitmap API from include/linux/bitmap.h and
lib/bitmap.c, which is based on long values. Both bitmap_set from
lib/bitmap.c and bitmap_set_ll, which is the lockless version from
genalloc.c, use BITMAP_LAST_WORD_MASK to set the first bits in a long in
the bitmap.
That one uses (1 << bits) - 1, 0b111, if you are setting the first three
bits. This means that the API counts from the least significant bits
(LSB from now on) to the MSB. The LSB in the first long is bit 0, then.
The same works for the lookup functions.
The genalloc code uses longs for the bitmap, as it should. In
include/linux/genalloc.h, struct gen_pool_chunk has unsigned long
bits[0] as its last member. When allocating the struct, genalloc should
reserve enough space for the bitmap. This should be a proper number of
longs that can fit the amount of bits in the bitmap.
However, genalloc allocates an integer number of bytes that fit the
amount of bits, but may not be an integer amount of longs. 9 bytes, for
example, could be allocated for 70 bits.
This is a problem in itself if the Least Significat Bit in a long is in
the byte with the largest address, which happens in Big Endian machines.
This means genalloc is not allocating the byte in which it will try to
set or check for a bit.
This may end up in memory corruption, where genalloc will try to set the
bits it has not allocated. In fact, genalloc may not set these bits
because it may find them already set, because they were not zeroed since
they were not allocated. And that's what causes a BUG when
gen_pool_destroy is called and check for any set bits.
What really happens is that genalloc uses kmalloc_node with __GFP_ZERO
on gen_pool_add_virt. With SLAB and SLUB, this means the whole slab
will be cleared, not only the requested bytes. Since struct
gen_pool_chunk has a size that is a multiple of 8, and slab sizes are
multiples of 8, we get lucky and allocate and clear the right amount of
bytes.
Hower, this is not the case with SLOB or with older code that did memset
after allocating instead of using __GFP_ZERO.
So, a simple module as this (running 3.6.0), will cause a crash when
rmmod'ed.
[root@phantom-lp2 foo]# cat foo.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/genalloc.h>
MODULE_LICENSE("GPL");
MODULE_VERSION("0.1");
static struct gen_pool *foo_pool;
static __init int foo_init(void)
{
int ret;
foo_pool = gen_pool_create(10, -1);
if (!foo_pool)
return -ENOMEM;
ret = gen_pool_add(foo_pool, 0xa0000000, 32 << 10, -1);
if (ret) {
gen_pool_destroy(foo_pool);
return ret;
}
return 0;
}
static __exit void foo_exit(void)
{
gen_pool_destroy(foo_pool);
}
module_init(foo_init);
module_exit(foo_exit);
[root@phantom-lp2 foo]# zcat /proc/config.gz | grep SLOB
CONFIG_SLOB=y
[root@phantom-lp2 foo]# insmod ./foo.ko
[root@phantom-lp2 foo]# rmmod foo
------------[ cut here ]------------
kernel BUG at lib/genalloc.c:243!
cpu 0x4: Vector: 700 (Program Check) at [c0000000bb0e7960]
pc: c0000000003cb50c: .gen_pool_destroy+0xac/0x110
lr: c0000000003cb4fc: .gen_pool_destroy+0x9c/0x110
sp: c0000000bb0e7be0
msr: 8000000000029032
current = 0xc0000000bb0e0000
paca = 0xc000000006d30e00 softe: 0 irq_happened: 0x01
pid = 13044, comm = rmmod
kernel BUG at lib/genalloc.c:243!
[c0000000bb0e7ca0] d000000004b00020 .foo_exit+0x20/0x38 [foo]
[c0000000bb0e7d20] c0000000000dff98 .SyS_delete_module+0x1a8/0x290
[c0000000bb0e7e30] c0000000000097d4 syscall_exit+0x0/0x94
--- Exception: c00 (System Call) at 000000800753d1a0
SP (fffd0b0e640) is in userspace
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@linux.vnet.ibm.com>
Cc: Paul Gortmaker <paul.gortmaker@windriver.com>
Cc: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-25 20:37:51 +00:00
|
|
|
BITS_TO_LONGS(nbits) * sizeof(long);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2019-01-03 23:26:51 +00:00
|
|
|
chunk = vzalloc_node(nbytes, nid);
|
2006-06-23 09:03:21 +00:00
|
|
|
if (unlikely(chunk == NULL))
|
2011-05-25 00:13:34 +00:00
|
|
|
return -ENOMEM;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2011-05-25 00:13:34 +00:00
|
|
|
chunk->phys_addr = phys;
|
|
|
|
chunk->start_addr = virt;
|
2013-09-11 21:21:43 +00:00
|
|
|
chunk->end_addr = virt + size - 1;
|
2019-06-13 22:56:27 +00:00
|
|
|
chunk->owner = owner;
|
2017-11-17 23:28:16 +00:00
|
|
|
atomic_long_set(&chunk->avail, size);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
spin_lock(&pool->lock);
|
|
|
|
list_add_rcu(&chunk->next_chunk, &pool->chunks);
|
|
|
|
spin_unlock(&pool->lock);
|
2006-06-23 09:03:21 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
2019-06-13 22:56:27 +00:00
|
|
|
EXPORT_SYMBOL(gen_pool_add_owner);
|
2011-05-25 00:13:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_virt_to_phys - return the physical address of memory
|
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @addr: starting address of memory
|
|
|
|
*
|
|
|
|
* Returns the physical address on success, or -1 on error.
|
|
|
|
*/
|
|
|
|
phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
|
|
|
|
{
|
|
|
|
struct gen_pool_chunk *chunk;
|
2011-07-13 05:14:24 +00:00
|
|
|
phys_addr_t paddr = -1;
|
2011-05-25 00:13:34 +00:00
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
|
2013-09-11 21:21:43 +00:00
|
|
|
if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
|
2011-07-13 05:14:24 +00:00
|
|
|
paddr = chunk->phys_addr + (addr - chunk->start_addr);
|
|
|
|
break;
|
|
|
|
}
|
2011-05-25 00:13:34 +00:00
|
|
|
}
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
2011-05-25 00:13:34 +00:00
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
return paddr;
|
2011-05-25 00:13:34 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_virt_to_phys);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-10-02 09:17:01 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_destroy - destroy a special memory pool
|
2006-10-02 09:17:00 +00:00
|
|
|
* @pool: pool to destroy
|
2006-10-02 09:17:01 +00:00
|
|
|
*
|
|
|
|
* Destroy the specified special memory pool. Verifies that there are no
|
|
|
|
* outstanding allocations.
|
2006-10-02 09:17:00 +00:00
|
|
|
*/
|
|
|
|
void gen_pool_destroy(struct gen_pool *pool)
|
|
|
|
{
|
|
|
|
struct list_head *_chunk, *_next_chunk;
|
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
int order = pool->min_alloc_order;
|
|
|
|
int bit, end_bit;
|
|
|
|
|
|
|
|
list_for_each_safe(_chunk, _next_chunk, &pool->chunks) {
|
|
|
|
chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
|
|
|
|
list_del(&chunk->next_chunk);
|
|
|
|
|
2013-09-11 21:21:43 +00:00
|
|
|
end_bit = chunk_size(chunk) >> order;
|
2006-10-02 09:17:00 +00:00
|
|
|
bit = find_next_bit(chunk->bits, end_bit, 0);
|
|
|
|
BUG_ON(bit < end_bit);
|
|
|
|
|
2019-01-03 23:26:51 +00:00
|
|
|
vfree(chunk);
|
2006-10-02 09:17:00 +00:00
|
|
|
}
|
2015-09-04 22:47:47 +00:00
|
|
|
kfree_const(pool->name);
|
2006-10-02 09:17:00 +00:00
|
|
|
kfree(pool);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_destroy);
|
|
|
|
|
2006-10-02 09:17:01 +00:00
|
|
|
/**
|
2019-06-13 22:56:27 +00:00
|
|
|
* gen_pool_alloc_algo_owner - allocate special memory from the pool
|
2015-11-30 02:48:52 +00:00
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @size: number of bytes to allocate from the pool
|
|
|
|
* @algo: algorithm passed from caller
|
|
|
|
* @data: data passed to algorithm
|
2019-06-13 22:56:27 +00:00
|
|
|
* @owner: optionally retrieve the chunk owner
|
2015-11-30 02:48:52 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-06-13 22:56:27 +00:00
|
|
|
unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
|
|
|
|
genpool_algo_t algo, void *data, void **owner)
|
2005-06-22 00:15:02 +00:00
|
|
|
{
|
2006-06-23 09:03:21 +00:00
|
|
|
struct gen_pool_chunk *chunk;
|
2011-07-13 05:14:24 +00:00
|
|
|
unsigned long addr = 0;
|
2006-06-23 09:03:21 +00:00
|
|
|
int order = pool->min_alloc_order;
|
2016-10-28 00:46:59 +00:00
|
|
|
int nbits, start_bit, end_bit, remain;
|
2011-07-13 05:14:24 +00:00
|
|
|
|
|
|
|
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
|
|
|
|
BUG_ON(in_nmi());
|
|
|
|
#endif
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2019-06-13 22:56:27 +00:00
|
|
|
if (owner)
|
|
|
|
*owner = NULL;
|
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
if (size == 0)
|
|
|
|
return 0;
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2006-06-23 09:03:21 +00:00
|
|
|
nbits = (size + (1UL << order) - 1) >> order;
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
|
2017-11-17 23:28:16 +00:00
|
|
|
if (size > atomic_long_read(&chunk->avail))
|
2011-07-13 05:14:24 +00:00
|
|
|
continue;
|
2006-06-23 09:03:21 +00:00
|
|
|
|
2016-10-28 00:46:59 +00:00
|
|
|
start_bit = 0;
|
2013-09-11 21:21:43 +00:00
|
|
|
end_bit = chunk_size(chunk) >> order;
|
2011-07-13 05:14:24 +00:00
|
|
|
retry:
|
2015-11-30 02:48:52 +00:00
|
|
|
start_bit = algo(chunk->bits, end_bit, start_bit,
|
2019-01-03 23:26:44 +00:00
|
|
|
nbits, data, pool, chunk->start_addr);
|
2011-07-13 05:14:24 +00:00
|
|
|
if (start_bit >= end_bit)
|
2009-12-16 00:48:31 +00:00
|
|
|
continue;
|
2011-07-13 05:14:24 +00:00
|
|
|
remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
|
|
|
|
if (remain) {
|
|
|
|
remain = bitmap_clear_ll(chunk->bits, start_bit,
|
|
|
|
nbits - remain);
|
|
|
|
BUG_ON(remain);
|
|
|
|
goto retry;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
2009-12-16 00:48:31 +00:00
|
|
|
|
|
|
|
addr = chunk->start_addr + ((unsigned long)start_bit << order);
|
2011-07-13 05:14:24 +00:00
|
|
|
size = nbits << order;
|
2017-11-17 23:28:16 +00:00
|
|
|
atomic_long_sub(size, &chunk->avail);
|
2019-06-13 22:56:27 +00:00
|
|
|
if (owner)
|
|
|
|
*owner = chunk->owner;
|
2011-07-13 05:14:24 +00:00
|
|
|
break;
|
2006-06-23 09:03:21 +00:00
|
|
|
}
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
return addr;
|
2006-06-23 09:03:21 +00:00
|
|
|
}
|
2019-06-13 22:56:27 +00:00
|
|
|
EXPORT_SYMBOL(gen_pool_alloc_algo_owner);
|
2005-06-22 00:15:02 +00:00
|
|
|
|
2013-11-12 23:09:52 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2019-05-29 10:28:39 +00:00
|
|
|
* @dma: dma-view physical address return value. Use %NULL if unneeded.
|
2013-11-12 23:09:52 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2019-05-29 10:28:39 +00:00
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated memory, or %NULL on failure
|
2013-11-12 23:09:52 +00:00
|
|
|
*/
|
|
|
|
void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
|
2019-06-25 15:05:58 +00:00
|
|
|
{
|
|
|
|
return gen_pool_dma_alloc_algo(pool, size, dma, pool->algo, pool->data);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_dma_alloc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_dma_alloc_algo - allocate special memory from the pool for DMA
|
|
|
|
* usage with the given pool algorithm
|
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @size: number of bytes to allocate from the pool
|
|
|
|
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
|
|
|
|
* @algo: algorithm passed from caller
|
|
|
|
* @data: data passed to algorithm
|
|
|
|
*
|
|
|
|
* Allocate the requested number of bytes from the specified pool. Uses the
|
|
|
|
* given pool allocation function. Can not be used in NMI handler on
|
|
|
|
* architectures without NMI-safe cmpxchg implementation.
|
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated memory, or %NULL on failure
|
|
|
|
*/
|
|
|
|
void *gen_pool_dma_alloc_algo(struct gen_pool *pool, size_t size,
|
|
|
|
dma_addr_t *dma, genpool_algo_t algo, void *data)
|
2013-11-12 23:09:52 +00:00
|
|
|
{
|
|
|
|
unsigned long vaddr;
|
|
|
|
|
|
|
|
if (!pool)
|
|
|
|
return NULL;
|
|
|
|
|
2019-06-25 15:05:58 +00:00
|
|
|
vaddr = gen_pool_alloc_algo(pool, size, algo, data);
|
2013-11-12 23:09:52 +00:00
|
|
|
if (!vaddr)
|
|
|
|
return NULL;
|
|
|
|
|
2014-01-29 22:05:37 +00:00
|
|
|
if (dma)
|
|
|
|
*dma = gen_pool_virt_to_phys(pool, vaddr);
|
2013-11-12 23:09:52 +00:00
|
|
|
|
|
|
|
return (void *)vaddr;
|
|
|
|
}
|
2019-06-25 15:05:58 +00:00
|
|
|
EXPORT_SYMBOL(gen_pool_dma_alloc_algo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_dma_alloc_align - allocate special memory from the pool for DMA
|
|
|
|
* usage with the given alignment
|
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @size: number of bytes to allocate from the pool
|
|
|
|
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
|
|
|
|
* @align: alignment in bytes for starting address
|
|
|
|
*
|
|
|
|
* Allocate the requested number bytes from the specified pool, with the given
|
|
|
|
* alignment restriction. Can not be used in NMI handler on architectures
|
|
|
|
* without NMI-safe cmpxchg implementation.
|
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated memory, or %NULL on failure
|
|
|
|
*/
|
|
|
|
void *gen_pool_dma_alloc_align(struct gen_pool *pool, size_t size,
|
|
|
|
dma_addr_t *dma, int align)
|
|
|
|
{
|
|
|
|
struct genpool_data_align data = { .align = align };
|
|
|
|
|
|
|
|
return gen_pool_dma_alloc_algo(pool, size, dma,
|
|
|
|
gen_pool_first_fit_align, &data);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_dma_alloc_align);
|
2013-11-12 23:09:52 +00:00
|
|
|
|
2019-05-29 10:28:39 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_dma_zalloc - allocate special zeroed 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 return value. Use %NULL if unneeded.
|
|
|
|
*
|
|
|
|
* Allocate the requested number of zeroed 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.
|
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated zeroed memory, or %NULL on failure
|
|
|
|
*/
|
|
|
|
void *gen_pool_dma_zalloc(struct gen_pool *pool, size_t size, dma_addr_t *dma)
|
|
|
|
{
|
2019-06-25 15:05:58 +00:00
|
|
|
return gen_pool_dma_zalloc_algo(pool, size, dma, pool->algo, pool->data);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_dma_zalloc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_dma_zalloc_algo - allocate special zeroed memory from the pool for
|
|
|
|
* DMA usage with the given pool algorithm
|
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @size: number of bytes to allocate from the pool
|
|
|
|
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
|
|
|
|
* @algo: algorithm passed from caller
|
|
|
|
* @data: data passed to algorithm
|
|
|
|
*
|
|
|
|
* Allocate the requested number of zeroed bytes from the specified pool. Uses
|
|
|
|
* the given pool allocation function. Can not be used in NMI handler on
|
|
|
|
* architectures without NMI-safe cmpxchg implementation.
|
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated zeroed memory, or %NULL on failure
|
|
|
|
*/
|
|
|
|
void *gen_pool_dma_zalloc_algo(struct gen_pool *pool, size_t size,
|
|
|
|
dma_addr_t *dma, genpool_algo_t algo, void *data)
|
|
|
|
{
|
|
|
|
void *vaddr = gen_pool_dma_alloc_algo(pool, size, dma, algo, data);
|
2019-05-29 10:28:39 +00:00
|
|
|
|
|
|
|
if (vaddr)
|
|
|
|
memset(vaddr, 0, size);
|
|
|
|
|
|
|
|
return vaddr;
|
|
|
|
}
|
2019-06-25 15:05:58 +00:00
|
|
|
EXPORT_SYMBOL(gen_pool_dma_zalloc_algo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_dma_zalloc_align - allocate special zeroed memory from the pool for
|
|
|
|
* DMA usage with the given alignment
|
|
|
|
* @pool: pool to allocate from
|
|
|
|
* @size: number of bytes to allocate from the pool
|
|
|
|
* @dma: DMA-view physical address return value. Use %NULL if unneeded.
|
|
|
|
* @align: alignment in bytes for starting address
|
|
|
|
*
|
|
|
|
* Allocate the requested number of zeroed bytes from the specified pool,
|
|
|
|
* with the given alignment restriction. Can not be used in NMI handler on
|
|
|
|
* architectures without NMI-safe cmpxchg implementation.
|
|
|
|
*
|
|
|
|
* Return: virtual address of the allocated zeroed memory, or %NULL on failure
|
|
|
|
*/
|
|
|
|
void *gen_pool_dma_zalloc_align(struct gen_pool *pool, size_t size,
|
|
|
|
dma_addr_t *dma, int align)
|
|
|
|
{
|
|
|
|
struct genpool_data_align data = { .align = align };
|
|
|
|
|
|
|
|
return gen_pool_dma_zalloc_algo(pool, size, dma,
|
|
|
|
gen_pool_first_fit_align, &data);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_dma_zalloc_align);
|
2013-11-12 23:09:52 +00:00
|
|
|
|
2006-10-02 09:17:01 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_free - free allocated special memory back to the pool
|
2006-06-23 09:03:21 +00:00
|
|
|
* @pool: pool to free to
|
|
|
|
* @addr: starting address of memory to free back to pool
|
|
|
|
* @size: size in bytes of memory to free
|
2019-06-13 22:56:27 +00:00
|
|
|
* @owner: private data stashed at gen_pool_add() time
|
2006-10-02 09:17:01 +00:00
|
|
|
*
|
2011-07-13 05:14:24 +00:00
|
|
|
* Free previously allocated special memory back to the specified
|
|
|
|
* pool. Can not be used in NMI handler on architectures without
|
|
|
|
* NMI-safe cmpxchg implementation.
|
2006-06-23 09:03:21 +00:00
|
|
|
*/
|
2019-06-13 22:56:27 +00:00
|
|
|
void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
|
|
|
|
void **owner)
|
2006-06-23 09:03:21 +00:00
|
|
|
{
|
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
int order = pool->min_alloc_order;
|
2011-07-13 05:14:24 +00:00
|
|
|
int start_bit, nbits, remain;
|
2006-06-23 09:03:21 +00:00
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
|
|
|
|
BUG_ON(in_nmi());
|
|
|
|
#endif
|
2006-06-23 09:03:21 +00:00
|
|
|
|
2019-06-13 22:56:27 +00:00
|
|
|
if (owner)
|
|
|
|
*owner = NULL;
|
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
nbits = (size + (1UL << order) - 1) >> order;
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
|
2013-09-11 21:21:43 +00:00
|
|
|
if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
|
|
|
|
BUG_ON(addr + size - 1 > chunk->end_addr);
|
2011-07-13 05:14:24 +00:00
|
|
|
start_bit = (addr - chunk->start_addr) >> order;
|
|
|
|
remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
|
|
|
|
BUG_ON(remain);
|
|
|
|
size = nbits << order;
|
2017-11-17 23:28:16 +00:00
|
|
|
atomic_long_add(size, &chunk->avail);
|
2019-06-13 22:56:27 +00:00
|
|
|
if (owner)
|
|
|
|
*owner = chunk->owner;
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
return;
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
BUG();
|
2005-06-22 00:15:02 +00:00
|
|
|
}
|
2019-06-13 22:56:27 +00:00
|
|
|
EXPORT_SYMBOL(gen_pool_free_owner);
|
2011-07-13 05:14:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_for_each_chunk - call func for every chunk of generic memory pool
|
|
|
|
* @pool: the generic memory pool
|
|
|
|
* @func: func to call
|
|
|
|
* @data: additional data used by @func
|
|
|
|
*
|
|
|
|
* Call @func for every chunk of generic memory pool. The @func is
|
|
|
|
* called with rcu_read_lock held.
|
|
|
|
*/
|
|
|
|
void gen_pool_for_each_chunk(struct gen_pool *pool,
|
|
|
|
void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk, void *data),
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
|
|
|
|
func(pool, chunk, data);
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_for_each_chunk);
|
|
|
|
|
2014-10-09 22:26:38 +00:00
|
|
|
/**
|
|
|
|
* addr_in_gen_pool - checks if an address falls within the range of a pool
|
|
|
|
* @pool: the generic memory pool
|
|
|
|
* @start: start address
|
|
|
|
* @size: size of the region
|
|
|
|
*
|
|
|
|
* Check if the range of addresses falls within the specified pool. Returns
|
|
|
|
* true if the entire range is contained in the pool and false otherwise.
|
|
|
|
*/
|
|
|
|
bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
bool found = false;
|
2015-02-12 23:02:18 +00:00
|
|
|
unsigned long end = start + size - 1;
|
2014-10-09 22:26:38 +00:00
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) {
|
|
|
|
if (start >= chunk->start_addr && start <= chunk->end_addr) {
|
|
|
|
if (end <= chunk->end_addr) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rcu_read_unlock();
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2011-07-13 05:14:24 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_avail - get available free space of the pool
|
|
|
|
* @pool: pool to get available free space
|
|
|
|
*
|
|
|
|
* Return available free space of the specified pool.
|
|
|
|
*/
|
|
|
|
size_t gen_pool_avail(struct gen_pool *pool)
|
|
|
|
{
|
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
size_t avail = 0;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
|
2017-11-17 23:28:16 +00:00
|
|
|
avail += atomic_long_read(&chunk->avail);
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
return avail;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gen_pool_avail);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_size - get size in bytes of memory managed by the pool
|
|
|
|
* @pool: pool to get size
|
|
|
|
*
|
|
|
|
* Return size in bytes of memory managed by the pool.
|
|
|
|
*/
|
|
|
|
size_t gen_pool_size(struct gen_pool *pool)
|
|
|
|
{
|
|
|
|
struct gen_pool_chunk *chunk;
|
|
|
|
size_t size = 0;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
|
2013-09-11 21:21:43 +00:00
|
|
|
size += chunk_size(chunk);
|
2011-07-13 05:14:24 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gen_pool_size);
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_set_algo - set the allocation algorithm
|
|
|
|
* @pool: pool to change allocation algorithm
|
|
|
|
* @algo: custom algorithm function
|
|
|
|
* @data: additional data used by @algo
|
|
|
|
*
|
|
|
|
* Call @algo for each memory allocation in the pool.
|
|
|
|
* If @algo is NULL use gen_pool_first_fit as default
|
|
|
|
* memory allocation function.
|
|
|
|
*/
|
|
|
|
void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo, void *data)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
|
|
|
|
pool->algo = algo;
|
|
|
|
if (!pool->algo)
|
|
|
|
pool->algo = gen_pool_first_fit;
|
|
|
|
|
|
|
|
pool->data = data;
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_set_algo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gen_pool_first_fit - find the first available region
|
|
|
|
* of memory matching the size requirement (no alignment constraint)
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @data: additional data - unused
|
2015-11-30 02:48:52 +00:00
|
|
|
* @pool: pool to find the fit region memory from
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
*/
|
|
|
|
unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
|
2015-11-30 02:48:52 +00:00
|
|
|
unsigned long start, unsigned int nr, void *data,
|
2019-01-03 23:26:44 +00:00
|
|
|
struct gen_pool *pool, unsigned long start_addr)
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
{
|
|
|
|
return bitmap_find_next_zero_area(map, size, start, nr, 0);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_first_fit);
|
|
|
|
|
2015-11-30 02:48:52 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_first_fit_align - find the first available region
|
|
|
|
* of memory matching the size requirement (alignment constraint)
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @data: data for alignment
|
|
|
|
* @pool: pool to get order from
|
|
|
|
*/
|
|
|
|
unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
|
|
|
|
unsigned long start, unsigned int nr, void *data,
|
2019-01-03 23:26:44 +00:00
|
|
|
struct gen_pool *pool, unsigned long start_addr)
|
2015-11-30 02:48:52 +00:00
|
|
|
{
|
|
|
|
struct genpool_data_align *alignment;
|
2019-01-03 23:26:44 +00:00
|
|
|
unsigned long align_mask, align_off;
|
2015-11-30 02:48:52 +00:00
|
|
|
int order;
|
|
|
|
|
|
|
|
alignment = data;
|
|
|
|
order = pool->min_alloc_order;
|
|
|
|
align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
|
2019-01-03 23:26:44 +00:00
|
|
|
align_off = (start_addr & (alignment->align - 1)) >> order;
|
|
|
|
|
|
|
|
return bitmap_find_next_zero_area_off(map, size, start, nr,
|
|
|
|
align_mask, align_off);
|
2015-11-30 02:48:52 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_first_fit_align);
|
|
|
|
|
2015-11-30 02:48:53 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_fixed_alloc - reserve a specific region
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @data: data for alignment
|
|
|
|
* @pool: pool to get order from
|
|
|
|
*/
|
|
|
|
unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size,
|
|
|
|
unsigned long start, unsigned int nr, void *data,
|
2019-01-03 23:26:44 +00:00
|
|
|
struct gen_pool *pool, unsigned long start_addr)
|
2015-11-30 02:48:53 +00:00
|
|
|
{
|
|
|
|
struct genpool_data_fixed *fixed_data;
|
|
|
|
int order;
|
|
|
|
unsigned long offset_bit;
|
|
|
|
unsigned long start_bit;
|
|
|
|
|
|
|
|
fixed_data = data;
|
|
|
|
order = pool->min_alloc_order;
|
|
|
|
offset_bit = fixed_data->offset >> order;
|
2015-11-30 02:48:54 +00:00
|
|
|
if (WARN_ON(fixed_data->offset & ((1UL << order) - 1)))
|
2015-11-30 02:48:53 +00:00
|
|
|
return size;
|
|
|
|
|
|
|
|
start_bit = bitmap_find_next_zero_area(map, size,
|
|
|
|
start + offset_bit, nr, 0);
|
|
|
|
if (start_bit != offset_bit)
|
|
|
|
start_bit = size;
|
|
|
|
return start_bit;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_fixed_alloc);
|
|
|
|
|
2014-10-09 22:26:35 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_first_fit_order_align - find the first available region
|
|
|
|
* of memory matching the size requirement. The region will be aligned
|
|
|
|
* to the order of the size specified.
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @data: additional data - unused
|
2015-11-30 02:48:52 +00:00
|
|
|
* @pool: pool to find the fit region memory from
|
2014-10-09 22:26:35 +00:00
|
|
|
*/
|
|
|
|
unsigned long gen_pool_first_fit_order_align(unsigned long *map,
|
|
|
|
unsigned long size, unsigned long start,
|
2019-01-03 23:26:44 +00:00
|
|
|
unsigned int nr, void *data, struct gen_pool *pool,
|
|
|
|
unsigned long start_addr)
|
2014-10-09 22:26:35 +00:00
|
|
|
{
|
|
|
|
unsigned long align_mask = roundup_pow_of_two(nr) - 1;
|
|
|
|
|
|
|
|
return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_first_fit_order_align);
|
|
|
|
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_best_fit - find the best fitting region of memory
|
|
|
|
* macthing the size requirement (no alignment constraint)
|
|
|
|
* @map: The address to base the search on
|
|
|
|
* @size: The bitmap size in bits
|
|
|
|
* @start: The bitnumber to start searching at
|
|
|
|
* @nr: The number of zeroed bits we're looking for
|
|
|
|
* @data: additional data - unused
|
2015-11-30 02:48:52 +00:00
|
|
|
* @pool: pool to find the fit region memory from
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
*
|
|
|
|
* Iterate over the bitmap to find the smallest free region
|
|
|
|
* which we can allocate the memory.
|
|
|
|
*/
|
|
|
|
unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
|
2015-11-30 02:48:52 +00:00
|
|
|
unsigned long start, unsigned int nr, void *data,
|
2019-01-03 23:26:44 +00:00
|
|
|
struct gen_pool *pool, unsigned long start_addr)
|
genalloc: make it possible to use a custom allocation algorithm
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: Benjamin Gaignard <benjamin.gaignard@stericsson.com>
Cc: Huang Ying <ying.huang@intel.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2012-10-05 00:13:20 +00:00
|
|
|
{
|
|
|
|
unsigned long start_bit = size;
|
|
|
|
unsigned long len = size + 1;
|
|
|
|
unsigned long index;
|
|
|
|
|
|
|
|
index = bitmap_find_next_zero_area(map, size, start, nr, 0);
|
|
|
|
|
|
|
|
while (index < size) {
|
|
|
|
int next_bit = find_next_bit(map, size, index + nr);
|
|
|
|
if ((next_bit - index) < len) {
|
|
|
|
len = next_bit - index;
|
|
|
|
start_bit = index;
|
|
|
|
if (len == nr)
|
|
|
|
return start_bit;
|
|
|
|
}
|
|
|
|
index = bitmap_find_next_zero_area(map, size,
|
|
|
|
next_bit + 1, nr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return start_bit;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(gen_pool_best_fit);
|
2013-04-29 23:17:10 +00:00
|
|
|
|
|
|
|
static void devm_gen_pool_release(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
gen_pool_destroy(*(struct gen_pool **)res);
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:47:47 +00:00
|
|
|
static int devm_gen_pool_match(struct device *dev, void *res, void *data)
|
|
|
|
{
|
|
|
|
struct gen_pool **p = res;
|
|
|
|
|
|
|
|
/* NULL data matches only a pool without an assigned name */
|
|
|
|
if (!data && !(*p)->name)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!data || !(*p)->name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return !strcmp((*p)->name, data);
|
|
|
|
}
|
|
|
|
|
2015-09-04 22:47:43 +00:00
|
|
|
/**
|
|
|
|
* gen_pool_get - Obtain the gen_pool (if any) for a device
|
|
|
|
* @dev: device to retrieve the gen_pool from
|
|
|
|
* @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
|
|
|
|
*
|
|
|
|
* Returns the gen_pool for the device if one is present, or NULL.
|
|
|
|
*/
|
|
|
|
struct gen_pool *gen_pool_get(struct device *dev, const char *name)
|
|
|
|
{
|
|
|
|
struct gen_pool **p;
|
|
|
|
|
2015-09-04 22:47:47 +00:00
|
|
|
p = devres_find(dev, devm_gen_pool_release, devm_gen_pool_match,
|
|
|
|
(void *)name);
|
2015-09-04 22:47:43 +00:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(gen_pool_get);
|
|
|
|
|
2013-04-29 23:17:10 +00:00
|
|
|
/**
|
|
|
|
* devm_gen_pool_create - managed gen_pool_create
|
|
|
|
* @dev: device that provides the gen_pool
|
|
|
|
* @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
|
2015-09-04 22:47:43 +00:00
|
|
|
* @nid: node selector for allocated gen_pool, %NUMA_NO_NODE for all nodes
|
|
|
|
* @name: name of a gen_pool or NULL, identifies a particular gen_pool on device
|
2013-04-29 23:17:10 +00:00
|
|
|
*
|
|
|
|
* Create a new special memory pool that can be used to manage special purpose
|
|
|
|
* memory not managed by the regular kmalloc/kfree interface. The pool will be
|
|
|
|
* automatically destroyed by the device management code.
|
|
|
|
*/
|
|
|
|
struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
|
2015-09-04 22:47:43 +00:00
|
|
|
int nid, const char *name)
|
2013-04-29 23:17:10 +00:00
|
|
|
{
|
|
|
|
struct gen_pool **ptr, *pool;
|
2015-09-04 22:47:47 +00:00
|
|
|
const char *pool_name = NULL;
|
2013-04-29 23:17:10 +00:00
|
|
|
|
2015-09-04 22:47:43 +00:00
|
|
|
/* Check that genpool to be created is uniquely addressed on device */
|
|
|
|
if (gen_pool_get(dev, name))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
2015-09-04 22:47:47 +00:00
|
|
|
if (name) {
|
|
|
|
pool_name = kstrdup_const(name, GFP_KERNEL);
|
|
|
|
if (!pool_name)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
}
|
|
|
|
|
2013-04-29 23:17:10 +00:00
|
|
|
ptr = devres_alloc(devm_gen_pool_release, sizeof(*ptr), GFP_KERNEL);
|
2015-02-13 22:36:47 +00:00
|
|
|
if (!ptr)
|
2015-09-04 22:47:47 +00:00
|
|
|
goto free_pool_name;
|
2013-04-29 23:17:10 +00:00
|
|
|
|
|
|
|
pool = gen_pool_create(min_alloc_order, nid);
|
2015-09-04 22:47:47 +00:00
|
|
|
if (!pool)
|
|
|
|
goto free_devres;
|
|
|
|
|
|
|
|
*ptr = pool;
|
|
|
|
pool->name = pool_name;
|
|
|
|
devres_add(dev, ptr);
|
2013-04-29 23:17:10 +00:00
|
|
|
|
|
|
|
return pool;
|
2015-09-04 22:47:47 +00:00
|
|
|
|
|
|
|
free_devres:
|
|
|
|
devres_free(ptr);
|
|
|
|
free_pool_name:
|
|
|
|
kfree_const(pool_name);
|
|
|
|
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2013-04-29 23:17:10 +00:00
|
|
|
}
|
2014-12-02 23:59:45 +00:00
|
|
|
EXPORT_SYMBOL(devm_gen_pool_create);
|
2013-04-29 23:17:10 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_OF
|
|
|
|
/**
|
2015-06-30 22:00:07 +00:00
|
|
|
* of_gen_pool_get - find a pool by phandle property
|
2013-04-29 23:17:10 +00:00
|
|
|
* @np: device node
|
|
|
|
* @propname: property name containing phandle(s)
|
|
|
|
* @index: index into the phandle array
|
|
|
|
*
|
|
|
|
* Returns the pool that contains the chunk starting at the physical
|
|
|
|
* address of the device tree node pointed at by the phandle property,
|
|
|
|
* or NULL if not found.
|
|
|
|
*/
|
2015-06-30 22:00:07 +00:00
|
|
|
struct gen_pool *of_gen_pool_get(struct device_node *np,
|
2013-04-29 23:17:10 +00:00
|
|
|
const char *propname, int index)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev;
|
2015-09-04 22:47:47 +00:00
|
|
|
struct device_node *np_pool, *parent;
|
|
|
|
const char *name = NULL;
|
|
|
|
struct gen_pool *pool = NULL;
|
2013-04-29 23:17:10 +00:00
|
|
|
|
|
|
|
np_pool = of_parse_phandle(np, propname, index);
|
|
|
|
if (!np_pool)
|
|
|
|
return NULL;
|
2015-09-04 22:47:47 +00:00
|
|
|
|
2013-04-29 23:17:10 +00:00
|
|
|
pdev = of_find_device_by_node(np_pool);
|
2015-09-04 22:47:47 +00:00
|
|
|
if (!pdev) {
|
|
|
|
/* Check if named gen_pool is created by parent node device */
|
|
|
|
parent = of_get_parent(np_pool);
|
|
|
|
pdev = of_find_device_by_node(parent);
|
|
|
|
of_node_put(parent);
|
|
|
|
|
|
|
|
of_property_read_string(np_pool, "label", &name);
|
|
|
|
if (!name)
|
|
|
|
name = np_pool->name;
|
|
|
|
}
|
|
|
|
if (pdev)
|
|
|
|
pool = gen_pool_get(&pdev->dev, name);
|
2014-09-25 23:05:25 +00:00
|
|
|
of_node_put(np_pool);
|
2015-09-04 22:47:47 +00:00
|
|
|
|
|
|
|
return pool;
|
2013-04-29 23:17:10 +00:00
|
|
|
}
|
2015-06-30 22:00:07 +00:00
|
|
|
EXPORT_SYMBOL_GPL(of_gen_pool_get);
|
2013-04-29 23:17:10 +00:00
|
|
|
#endif /* CONFIG_OF */
|