linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: NeilBrown <neilb@suse.com>
To: Thomas Graf <tgraf@suug.ch>, Herbert Xu <herbert@gondor.apana.org.au>
Cc: netdev@vger.kernel.org,
	"Paul E. McKenney" <paulmck@linux.vnet.ibm.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH 1/3] rhashtable: use cmpxchg() in nested_table_alloc()
Date: Thu, 14 Mar 2019 16:05:28 +1100	[thread overview]
Message-ID: <155253992824.5022.10318044793639169265.stgit@noble.brown> (raw)
In-Reply-To: <155253979234.5022.1840929790507376038.stgit@noble.brown>

nested_table_alloc() relies on the fact that there is
at most one spinlock allocated for every slot in the top
level nested table, so it is not possible for two threads
to try to allocate the same table at the same time.

This assumption is a little fragile (it is not explicit) and is
unnecessary.  We can instead protect against
a race using cmpxchg() - if it the cmp fails, free the table
that was just allocated.

Signed-off-by: NeilBrown <neilb@suse.com>
---
 lib/rhashtable.c |    8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 0a105d4af166..c983c0ee15d5 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -131,9 +131,11 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht,
 			INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
 	}
 
-	rcu_assign_pointer(*prev, ntbl);
-
-	return ntbl;
+	if (cmpxchg(prev, NULL, ntbl) == NULL)
+		return ntbl;
+	/* Raced with another thread. */
+	kfree(ntbl);
+	return rcu_dereference(*prev);
 }
 
 static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,



  parent reply	other threads:[~2019-03-14  5:06 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-14  5:05 [PATCH 0/3] Three rhashtable improvements NeilBrown
2019-03-14  5:05 ` [PATCH 3/3] rhashtable: rename rht_for_each*continue as *from NeilBrown
2019-03-15  5:49   ` Herbert Xu
2019-03-17  7:50   ` Miguel Ojeda
2019-03-18  0:15     ` NeilBrown
2019-03-14  5:05 ` [PATCH 2/3] rhashtable: don't hold lock on first table throughout insertion NeilBrown
2019-03-14 14:58   ` Paul E. McKenney
2019-03-15  5:47   ` Herbert Xu
2019-03-15  6:46     ` NeilBrown
2019-03-20  5:40       ` Herbert Xu
2019-03-14  5:05 ` NeilBrown [this message]
2019-03-15  5:10   ` [PATCH 1/3] rhashtable: use cmpxchg() in nested_table_alloc() Herbert Xu
2019-03-15  6:51     ` NeilBrown
2019-03-20  5:41       ` Herbert Xu

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=155253992824.5022.10318044793639169265.stgit@noble.brown \
    --to=neilb@suse.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=paulmck@linux.vnet.ibm.com \
    --cc=tgraf@suug.ch \
    /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).