2015-12-13 03:47:22

by Wang, Zhi A

[permalink] [raw]
Subject: [PATCH] mm: mempool: Factor out mempool_refill()

This patch factors out mempool_refill() from mempool_resize(). It's reasonable
that the mempool user wants to refill the pool immdiately when it has chance
e.g. inside a sleepible context, so that next time in the IRQ context the pool
would have much more available elements to allocate.

After the refactor, mempool_refill() can also executes with mempool_resize()
/mempool_alloc/mempool_free() or another mempool_refill().

Signed-off-by: Zhi Wang <[email protected]>
---
include/linux/mempool.h | 1 +
mm/mempool.c | 61 ++++++++++++++++++++++++++++++++++++-------------
2 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/include/linux/mempool.h b/include/linux/mempool.h
index 69b6951..71f7460 100644
--- a/include/linux/mempool.h
+++ b/include/linux/mempool.h
@@ -30,6 +30,7 @@ extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
gfp_t gfp_mask, int nid);

extern int mempool_resize(mempool_t *pool, int new_min_nr);
+extern void mempool_refill(mempool_t *pool);
extern void mempool_destroy(mempool_t *pool);
extern void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask);
extern void mempool_free(void *element, mempool_t *pool);
diff --git a/mm/mempool.c b/mm/mempool.c
index 004d42b..139c477 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -223,6 +223,47 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
EXPORT_SYMBOL(mempool_create_node);

/**
+ * mempool_refill - refill an existing memory pool immediately
+ * @pool: pointer to the memory pool which was allocated via
+ * mempool_create().
+ *
+ * This function tries to refill the pool with new elements
+ * immediately. Similar with mempool_resize(), it cannot be
+ * guaranteed that the pool will be fully filled immediately.
+ *
+ * Note, the caller must guarantee that no mempool_destroy is called
+ * while this function is running. mempool_alloc() & mempool_free()
+ * might be called (eg. from IRQ contexts) while this function executes.
+ */
+void mempool_refill(mempool_t *pool)
+{
+ void *element;
+ unsigned long flags;
+
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr >= pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ return;
+ }
+
+ while (pool->curr_nr < pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ element = pool->alloc(GFP_KERNEL, pool->pool_data);
+ if (!element)
+ return;
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr < pool->min_nr) {
+ add_element(pool, element);
+ } else {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ pool->free(element, pool->pool_data); /* Raced */
+ return;
+ }
+ }
+}
+EXPORT_SYMBOL(mempool_refill);
+
+/**
* mempool_resize - resize an existing memory pool
* @pool: pointer to the memory pool which was allocated via
* mempool_create().
@@ -256,7 +297,8 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
spin_lock_irqsave(&pool->lock, flags);
}
pool->min_nr = new_min_nr;
- goto out_unlock;
+ spin_unlock_irqrestore(&pool->lock, flags);
+ goto out;
}
spin_unlock_irqrestore(&pool->lock, flags);

@@ -279,22 +321,9 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
pool->elements = new_elements;
pool->min_nr = new_min_nr;

- while (pool->curr_nr < pool->min_nr) {
- spin_unlock_irqrestore(&pool->lock, flags);
- element = pool->alloc(GFP_KERNEL, pool->pool_data);
- if (!element)
- goto out;
- spin_lock_irqsave(&pool->lock, flags);
- if (pool->curr_nr < pool->min_nr) {
- add_element(pool, element);
- } else {
- spin_unlock_irqrestore(&pool->lock, flags);
- pool->free(element, pool->pool_data); /* Raced */
- goto out;
- }
- }
-out_unlock:
spin_unlock_irqrestore(&pool->lock, flags);
+
+ mempool_refill(pool);
out:
return 0;
}
--
1.9.1


2015-12-13 15:35:41

by Wang, Zhi A

[permalink] [raw]
Subject: [PATCH] mm: mempool: Factor out mempool_refill()

This patch factors out mempool_refill() from mempool_resize(). It's reasonable
that the mempool user wants to refill the pool immdiately when it has chance
e.g. inside a sleepible context, so that next time in the IRQ context the pool
would have much more available elements to allocate.

After the refactor, mempool_refill() can also executes with mempool_resize()
/mempool_alloc/mempool_free() or another mempool_refill().

Signed-off-by: Zhi Wang <[email protected]>
---
include/linux/mempool.h | 1 +
mm/mempool.c | 61 ++++++++++++++++++++++++++++++++++++-------------
2 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/include/linux/mempool.h b/include/linux/mempool.h
index 69b6951..71f7460 100644
--- a/include/linux/mempool.h
+++ b/include/linux/mempool.h
@@ -30,6 +30,7 @@ extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
gfp_t gfp_mask, int nid);

