From: Sage Weil <sage@newdream.net>
To: linux-kernel@vger.kernel.org
Cc: Sage Weil <sage@newdream.net>, Neil Brown <neilb@suse.de>,
linux-raid@vger.kernel.org, linux-mm@kvack.org,
stable@kernel.org
Subject: [PATCH] mempool: launder reused items from kzalloc pool
Date: Tue, 28 Jul 2009 13:46:07 -0700 [thread overview]
Message-ID: <1248813967-27448-1-git-send-email-sage@newdream.net> (raw)
The kzalloc pool created by mempool_create_kzalloc_pool() only zeros items
the first time they are allocated; it doesn't re-zero freed items that are
returned to the pool. This only comes up when the pool is used in the
first place (when memory is very low).
Fix this by adding a mempool_launder_t method that is called before
returning items to the pool, and set it in mempool_create_kzalloc_pool.
This preserves the use of __GFP_ZERO in the common case where the pool
isn't touched at all.
There are currently two in-tree users of mempool_create_kzalloc_pool:
drivers/md/multipath.c
drivers/scsi/ibmvscsi/ibmvfc.c
The first appears to be affected by this bug. The second manually zeros
each allocation, and can stop doing so after this is fixed.
Alternatively, mempool_create_kzalloc_pool() could be removed entirely and
the callers could zero allocations themselves.
CC: Neil Brown <neilb@suse.de>
CC: <linux-raid@vger.kernel.org>
CC: <linux-kernel@vger.kernel.org>
CC: <linux-mm@kvack.org>
CC: <stable@kernel.org>
Signed-off-by: Sage Weil <sage@newdream.net>
---
include/linux/mempool.h | 10 ++++++++--
mm/mempool.c | 9 +++++++++
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/include/linux/mempool.h b/include/linux/mempool.h
index 9be484d..889c7e1 100644
--- a/include/linux/mempool.h
+++ b/include/linux/mempool.h
@@ -10,6 +10,7 @@ struct kmem_cache;
typedef void * (mempool_alloc_t)(gfp_t gfp_mask, void *pool_data);
typedef void (mempool_free_t)(void *element, void *pool_data);
+typedef void (mempool_launder_t)(void *element, void *pool_data);
typedef struct mempool_s {
spinlock_t lock;
@@ -20,6 +21,7 @@ typedef struct mempool_s {
void *pool_data;
mempool_alloc_t *alloc;
mempool_free_t *free;
+ mempool_launder_t *launder;
wait_queue_head_t wait;
} mempool_t;
@@ -52,6 +54,7 @@ mempool_create_slab_pool(int min_nr, struct kmem_cache *kc)
*/
void *mempool_kmalloc(gfp_t gfp_mask, void *pool_data);
void *mempool_kzalloc(gfp_t gfp_mask, void *pool_data);
+void mempool_rezero(void *element, void *pool_data);
void mempool_kfree(void *element, void *pool_data);
static inline mempool_t *mempool_create_kmalloc_pool(int min_nr, size_t size)
{
@@ -60,8 +63,11 @@ static inline mempool_t *mempool_create_kmalloc_pool(int min_nr, size_t size)
}
static inline mempool_t *mempool_create_kzalloc_pool(int min_nr, size_t size)
{
- return mempool_create(min_nr, mempool_kzalloc, mempool_kfree,
- (void *) size);
+ mempool_t *pool = mempool_create(min_nr, mempool_kzalloc, mempool_kfree,
+ (void *) size);
+ if (pool)
+ pool->launder = mempool_rezero;
+ return pool;
}
/*
diff --git a/mm/mempool.c b/mm/mempool.c
index a46eb1b..6bb3056 100644
--- a/mm/mempool.c
+++ b/mm/mempool.c
@@ -269,6 +269,8 @@ void mempool_free(void *element, mempool_t *pool)
if (pool->curr_nr < pool->min_nr) {
spin_lock_irqsave(&pool->lock, flags);
if (pool->curr_nr < pool->min_nr) {
+ if (pool->launder)
+ pool->launder(element, pool->pool_data);
add_element(pool, element);
spin_unlock_irqrestore(&pool->lock, flags);
wake_up(&pool->wait);
@@ -315,6 +317,13 @@ void *mempool_kzalloc(gfp_t gfp_mask, void *pool_data)
}
EXPORT_SYMBOL(mempool_kzalloc);
+void mempool_rezero(void *element, void *pool_data)
+{
+ size_t size = (size_t) pool_data;
+ memset(element, 0, size);
+}
+EXPORT_SYMBOL(mempool_rezero);
+
void mempool_kfree(void *element, void *pool_data)
{
kfree(element);
--
1.5.6.5
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next reply other threads:[~2009-07-28 20:46 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-28 20:46 Sage Weil [this message]
2009-08-03 20:20 ` [PATCH] mempool: launder reused items from kzalloc pool Andrew Morton
2009-08-03 20:37 ` Sage Weil
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1248813967-27448-1-git-send-email-sage@newdream.net \
--to=sage@newdream.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-raid@vger.kernel.org \
--cc=neilb@suse.de \
--cc=stable@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).