Skip to content

Commit 0565d31

Browse files
htejuntorvalds
authored andcommitted
mempool: drop unnecessary and incorrect BUG_ON() from mempool_destroy()
mempool_destroy() is a thin wrapper around free_pool(). The only thing it adds is BUG_ON(pool->curr_nr != pool->min_nr). The intention seems to be to enforce that all allocated elements are freed; however, the BUG_ON() can't achieve that (it doesn't know anything about objects above min_nr) and incorrect as mempool_resize() is allowed to leave the pool extended but not filled. Furthermore, panicking is way worse than any memory leak and there are better debug tools to track memory leaks. Drop the BUG_ON() from mempool_destory() and as that leaves the function identical to free_pool(), replace it. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5b99054 commit 0565d31

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

mm/mempool.c

+11-19
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ static void *remove_element(mempool_t *pool)
2727
return pool->elements[--pool->curr_nr];
2828
}
2929

30-
static void free_pool(mempool_t *pool)
30+
/**
31+
* mempool_destroy - deallocate a memory pool
32+
* @pool: pointer to the memory pool which was allocated via
33+
* mempool_create().
34+
*
35+
* Free all reserved elements in @pool and @pool itself. This function
36+
* only sleeps if the free_fn() function sleeps.
37+
*/
38+
void mempool_destroy(mempool_t *pool)
3139
{
3240
while (pool->curr_nr) {
3341
void *element = remove_element(pool);
@@ -36,6 +44,7 @@ static void free_pool(mempool_t *pool)
3644
kfree(pool->elements);
3745
kfree(pool);
3846
}
47+
EXPORT_SYMBOL(mempool_destroy);
3948

4049
/**
4150
* mempool_create - create a memory pool
@@ -86,7 +95,7 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
8695

8796
element = pool->alloc(GFP_KERNEL, pool->pool_data);
8897
if (unlikely(!element)) {
89-
free_pool(pool);
98+
mempool_destroy(pool);
9099
return NULL;
91100
}
92101
add_element(pool, element);
@@ -171,23 +180,6 @@ int mempool_resize(mempool_t *pool, int new_min_nr, gfp_t gfp_mask)
171180
}
172181
EXPORT_SYMBOL(mempool_resize);
173182

174-
/**
175-
* mempool_destroy - deallocate a memory pool
176-
* @pool: pointer to the memory pool which was allocated via
177-
* mempool_create().
178-
*
179-
* this function only sleeps if the free_fn() function sleeps. The caller
180-
* has to guarantee that all elements have been returned to the pool (ie:
181-
* freed) prior to calling mempool_destroy().
182-
*/
183-
void mempool_destroy(mempool_t *pool)
184-
{
185-
/* Check for outstanding elements */
186-
BUG_ON(pool->curr_nr != pool->min_nr);
187-
free_pool(pool);
188-
}
189-
EXPORT_SYMBOL(mempool_destroy);
190-
191183
/**
192184
* mempool_alloc - allocate an element from a specific memory pool
193185
* @pool: pointer to the memory pool which was allocated via

0 commit comments

Comments
 (0)