[PATCH 1/4] mm: mempool: extract common initialization code

From: Yang Shi
Date: Wed Oct 05 2022 - 14:04:03 EST


Extract the common initialization code to __mempool_init() and
__mempool_create(). This will make adding mempool bulk init
code easier.

Signed-off-by: Yang Shi <shy828301@xxxxxxxxx>
---
mm/mempool.c | 57 ++++++++++++++++++++++++++++++++++------------------
1 file changed, 37 insertions(+), 20 deletions(-)

diff --git a/mm/mempool.c b/mm/mempool.c
index 96488b13a1ef..ba32151f3843 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -176,9 +176,10 @@ void mempool_destroy(mempool_t *pool)
}
EXPORT_SYMBOL(mempool_destroy);

-int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
- mempool_free_t *free_fn, void *pool_data,
- gfp_t gfp_mask, int node_id)
+static inline int __mempool_init(mempool_t *pool, int min_nr,
+ mempool_alloc_t *alloc_fn,
+ mempool_free_t *free_fn, void *pool_data,
+ gfp_t gfp_mask, int node_id)
{
spin_lock_init(&pool->lock);
pool->min_nr = min_nr;
@@ -208,6 +209,14 @@ int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,

return 0;
}
+
+int mempool_init_node(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
+ mempool_free_t *free_fn, void *pool_data,
+ gfp_t gfp_mask, int node_id)
+{
+ return __mempool_init(pool, min_nr, alloc_fn, free_fn, pool_data,
+ gfp_mask, node_id);
+}
EXPORT_SYMBOL(mempool_init_node);

/**
@@ -227,12 +236,31 @@ EXPORT_SYMBOL(mempool_init_node);
int mempool_init(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
mempool_free_t *free_fn, void *pool_data)
{
- return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
- pool_data, GFP_KERNEL, NUMA_NO_NODE);
+ return __mempool_init(pool, min_nr, alloc_fn, free_fn,
+ pool_data, GFP_KERNEL, NUMA_NO_NODE);

}
EXPORT_SYMBOL(mempool_init);

+static mempool_t *__mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
+ mempool_free_t *free_fn, void *pool_data,
+ gfp_t gfp_mask, int node_id)
+{
+ mempool_t *pool;
+
+ pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
+ if (!pool)
+ return NULL;
+
+ if (__mempool_init(pool, min_nr, alloc_fn, free_fn, pool_data,
+ gfp_mask, node_id)) {
+ kfree(pool);
+ return NULL;
+ }
+
+ return pool;
+}
+
/**
* mempool_create - create a memory pool
* @min_nr: the minimum number of elements guaranteed to be
@@ -252,8 +280,8 @@ EXPORT_SYMBOL(mempool_init);
mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn,
mempool_free_t *free_fn, void *pool_data)
{
- return mempool_create_node(min_nr, alloc_fn, free_fn, pool_data,
- GFP_KERNEL, NUMA_NO_NODE);
+ return __mempool_create(min_nr, alloc_fn, free_fn, pool_data,
+ GFP_KERNEL, NUMA_NO_NODE);
}
EXPORT_SYMBOL(mempool_create);

@@ -261,19 +289,8 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
mempool_free_t *free_fn, void *pool_data,
gfp_t gfp_mask, int node_id)
{
- mempool_t *pool;
-
- pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
- if (!pool)
- return NULL;
-
- if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
- gfp_mask, node_id)) {
- kfree(pool);
- return NULL;
- }
-
- return pool;
+ return __mempool_create(min_nr, alloc_fn, free_fn, pool_data,
+ gfp_mask, node_id);
}
EXPORT_SYMBOL(mempool_create_node);

--
2.26.3