Skip to content

Commit de2dd0e

Browse files
Zhao QiangScott Wood
authored andcommitted
genalloc:support memory-allocation with bytes-alignment to genalloc
Bytes alignment is required to manage some special RAM, so add gen_pool_first_fit_align to genalloc, meanwhile add gen_pool_alloc_algo to pass algo in case user layer using more than one algo, and pass data to gen_pool_first_fit_align(modify gen_pool_alloc as a wrapper) Signed-off-by: Zhao Qiang <qiang.zhao@freescale.com> Signed-off-by: Scott Wood <scottwood@freescale.com>
1 parent e9d764f commit de2dd0e

2 files changed

Lines changed: 78 additions & 10 deletions

File tree

‎include/linux/genalloc.h‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
#ifndef __GENALLOC_H__
3131
#define __GENALLOC_H__
3232

33+
#include <linux/types.h>
3334
#include <linux/spinlock_types.h>
3435

3536
struct device;
3637
struct device_node;
38+
struct gen_pool;
3739

3840
/**
3941
* Allocation callback function type definition
@@ -47,7 +49,7 @@ typedef unsigned long (*genpool_algo_t)(unsigned long *map,
4749
unsigned long size,
4850
unsigned long start,
4951
unsigned int nr,
50-
void *data);
52+
void *data, struct gen_pool *pool);
5153

5254
/*
5355
* General purpose special memory pool descriptor.
@@ -75,6 +77,13 @@ struct gen_pool_chunk {
7577
unsigned long bits[0]; /* bitmap for allocating memory chunk */
7678
};
7779

80+
/*
81+
* gen_pool data descriptor for gen_pool_first_fit_align.
82+
*/
83+
struct genpool_data_align {
84+
int align; /* alignment by bytes for starting address */
85+
};
86+
7887
extern struct gen_pool *gen_pool_create(int, int);
7988
extern phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long);
8089
extern int gen_pool_add_virt(struct gen_pool *, unsigned long, phys_addr_t,
@@ -98,6 +107,8 @@ static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
98107
}
99108
extern void gen_pool_destroy(struct gen_pool *);
100109
extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
110+
extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,
111+
genpool_algo_t algo, void *data);
101112
extern void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size,
102113
dma_addr_t *dma);
103114
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
@@ -110,14 +121,22 @@ extern void gen_pool_set_algo(struct gen_pool *pool, genpool_algo_t algo,
110121
void *data);
111122

112123
extern unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
113-
unsigned long start, unsigned int nr, void *data);
124+
unsigned long start, unsigned int nr, void *data,
125+
struct gen_pool *pool);
126+
127+
extern unsigned long gen_pool_first_fit_align(unsigned long *map,
128+
unsigned long size, unsigned long start, unsigned int nr,
129+
void *data, struct gen_pool *pool);
130+
114131

115132
extern unsigned long gen_pool_first_fit_order_align(unsigned long *map,
116133
unsigned long size, unsigned long start, unsigned int nr,
117-
void *data);
134+
void *data, struct gen_pool *pool);
118135

119136
extern unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
120-
unsigned long start, unsigned int nr, void *data);
137+
unsigned long start, unsigned int nr, void *data,
138+
struct gen_pool *pool);
139+
121140

122141
extern struct gen_pool *devm_gen_pool_create(struct device *dev,
123142
int min_alloc_order, int nid, const char *name);

