From: Eric Dumazet <eric.dumazet@gmail.com>
To: Alexandru Copot <alex.mihai.c@gmail.com>
Cc: davem@davemloft.net, gerrit@erg.abdn.ac.uk, kuznet@ms2.inr.ac.ru,
jmorris@namei.org, yoshfuji@linux-ipv6.org, kaber@trash.net,
netdev@vger.kernel.org, Daniel Baluta <dbaluta@ixiacom.com>,
Lucian Grijincu <lucian.grijincu@gmail.com>
Subject: Re: [RFC PATCH 4/4] inet: use second hash in inet_csk_get_port
Date: Wed, 30 May 2012 19:20:56 +0200 [thread overview]
Message-ID: <1338398456.2760.338.camel@edumazet-glaptop> (raw)
In-Reply-To: <1338363410-6562-5-git-send-email-alex.mihai.c@gmail.com>
On Wed, 2012-05-30 at 10:36 +0300, Alexandru Copot wrote:
> +struct inet_bind_bucket *
> +inet4_find_bind_buckets(struct sock *sk,
> + unsigned short port,
> + struct inet_bind_hashbucket **p_bhead,
> + struct inet_bind_hashbucket **p_portaddr_bhead)
> +{
> + struct net *net = sock_net(sk);
> + struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;
> + struct inet_bind_bucket *tb = NULL;
> + struct hlist_node *node;
> +
> + struct inet_bind_hashbucket *bhead, *portaddr_bhead, *portaddrany_bhead;
> + bhead = &hinfo->bhash[inet_bhashfn(net, port, hinfo->bhash_size)];
> + portaddr_bhead = inet4_portaddr_hashbucket(hinfo, net,
> + sk_rcv_saddr(sk), port);
> + portaddrany_bhead = inet4_portaddr_hashbucket(hinfo, net,
> + INADDR_ANY, port);
> +
> + *p_portaddr_bhead = portaddr_bhead;
> + *p_bhead = bhead;
> +
> + /*
> + * prevent dead locks by always taking locks in a fixed order:
> + * - always take the port-only lock first. This is done because in some
> + * other places this is the lock taken, being folllowed in only some
> + * cases by the portaddr lock.
> + * - between portaddr and portaddrany always choose the one with the
> + * lower address. Unlock ordering is not important, as long as the
> + * locking order is consistent.
> + * - make sure to not take the same lock twice
> + */
> + spin_lock(&bhead->lock);
> + if (portaddr_bhead > portaddrany_bhead) {
> + spin_lock(&portaddrany_bhead->lock);
> + spin_lock(&portaddr_bhead->lock);
> + } else if (portaddr_bhead < portaddrany_bhead) {
> + spin_lock(&portaddr_bhead->lock);
> + spin_lock(&portaddrany_bhead->lock);
> + } else {
> + spin_lock(&portaddr_bhead->lock);
> + }
> +
> + if (sk_rcv_saddr(sk) != INADDR_ANY) {
> + struct inet_bind_hashbucket *_head;
> +
> + _head = portaddr_bhead;
> + if (bhead->count < portaddr_bhead->count) {
> + _head = bhead;
> + inet_bind_bucket_for_each(tb, node, &_head->chain)
> + if ((net_eq(ib_net(tb), net)) &&
> + (tb->port == port) &&
> + (tb->ib_addr_ipv4 == sk_rcv_saddr(sk)))
> + goto found;
> + } else {
> + inet_portaddr_bind_bucket_for_each(tb, node, &_head->chain)
> + if ((net_eq(ib_net(tb), net)) &&
> + (tb->port == port) &&
> + (tb->ib_addr_ipv4 == sk_rcv_saddr(sk)))
> + goto found;
> + }
> + _head = portaddrany_bhead;
> + if (bhead->count < portaddrany_bhead->count) {
> + _head = bhead;
> + inet_bind_bucket_for_each(tb, node, &_head->chain)
> + if ((ib_net(tb) == net) &&
> + (tb->port == port) &&
> + (tb->ib_addr_ipv4 == INADDR_ANY))
> + goto found;
> + } else {
> + inet_portaddr_bind_bucket_for_each(tb, node, &_head->chain)
> + if ((ib_net(tb) == net) &&
> + (tb->port == port) &&
> + (tb->ib_addr_ipv4 == INADDR_ANY))
> + goto found;
> + }
> + } else {
> + inet_bind_bucket_for_each(tb, node, &bhead->chain)
> + if ((ib_net(tb) == net) && (tb->port == port))
> + goto found;
> + }
> +
> + tb = NULL;
> +found:
> + if (portaddr_bhead != portaddrany_bhead)
> + spin_unlock(&portaddrany_bhead->lock);
> +
> + /* the other locks remain taken, as the caller
> + * may want to change the hash tabels */
> + return tb;
> +}
> +
> +
How this is going to work with IPv6 sockets in the middle of the
chains ?
Also, comments are not properly formatted, they should all look like :
/* the other locks remain taken, as the caller
* may want to change the hash tables
*/
And finally, make sure LOCKDEP is happy with your locking code.
next prev parent reply other threads:[~2012-05-30 17:21 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-30 7:36 [RFC PATCH 0/4] inet: add second hash table Alexandru Copot
2012-05-30 7:36 ` [RFC PATCH 1/4] inet: add counter to inet_bind_hashbucket Alexandru Copot
2012-05-30 8:00 ` Eric Dumazet
2012-05-30 7:36 ` [RFC PATCH 2/4] inet: add a second bind hash Alexandru Copot
2012-05-30 7:36 ` [RFC PATCH 3/4] inet: add/remove inet buckets in the " Alexandru Copot
2012-05-30 7:36 ` [RFC PATCH 4/4] inet: use second hash in inet_csk_get_port Alexandru Copot
2012-05-30 16:42 ` Eric Dumazet
2012-05-30 17:20 ` Eric Dumazet [this message]
2012-05-30 19:11 ` Alexandru Copot
2012-05-30 7:57 ` [RFC PATCH 0/4] inet: add second hash table Eric Dumazet
2012-05-30 12:32 ` Daniel Baluta
2012-05-30 12:41 ` Eric Dumazet
2012-05-30 16:27 ` Ben Greear
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=1338398456.2760.338.camel@edumazet-glaptop \
--to=eric.dumazet@gmail.com \
--cc=alex.mihai.c@gmail.com \
--cc=davem@davemloft.net \
--cc=dbaluta@ixiacom.com \
--cc=gerrit@erg.abdn.ac.uk \
--cc=jmorris@namei.org \
--cc=kaber@trash.net \
--cc=kuznet@ms2.inr.ac.ru \
--cc=lucian.grijincu@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=yoshfuji@linux-ipv6.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