public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/genalloc: Set chunk size to real size which gen_pool managed.
@ 2022-06-12 10:59 wuchi
  2022-06-13 18:14 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: wuchi @ 2022-06-12 10:59 UTC (permalink / raw)
  To: akpm, alexs, sjhuang, sfr; +Cc: linux-kernel

The demand size (chunk->avail > size > round_down(chunk->avail)) will
lead to meaningless algo calls in gen_pool_alloc_algo_owner without the
patch, alse move the follow code:
	size = nbits << order
out of read-side critical section.

Signed-off-by: wuchi <wuchi.zero@gmail.com>
---
 lib/genalloc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/genalloc.c b/lib/genalloc.c
index 00fc50d0a640..1b1843613fb8 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -193,6 +193,7 @@ int gen_pool_add_owner(struct gen_pool *pool, unsigned long virt, phys_addr_t ph
 	if (unlikely(chunk == NULL))
 		return -ENOMEM;
 
+	size = nbits << pool->min_alloc_order;
 	chunk->phys_addr = phys;
 	chunk->start_addr = virt;
 	chunk->end_addr = virt + size - 1;
@@ -293,6 +294,7 @@ unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
 		return 0;
 
 	nbits = (size + (1UL << order) - 1) >> order;
+	size = nbits << order;
 	rcu_read_lock();
 	list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
 		if (size > atomic_long_read(&chunk->avail))
@@ -314,7 +316,6 @@ unsigned long gen_pool_alloc_algo_owner(struct gen_pool *pool, size_t size,
 		}
 
 		addr = chunk->start_addr + ((unsigned long)start_bit << order);
-		size = nbits << order;
 		atomic_long_sub(size, &chunk->avail);
 		if (owner)
 			*owner = chunk->owner;
@@ -499,6 +500,7 @@ void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
 		*owner = NULL;
 
 	nbits = (size + (1UL << order) - 1) >> order;
+	size = nbits << order;
 	rcu_read_lock();
 	list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
 		if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
@@ -506,7 +508,6 @@ void gen_pool_free_owner(struct gen_pool *pool, unsigned long addr, size_t size,
 			start_bit = (addr - chunk->start_addr) >> order;
 			remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
 			BUG_ON(remain);
-			size = nbits << order;
 			atomic_long_add(size, &chunk->avail);
 			if (owner)
 				*owner = chunk->owner;
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-06-15 12:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-12 10:59 [PATCH] lib/genalloc: Set chunk size to real size which gen_pool managed wuchi
2022-06-13 18:14 ` Andrew Morton
2022-06-14  5:20   ` chi wu
2022-06-14 17:53     ` Andrew Morton
2022-06-15 12:41       ` chi wu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox