Skip to content

Commit

Permalink
rhashtable: Fix sleeping inside RCU critical section in walk_stop
Browse files Browse the repository at this point in the history
The commit 963ecbd ("rhashtable:
Fix use-after-free in rhashtable_walk_stop") fixed a real bug
but created another one because we may end up sleeping inside an
RCU critical section.

This patch fixes it properly by replacing the mutex with a spin
lock that specifically protects the walker lists.

Reported-by: Sasha Levin <sasha.levin@oracle.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Herbert Xu authored and David S. Miller committed Mar 24, 2015
1 parent ce046c5 commit ba7c95e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/linux/rhashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct rhashtable_params {
* @p: Configuration parameters
* @run_work: Deferred worker to expand/shrink asynchronously
* @mutex: Mutex to protect current/future table swapping
* @lock: Spin lock to protect walker list
* @being_destroyed: True if table is set up for destruction
*/
struct rhashtable {
Expand All @@ -144,6 +145,7 @@ struct rhashtable {
struct rhashtable_params p;
struct work_struct run_work;
struct mutex mutex;
spinlock_t lock;
};

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ static int rhashtable_rehash_table(struct rhashtable *ht)
/* Publish the new table pointer. */
rcu_assign_pointer(ht->tbl, new_tbl);

spin_lock(&ht->lock);
list_for_each_entry(walker, &old_tbl->walkers, list)
walker->tbl = NULL;
spin_unlock(&ht->lock);

/* Wait for readers. All new readers will see the new
* table, and thus no references to the old table will
Expand Down Expand Up @@ -635,12 +637,12 @@ void rhashtable_walk_stop(struct rhashtable_iter *iter)

ht = iter->ht;

mutex_lock(&ht->mutex);
spin_lock(&ht->lock);
if (tbl->rehash < tbl->size)
list_add(&iter->walker->list, &tbl->walkers);
else
iter->walker->tbl = NULL;
mutex_unlock(&ht->mutex);
spin_unlock(&ht->lock);

iter->p = NULL;

Expand Down Expand Up @@ -723,6 +725,7 @@ int rhashtable_init(struct rhashtable *ht,

memset(ht, 0, sizeof(*ht));
mutex_init(&ht->mutex);
spin_lock_init(&ht->lock);
memcpy(&ht->p, params, sizeof(*params));

if (params->min_size)
Expand Down

0 comments on commit ba7c95e

Please sign in to comment.