Commit 66d1bbed authored by Andrew Morton's avatar Andrew Morton Committed by Linus Torvalds

[PATCH] Add barriers to avoid race in mempool_alloc/free

From: Chris Mason <mason@suse.com>

mempool_alloc() and mempool_free() check pool->curr_nr without any locks
held.  This can lead to skipping a wakeup when there are people waiting,
and sleeping when there are free elements in the pool.

I can't trigger this reliably, but sooner or later someone on ppc is
probably going to hit it.
parent e798a41d
...@@ -209,6 +209,7 @@ void * mempool_alloc(mempool_t *pool, int gfp_mask) ...@@ -209,6 +209,7 @@ void * mempool_alloc(mempool_t *pool, int gfp_mask)
* If the pool is less than 50% full and we can perform effective * If the pool is less than 50% full and we can perform effective
* page reclaim then try harder to allocate an element. * page reclaim then try harder to allocate an element.
*/ */
mb();
if ((gfp_mask & __GFP_FS) && (gfp_mask != gfp_nowait) && if ((gfp_mask & __GFP_FS) && (gfp_mask != gfp_nowait) &&
(pool->curr_nr <= pool->min_nr/2)) { (pool->curr_nr <= pool->min_nr/2)) {
element = pool->alloc(gfp_mask, pool->pool_data); element = pool->alloc(gfp_mask, pool->pool_data);
...@@ -236,6 +237,7 @@ void * mempool_alloc(mempool_t *pool, int gfp_mask) ...@@ -236,6 +237,7 @@ void * mempool_alloc(mempool_t *pool, int gfp_mask)
blk_run_queues(); blk_run_queues();
prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
mb();
if (!pool->curr_nr) if (!pool->curr_nr)
io_schedule(); io_schedule();
finish_wait(&pool->wait, &wait); finish_wait(&pool->wait, &wait);
...@@ -256,6 +258,7 @@ void mempool_free(void *element, mempool_t *pool) ...@@ -256,6 +258,7 @@ void mempool_free(void *element, mempool_t *pool)
{ {
unsigned long flags; unsigned long flags;
mb();
if (pool->curr_nr < pool->min_nr) { if (pool->curr_nr < pool->min_nr) {
spin_lock_irqsave(&pool->lock, flags); spin_lock_irqsave(&pool->lock, flags);
if (pool->curr_nr < pool->min_nr) { if (pool->curr_nr < pool->min_nr) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment