All of lore.kernel.org
 help / color / mirror / Atom feed
From: Herbert Xu <herbert@gondor.apana.org.au>
To: David Miller <davem@redhat.com>
Cc: eric.dumazet@gmail.com, tgraf@suug.ch, netdev@vger.kernel.org,
	ying.xue@windriver.com
Subject: [net] netlink: Make autobind rover an atomic_t
Date: Sat, 16 May 2015 21:40:07 +0800	[thread overview]
Message-ID: <20150516134007.GA1152@gondor.apana.org.au> (raw)
In-Reply-To: <20150516123242.GA683@gondor.apana.org.au>

The commit 21e4902aea80ef35afc00ee8d2abdea4f519b7f7 ("netlink:
Lockless lookup with RCU grace period in socket release") removed
the locks around the autobind rover without making the rover itself
safe for use by multiple threads.

This patch converts rover to an atomic_t to make it at least
somewhat safe to use locklessly.  The tricky bit is when the
rover wraps around.  This patch simply deals with it by blindly
doing an atomic_set.  So if many threads encounter the wraparound
simultaneously then they'll all step on each other's toes and
all try to bind to -4097.  But this should eventually sort itself
out as they loop around and try the atomic_dec_return after the
last thread does an atomic_set.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index ec4adbd..6ffce5b 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -1292,24 +1292,27 @@ static int netlink_release(struct socket *sock)
 static int netlink_autobind(struct socket *sock)
 {
 	struct sock *sk = sock->sk;
+	struct sock *sk2;
 	struct net *net = sock_net(sk);
 	struct netlink_table *table = &nl_table[sk->sk_protocol];
 	s32 portid = task_tgid_vnr(current);
 	int err;
-	static s32 rover = -4097;
+	static atomic_t rover = ATOMIC_INIT(-4096);
 
 retry:
 	cond_resched();
 	rcu_read_lock();
-	if (__netlink_lookup(table, portid, net)) {
+	sk2 = __netlink_lookup(table, portid, net);
+	rcu_read_unlock();
+	if (sk2) {
 		/* Bind collision, search negative portid values. */
-		portid = rover--;
-		if (rover > -4097)
-			rover = -4097;
-		rcu_read_unlock();
+		portid = atomic_dec_return(&rover);
+		if (unlikely(portid > -4097)) {
+			atomic_set(&rover, -4097);
+			portid = -4097;
+		}
 		goto retry;
 	}
-	rcu_read_unlock();
 
 	err = netlink_insert(sk, portid);
 	if (err == -EADDRINUSE)
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

  reply	other threads:[~2015-05-16 13:40 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-13  5:30 netlink & rhashtable status Eric Dumazet
2015-05-13  5:40 ` Herbert Xu
2015-05-13  6:15   ` Eric Dumazet
2015-05-13  6:20     ` Herbert Xu
2015-05-13 13:04       ` Eric Dumazet
2015-05-13 16:18         ` Eric Dumazet
2015-05-13 16:35           ` David Miller
2015-05-14  2:55             ` Herbert Xu
2015-05-14  2:53           ` Herbert Xu
2015-05-14  3:17             ` Eric Dumazet
2015-05-14  3:34               ` Herbert Xu
2015-05-14  3:58                 ` Eric Dumazet
2015-05-14  4:13                   ` Eric Dumazet
2015-05-14  4:16                     ` Herbert Xu
2015-05-14  4:21                       ` Herbert Xu
2015-05-14  4:38                         ` Eric Dumazet
2015-05-14  5:03                           ` Herbert Xu
2015-05-14  5:56                         ` Red Hat INTERNAL-ONLY kernel discussion list <rhkernel-list@redhat.com> Herbert Xu
2015-05-14  5:58                         ` netlink: Disable insertions/removals during rehash Herbert Xu
2015-05-14  6:02                           ` netlink: Kill bogus lock_sock in netlink_insert Herbert Xu
2015-05-15 16:49                             ` David Miller
2015-05-15 18:01                               ` Eric Dumazet
2015-05-16 16:50                                 ` Eric Dumazet
2015-05-16 20:58                                   ` David Miller
2015-05-15 17:02                             ` David Miller
2015-05-16 12:32                               ` Herbert Xu
2015-05-16 13:40                                 ` Herbert Xu [this message]
2015-05-16 13:50                                   ` [net] netlink: Reset portid after netlink_insert failure Herbert Xu
2015-05-16 21:09                                     ` David Miller
2015-05-16 21:08                                   ` [net] netlink: Make autobind rover an atomic_t David Miller
2015-05-17  2:45                                     ` [net-next] netlink: Use random autobind rover Herbert Xu
2015-05-18  3:44                                       ` David Miller
2015-05-14 14:37                           ` netlink: Disable insertions/removals during rehash Eric Dumazet
2015-05-15  0:06                             ` Herbert Xu
2015-05-20 23:53                               ` Thomas Graf
2015-05-21  0:31                                 ` Eric Dumazet
2015-05-15 17:02                           ` David Miller
2015-05-16 13:16                             ` Herbert Xu
2015-05-16 21:10                               ` David Miller
2015-06-04 16:27                                 ` Guenter Roeck
2015-06-04 18:59                                   ` David Miller
2015-06-04 20:44                                     ` Eric Dumazet
2015-06-04 20:58                                     ` Guenter Roeck
2015-06-05  3:52                                   ` Herbert Xu
2015-06-05  5:27                                     ` Guenter Roeck
2015-06-26 10:44                         ` netlink & rhashtable status Konstantin Khlebnikov
2015-06-27  7:09                           ` Herbert Xu
2015-05-14  4:17                     ` Eric Dumazet

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=20150516134007.GA1152@gondor.apana.org.au \
    --to=herbert@gondor.apana.org.au \
    --cc=davem@redhat.com \
    --cc=eric.dumazet@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=tgraf@suug.ch \
    --cc=ying.xue@windriver.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.