Skip to content

Commit 9efb3a4

Browse files
labbotttorvalds
authored andcommitted
lib/genalloc.c: add genpool range check function
After allocating an address from a particular genpool, there is no good way to verify if that address actually belongs to a genpool. Introduce addr_in_gen_pool which will return if an address plus size falls completely within the genpool range. Signed-off-by: Laura Abbott <lauraa@codeaurora.org> Acked-by: Will Deacon <will.deacon@arm.com> Reviewed-by: Olof Johansson <olof@lixom.net> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Arnd Bergmann <arnd@arndb.de> Cc: David Riley <davidriley@chromium.org> Cc: Ritesh Harjain <ritesh.harjani@gmail.com> Cc: Russell King <linux@arm.linux.org.uk> Cc: Thierry Reding <thierry.reding@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 505e3be commit 9efb3a4

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎include/linux/genalloc.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ extern struct gen_pool *devm_gen_pool_create(struct device *dev,
121121
int min_alloc_order, int nid);
122122
extern struct gen_pool *dev_get_gen_pool(struct device *dev);
123123

124+
bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
125+
size_t size);
126+
124127
#ifdef CONFIG_OF
125128
extern struct gen_pool *of_get_named_gen_pool(struct device_node *np,
126129
const char *propname, int index);

‎lib/genalloc.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,35 @@ void gen_pool_for_each_chunk(struct gen_pool *pool,
402402
}
403403
EXPORT_SYMBOL(gen_pool_for_each_chunk);
404404

405+
/**
406+
* addr_in_gen_pool - checks if an address falls within the range of a pool
407+
* @pool: the generic memory pool
408+
* @start: start address
409+
* @size: size of the region
410+
*
411+
* Check if the range of addresses falls within the specified pool. Returns
412+
* true if the entire range is contained in the pool and false otherwise.
413+
*/
414+
bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
415+
size_t size)
416+
{
417+
bool found = false;
418+
unsigned long end = start + size;
419+
struct gen_pool_chunk *chunk;
420+
421+
rcu_read_lock();
422+
list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk) {
423+
if (start >= chunk->start_addr && start <= chunk->end_addr) {
424+
if (end <= chunk->end_addr) {
425+
found = true;
426+
break;
427+
}
428+
}
429+
}
430+
rcu_read_unlock();
431+
return found;
432+
}
433+
405434
/**
406435
* gen_pool_avail - get available free space of the pool
407436
* @pool: pool to get available free space

0 commit comments

Comments
 (0)