‎lib/genalloc.c‎

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,25 @@ EXPORT_SYMBOL(gen_pool_destroy);
269269
* NMI-safe cmpxchg implementation.
270270
*/
271271
unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
272+
{
273+
return gen_pool_alloc_algo(pool, size, pool->algo, pool->data);
274+
}
275+
EXPORT_SYMBOL(gen_pool_alloc);
276+
277+
/**
278+
* gen_pool_alloc_algo - allocate special memory from the pool
279+
* @pool: pool to allocate from
280+
* @size: number of bytes to allocate from the pool
281+
* @algo: algorithm passed from caller
282+
* @data: data passed to algorithm
283+
*
284+
* Allocate the requested number of bytes from the specified pool.
285+
* Uses the pool allocation function (with first-fit algorithm by default).
286+
* Can not be used in NMI handler on architectures without
287+
* NMI-safe cmpxchg implementation.
288+
*/
289+
unsigned long gen_pool_alloc_algo(struct gen_pool *pool, size_t size,
290+
genpool_algo_t algo, void *data)
272291
{
273292
struct gen_pool_chunk *chunk;
274293
unsigned long addr = 0;
@@ -290,8 +309,8 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
290309

291310
end_bit = chunk_size(chunk) >> order;
292311
retry:
293-
start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits,
294-
pool->data);
312+
start_bit = algo(chunk->bits, end_bit, start_bit,
313+
nbits, data, pool);
295314
if (start_bit >= end_bit)
296315
continue;
297316
remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
@@ -310,7 +329,7 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
310329
rcu_read_unlock();
311330
return addr;
312331
}
313-
EXPORT_SYMBOL(gen_pool_alloc);
332+
EXPORT_SYMBOL(gen_pool_alloc_algo);
314333

315334
/**
316335
* gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
@@ -501,14 +520,41 @@ EXPORT_SYMBOL(gen_pool_set_algo);
501520
* @start: The bitnumber to start searching at
502521
* @nr: The number of zeroed bits we're looking for
503522
* @data: additional data - unused
523+
* @pool: pool to find the fit region memory from
504524
*/
505525
unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
506-
unsigned long start, unsigned int nr, void *data)
526+
unsigned long start, unsigned int nr, void *data,
527+
struct gen_pool *pool)
507528
{
508529
return bitmap_find_next_zero_area(map, size, start, nr, 0);
509530
}
510531
EXPORT_SYMBOL(gen_pool_first_fit);
511532

533+
/**
534+
* gen_pool_first_fit_align - find the first available region
535+
* of memory matching the size requirement (alignment constraint)
536+
* @map: The address to base the search on
537+
* @size: The bitmap size in bits
538+
* @start: The bitnumber to start searching at
539+
* @nr: The number of zeroed bits we're looking for
540+
* @data: data for alignment
541+
* @pool: pool to get order from
542+
*/
543+
unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
544+
unsigned long start, unsigned int nr, void *data,
545+
struct gen_pool *pool)
546+
{
547+
struct genpool_data_align *alignment;
548+
unsigned long align_mask;
549+
int order;
550+
551+
alignment = data;
552+
order = pool->min_alloc_order;
553+
align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
554+
return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
555+
}
556+
EXPORT_SYMBOL(gen_pool_first_fit_align);
557+
512558
/**
513559
* gen_pool_first_fit_order_align - find the first available region
514560
* of memory matching the size requirement. The region will be aligned
@@ -518,10 +564,11 @@ EXPORT_SYMBOL(gen_pool_first_fit);
518564
* @start: The bitnumber to start searching at
519565
* @nr: The number of zeroed bits we're looking for
520566
* @data: additional data - unused
567+
* @pool: pool to find the fit region memory from
521568
*/
522569
unsigned long gen_pool_first_fit_order_align(unsigned long *map,
523570
unsigned long size, unsigned long start,
524-
unsigned int nr, void *data)
571+
unsigned int nr, void *data, struct gen_pool *pool)
525572
{
526573
unsigned long align_mask = roundup_pow_of_two(nr) - 1;
527574

@@ -537,12 +584,14 @@ EXPORT_SYMBOL(gen_pool_first_fit_order_align);
537584
* @start: The bitnumber to start searching at
538585
* @nr: The number of zeroed bits we're looking for
539586
* @data: additional data - unused
587+
* @pool: pool to find the fit region memory from
540588
*
541589
* Iterate over the bitmap to find the smallest free region
542590
* which we can allocate the memory.
543591
*/
544592
unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
545-
unsigned long start, unsigned int nr, void *data)
593+
unsigned long start, unsigned int nr, void *data,
594+
struct gen_pool *pool)
546595
{
547596
unsigned long start_bit = size;
548597
unsigned long len = size + 1;

0 commit comments

Comments
 (0)