extern int mempool_resize(mempool_t *pool, int new_min_nr);
+extern void mempool_refill(mempool_t *pool);
extern void mempool_destroy(mempool_t *pool);
extern void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask);
extern void mempool_free(void *element, mempool_t *pool);
diff --git a/mm/mempool.c b/mm/mempool.c
index 004d42b..139c477 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -223,6 +223,47 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
EXPORT_SYMBOL(mempool_create_node);

/**
+ * mempool_refill - refill an existing memory pool immediately
+ * @pool: pointer to the memory pool which was allocated via
+ * mempool_create().
+ *
+ * This function tries to refill the pool with new elements
+ * immediately. Similar with mempool_resize(), it cannot be
+ * guaranteed that the pool will be fully filled immediately.
+ *
+ * Note, the caller must guarantee that no mempool_destroy is called
+ * while this function is running. mempool_alloc() & mempool_free()
+ * might be called (eg. from IRQ contexts) while this function executes.
+ */
+void mempool_refill(mempool_t *pool)
+{
+ void *element;
+ unsigned long flags;
+
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr >= pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ return;
+ }
+
+ while (pool->curr_nr < pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ element = pool->alloc(GFP_KERNEL, pool->pool_data);
+ if (!element)
+ return;
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr < pool->min_nr) {
+ add_element(pool, element);
+ } else {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ pool->free(element, pool->pool_data); /* Raced */
+ return;
+ }
+ }
+}
+EXPORT_SYMBOL(mempool_refill);
+
+/**
* mempool_resize - resize an existing memory pool
* @pool: pointer to the memory pool which was allocated via
* mempool_create().
@@ -256,7 +297,8 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
spin_lock_irqsave(&pool->lock, flags);
}
pool->min_nr = new_min_nr;
- goto out_unlock;
+ spin_unlock_irqrestore(&pool->lock, flags);
+ goto out;
}
spin_unlock_irqrestore(&pool->lock, flags);

@@ -279,22 +321,9 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
pool->elements = new_elements;
pool->min_nr = new_min_nr;

- while (pool->curr_nr < pool->min_nr) {
- spin_unlock_irqrestore(&pool->lock, flags);
- element = pool->alloc(GFP_KERNEL, pool->pool_data);
- if (!element)
- goto out;
- spin_lock_irqsave(&pool->lock, flags);
- if (pool->curr_nr < pool->min_nr) {
- add_element(pool, element);
- } else {
- spin_unlock_irqrestore(&pool->lock, flags);
- pool->free(element, pool->pool_data); /* Raced */
- goto out;
- }
- }
-out_unlock:
spin_unlock_irqrestore(&pool->lock, flags);
+
+ mempool_refill(pool);
out:
return 0;
}
--
1.9.1

2015-12-14 11:09:54

by Wang, Zhi A

[permalink] [raw]
Subject: [PATCH] mm: mempool: Factor out mempool_refill()

This patch factors out mempool_refill() from mempool_resize(). It's reasonable
that the mempool user wants to refill the pool immdiately when it has chance
e.g. inside a sleepible context, so that next time in the IRQ context the pool
would have much more available elements to allocate.

After the refactor, mempool_refill() can also executes with mempool_resize()
/mempool_alloc/mempool_free() or another mempool_refill().

Signed-off-by: Zhi Wang <[email protected]>
---
include/linux/mempool.h | 1 +
mm/mempool.c | 61 ++++++++++++++++++++++++++++++++++++-------------
2 files changed, 46 insertions(+), 16 deletions(-)

diff --git a/include/linux/mempool.h b/include/linux/mempool.h
index 69b6951..71f7460 100644
--- a/include/linux/mempool.h
+++ b/include/linux/mempool.h
@@ -30,6 +30,7 @@ extern mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
gfp_t gfp_mask, int nid);

extern int mempool_resize(mempool_t *pool, int new_min_nr);
+extern void mempool_refill(mempool_t *pool);
extern void mempool_destroy(mempool_t *pool);
extern void * mempool_alloc(mempool_t *pool, gfp_t gfp_mask);
extern void mempool_free(void *element, mempool_t *pool);
diff --git a/mm/mempool.c b/mm/mempool.c
index 004d42b..139c477 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -223,6 +223,47 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,
EXPORT_SYMBOL(mempool_create_node);

/**
+ * mempool_refill - refill an existing memory pool immediately
+ * @pool: pointer to the memory pool which was allocated via
+ * mempool_create().
+ *
+ * This function tries to refill the pool with new elements
+ * immediately. Similar with mempool_resize(), it cannot be
+ * guaranteed that the pool will be fully filled immediately.
+ *
+ * Note, the caller must guarantee that no mempool_destroy is called
+ * while this function is running. mempool_alloc() & mempool_free()
+ * might be called (eg. from IRQ contexts) while this function executes.
+ */
+void mempool_refill(mempool_t *pool)
+{
+ void *element;
+ unsigned long flags;
+
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr >= pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ return;
+ }
+
+ while (pool->curr_nr < pool->min_nr) {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ element = pool->alloc(GFP_KERNEL, pool->pool_data);
+ if (!element)
+ return;
+ spin_lock_irqsave(&pool->lock, flags);
+ if (pool->curr_nr < pool->min_nr) {
+ add_element(pool, element);
+ } else {
+ spin_unlock_irqrestore(&pool->lock, flags);
+ pool->free(element, pool->pool_data); /* Raced */
+ return;
+ }
+ }
+}
+EXPORT_SYMBOL(mempool_refill);
+
+/**
* mempool_resize - resize an existing memory pool
* @pool: pointer to the memory pool which was allocated via
* mempool_create().
@@ -256,7 +297,8 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
spin_lock_irqsave(&pool->lock, flags);
}
pool->min_nr = new_min_nr;
- goto out_unlock;
+ spin_unlock_irqrestore(&pool->lock, flags);
+ goto out;
}
spin_unlock_irqrestore(&pool->lock, flags);

@@ -279,22 +321,9 @@ int mempool_resize(mempool_t *pool, int new_min_nr)
pool->elements = new_elements;
pool->min_nr = new_min_nr;

- while (pool->curr_nr < pool->min_nr) {
- spin_unlock_irqrestore(&pool->lock, flags);
- element = pool->alloc(GFP_KERNEL, pool->pool_data);
- if (!element)
- goto out;
- spin_lock_irqsave(&pool->lock, flags);
- if (pool->curr_nr < pool->min_nr) {
- add_element(pool, element);
- } else {
- spin_unlock_irqrestore(&pool->lock, flags);
- pool->free(element, pool->pool_data); /* Raced */
- goto out;
- }
- }
-out_unlock:
spin_unlock_irqrestore(&pool->lock, flags);
+
+ mempool_refill(pool);
out:
return 0;
}
--
1.9.1

2015-12-15 21:27:21

by Johannes Weiner

[permalink] [raw]
Subject: Re: [PATCH] mm: mempool: Factor out mempool_refill()

On Mon, Dec 14, 2015 at 11:09:43AM +0000, Wang, Zhi A wrote:
> This patch factors out mempool_refill() from mempool_resize(). It's reasonable
> that the mempool user wants to refill the pool immdiately when it has chance
> e.g. inside a sleepible context, so that next time in the IRQ context the pool
> would have much more available elements to allocate.
>
> After the refactor, mempool_refill() can also executes with mempool_resize()
> /mempool_alloc/mempool_free() or another mempool_refill().
>
> Signed-off-by: Zhi Wang <[email protected]>

Who is going to call that function? Adding a new interace usually
comes with a user, or as part of a series that adds users.

2015-12-16 03:20:33

by Wang, Zhi A

[permalink] [raw]
Subject: Re: [PATCH] mm: mempool: Factor out mempool_refill()

Hi Johannes:
Thanks for the reply. In the end of the mempool_resize(), it will
call the mempool_refill() to do the rest of the work. So this is not one
of the "no-caller" case. If you insist this is a "no-caller" case,
perhaps I should change it to a "static" function without exposing a new
interface?

Personally I think mempool_refill() should be one of the typical
interfaces in an implementation of a mempool. Currently the mempool will
not grow only if pool->min_nr > new_min_nr.

So when user wants to refill the mempool immediately, not resize a
mempool, in the current implementation, it has to do 2x
mempool_resize(). First one is mempool_resize(pool->min_nr - 1), second
one is mempool_resize(new_min_nr). So the refill action would truly
happen. This is ugly and not convenient.

On 12/16/15 05:26, Johannes Weiner wrote:
> On Mon, Dec 14, 2015 at 11:09:43AM +0000, Wang, Zhi A wrote:
>> This patch factors out mempool_refill() from mempool_resize(). It's reasonable
>> that the mempool user wants to refill the pool immdiately when it has chance
>> e.g. inside a sleepible context, so that next time in the IRQ context the pool
>> would have much more available elements to allocate.
>>
>> After the refactor, mempool_refill() can also executes with mempool_resize()
>> /mempool_alloc/mempool_free() or another mempool_refill().
>>
>> Signed-off-by: Zhi Wang <[email protected]>
>
> Who is going to call that function? Adding a new interace usually
> comes with a user, or as part of a series that adds users.
>