net: listening_hash get a spinlock per bucket

This patch prepares RCU migration of listening_hash table for
TCP/DCCP protocols.

listening_hash table being small (32 slots per protocol), we add
a spinlock for each slot, instead of a single rwlock for whole table.

This should reduce hold time of readers, and writers concurrency.

Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Eric Dumazet 2008-11-20 00:40:07 -08:00 коммит произвёл David S. Miller
Родитель d8b83c57a7
Коммит 5caea4ea70
6 изменённых файлов: 79 добавлений и 119 удалений

Просмотреть файл

@ -99,6 +99,11 @@ struct inet_bind_hashbucket {
struct hlist_head chain; struct hlist_head chain;
}; };
struct inet_listen_hashbucket {
spinlock_t lock;
struct hlist_head head;
};
/* This is for listening sockets, thus all sockets which possess wildcards. */ /* This is for listening sockets, thus all sockets which possess wildcards. */
#define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */ #define INET_LHTABLE_SIZE 32 /* Yes, really, this is all you need. */
@ -123,22 +128,21 @@ struct inet_hashinfo {
unsigned int bhash_size; unsigned int bhash_size;
/* Note : 4 bytes padding on 64 bit arches */ /* Note : 4 bytes padding on 64 bit arches */
/* All sockets in TCP_LISTEN state will be in here. This is the only struct kmem_cache *bind_bucket_cachep;
* table where wildcard'd TCP sockets can exist. Hash function here
* is just local port number.
*/
struct hlist_head listening_hash[INET_LHTABLE_SIZE];
/* All the above members are written once at bootup and /* All the above members are written once at bootup and
* never written again _or_ are predominantly read-access. * never written again _or_ are predominantly read-access.
* *
* Now align to a new cache line as all the following members * Now align to a new cache line as all the following members
* are often dirty. * might be often dirty.
*/ */
rwlock_t lhash_lock ____cacheline_aligned; /* All sockets in TCP_LISTEN state will be in here. This is the only
atomic_t lhash_users; * table where wildcard'd TCP sockets can exist. Hash function here
wait_queue_head_t lhash_wait; * is just local port number.
struct kmem_cache *bind_bucket_cachep; */
struct inet_listen_hashbucket listening_hash[INET_LHTABLE_SIZE]
____cacheline_aligned_in_smp;
}; };
static inline struct inet_ehash_bucket *inet_ehash_bucket( static inline struct inet_ehash_bucket *inet_ehash_bucket(
@ -236,26 +240,7 @@ extern void __inet_inherit_port(struct sock *sk, struct sock *child);
extern void inet_put_port(struct sock *sk); extern void inet_put_port(struct sock *sk);
extern void inet_listen_wlock(struct inet_hashinfo *hashinfo); void inet_hashinfo_init(struct inet_hashinfo *h);
/*
* - We may sleep inside this lock.
* - If sleeping is not required (or called from BH),
* use plain read_(un)lock(&inet_hashinfo.lhash_lock).
*/
static inline void inet_listen_lock(struct inet_hashinfo *hashinfo)
{
/* read_lock synchronizes to candidates to writers */
read_lock(&hashinfo->lhash_lock);
atomic_inc(&hashinfo->lhash_users);
read_unlock(&hashinfo->lhash_lock);
}
static inline void inet_listen_unlock(struct inet_hashinfo *hashinfo)
{
if (atomic_dec_and_test(&hashinfo->lhash_users))
wake_up(&hashinfo->lhash_wait);
}
extern void __inet_hash_nolisten(struct sock *sk); extern void __inet_hash_nolisten(struct sock *sk);
extern void inet_hash(struct sock *sk); extern void inet_hash(struct sock *sk);

Просмотреть файл

@ -44,12 +44,7 @@ atomic_t dccp_orphan_count = ATOMIC_INIT(0);
EXPORT_SYMBOL_GPL(dccp_orphan_count); EXPORT_SYMBOL_GPL(dccp_orphan_count);
struct inet_hashinfo __cacheline_aligned dccp_hashinfo = { struct inet_hashinfo dccp_hashinfo;
.lhash_lock = RW_LOCK_UNLOCKED,
.lhash_users = ATOMIC_INIT(0),
.lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
};
EXPORT_SYMBOL_GPL(dccp_hashinfo); EXPORT_SYMBOL_GPL(dccp_hashinfo);
/* the maximum queue length for tx in packets. 0 is no limit */ /* the maximum queue length for tx in packets. 0 is no limit */
@ -1030,6 +1025,7 @@ static int __init dccp_init(void)
BUILD_BUG_ON(sizeof(struct dccp_skb_cb) > BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
FIELD_SIZEOF(struct sk_buff, cb)); FIELD_SIZEOF(struct sk_buff, cb));
inet_hashinfo_init(&dccp_hashinfo);
dccp_hashinfo.bind_bucket_cachep = dccp_hashinfo.bind_bucket_cachep =
kmem_cache_create("dccp_bind_bucket", kmem_cache_create("dccp_bind_bucket",
sizeof(struct inet_bind_bucket), 0, sizeof(struct inet_bind_bucket), 0,

Просмотреть файл

@ -718,13 +718,15 @@ static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
goto skip_listen_ht; goto skip_listen_ht;
inet_listen_lock(hashinfo);
for (i = s_i; i < INET_LHTABLE_SIZE; i++) { for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
struct sock *sk; struct sock *sk;
struct hlist_node *node; struct hlist_node *node;
struct inet_listen_hashbucket *ilb;
num = 0; num = 0;
sk_for_each(sk, node, &hashinfo->listening_hash[i]) { ilb = &hashinfo->listening_hash[i];
spin_lock_bh(&ilb->lock);
sk_for_each(sk, node, &ilb->head) {
struct inet_sock *inet = inet_sk(sk); struct inet_sock *inet = inet_sk(sk);
if (num < s_num) { if (num < s_num) {
@ -742,7 +744,7 @@ static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
goto syn_recv; goto syn_recv;
if (inet_csk_diag_dump(sk, skb, cb) < 0) { if (inet_csk_diag_dump(sk, skb, cb) < 0) {
inet_listen_unlock(hashinfo); spin_unlock_bh(&ilb->lock);
goto done; goto done;
} }
@ -751,7 +753,7 @@ syn_recv:
goto next_listen; goto next_listen;
if (inet_diag_dump_reqs(skb, sk, cb) < 0) { if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
inet_listen_unlock(hashinfo); spin_unlock_bh(&ilb->lock);
goto done; goto done;
} }
@ -760,12 +762,12 @@ next_listen:
cb->args[4] = 0; cb->args[4] = 0;
++num; ++num;
} }
spin_unlock_bh(&ilb->lock);
s_num = 0; s_num = 0;
cb->args[3] = 0; cb->args[3] = 0;
cb->args[4] = 0; cb->args[4] = 0;
} }
inet_listen_unlock(hashinfo);
skip_listen_ht: skip_listen_ht:
cb->args[0] = 1; cb->args[0] = 1;
s_i = num = s_num = 0; s_i = num = s_num = 0;

Просмотреть файл

@ -110,35 +110,6 @@ void __inet_inherit_port(struct sock *sk, struct sock *child)
EXPORT_SYMBOL_GPL(__inet_inherit_port); EXPORT_SYMBOL_GPL(__inet_inherit_port);
/*
* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it can be very bad on SMP.
* Look, when several writers sleep and reader wakes them up, all but one
* immediately hit write lock and grab all the cpus. Exclusive sleep solves
* this, _but_ remember, it adds useless work on UP machines (wake up each
* exclusive lock release). It should be ifdefed really.
*/
void inet_listen_wlock(struct inet_hashinfo *hashinfo)
__acquires(hashinfo->lhash_lock)
{
write_lock(&hashinfo->lhash_lock);
if (atomic_read(&hashinfo->lhash_users)) {
DEFINE_WAIT(wait);
for (;;) {
prepare_to_wait_exclusive(&hashinfo->lhash_wait,
&wait, TASK_UNINTERRUPTIBLE);
if (!atomic_read(&hashinfo->lhash_users))
break;
write_unlock_bh(&hashinfo->lhash_lock);
schedule();
write_lock_bh(&hashinfo->lhash_lock);
}
finish_wait(&hashinfo->lhash_wait, &wait);
}
}
/* /*
* Don't inline this cruft. Here are some nice properties to exploit here. The * Don't inline this cruft. Here are some nice properties to exploit here. The
* BSD API does not allow a listening sock to specify the remote port nor the * BSD API does not allow a listening sock to specify the remote port nor the
@ -191,25 +162,25 @@ struct sock *__inet_lookup_listener(struct net *net,
const int dif) const int dif)
{ {
struct sock *sk = NULL; struct sock *sk = NULL;
const struct hlist_head *head; struct inet_listen_hashbucket *ilb;
read_lock(&hashinfo->lhash_lock); ilb = &hashinfo->listening_hash[inet_lhashfn(net, hnum)];
head = &hashinfo->listening_hash[inet_lhashfn(net, hnum)]; spin_lock(&ilb->lock);
if (!hlist_empty(head)) { if (!hlist_empty(&ilb->head)) {
const struct inet_sock *inet = inet_sk((sk = __sk_head(head))); const struct inet_sock *inet = inet_sk((sk = __sk_head(&ilb->head)));
if (inet->num == hnum && !sk->sk_node.next && if (inet->num == hnum && !sk->sk_node.next &&
(!inet->rcv_saddr || inet->rcv_saddr == daddr) && (!inet->rcv_saddr || inet->rcv_saddr == daddr) &&
(sk->sk_family == PF_INET || !ipv6_only_sock(sk)) && (sk->sk_family == PF_INET || !ipv6_only_sock(sk)) &&
!sk->sk_bound_dev_if && net_eq(sock_net(sk), net)) !sk->sk_bound_dev_if && net_eq(sock_net(sk), net))
goto sherry_cache; goto sherry_cache;
sk = inet_lookup_listener_slow(net, head, daddr, hnum, dif); sk = inet_lookup_listener_slow(net, &ilb->head, daddr, hnum, dif);
} }
if (sk) { if (sk) {
sherry_cache: sherry_cache:
sock_hold(sk); sock_hold(sk);
} }
read_unlock(&hashinfo->lhash_lock); spin_unlock(&ilb->lock);
return sk; return sk;
} }
EXPORT_SYMBOL_GPL(__inet_lookup_listener); EXPORT_SYMBOL_GPL(__inet_lookup_listener);
@ -389,8 +360,7 @@ EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
static void __inet_hash(struct sock *sk) static void __inet_hash(struct sock *sk)
{ {
struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
struct hlist_head *list; struct inet_listen_hashbucket *ilb;
rwlock_t *lock;
if (sk->sk_state != TCP_LISTEN) { if (sk->sk_state != TCP_LISTEN) {
__inet_hash_nolisten(sk); __inet_hash_nolisten(sk);
@ -398,14 +368,12 @@ static void __inet_hash(struct sock *sk)
} }
WARN_ON(!sk_unhashed(sk)); WARN_ON(!sk_unhashed(sk));
list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
lock = &hashinfo->lhash_lock;
inet_listen_wlock(hashinfo); spin_lock(&ilb->lock);
__sk_add_node(sk, list); __sk_add_node(sk, &ilb->head);
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
write_unlock(lock); spin_unlock(&ilb->lock);
wake_up(&hashinfo->lhash_wait);
} }
void inet_hash(struct sock *sk) void inet_hash(struct sock *sk)
@ -420,29 +388,27 @@ EXPORT_SYMBOL_GPL(inet_hash);
void inet_unhash(struct sock *sk) void inet_unhash(struct sock *sk)
{ {
rwlock_t *lock;
struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
if (sk_unhashed(sk)) if (sk_unhashed(sk))
goto out; return;
if (sk->sk_state == TCP_LISTEN) { if (sk->sk_state == TCP_LISTEN) {
local_bh_disable(); struct inet_listen_hashbucket *ilb;
inet_listen_wlock(hashinfo);
lock = &hashinfo->lhash_lock; ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
spin_lock_bh(&ilb->lock);
if (__sk_del_node_init(sk)) if (__sk_del_node_init(sk))
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
spin_unlock_bh(&ilb->lock);
} else { } else {
lock = inet_ehash_lockp(hashinfo, sk->sk_hash); rwlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
write_lock_bh(lock); write_lock_bh(lock);
if (__sk_nulls_del_node_init_rcu(sk)) if (__sk_nulls_del_node_init_rcu(sk))
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
}
write_unlock_bh(lock); write_unlock_bh(lock);
out: }
if (sk->sk_state == TCP_LISTEN)
wake_up(&hashinfo->lhash_wait);
} }
EXPORT_SYMBOL_GPL(inet_unhash); EXPORT_SYMBOL_GPL(inet_unhash);
@ -556,3 +522,13 @@ int inet_hash_connect(struct inet_timewait_death_row *death_row,
} }
EXPORT_SYMBOL_GPL(inet_hash_connect); EXPORT_SYMBOL_GPL(inet_hash_connect);
void inet_hashinfo_init(struct inet_hashinfo *h)
{
int i;
for (i = 0; i < INET_LHTABLE_SIZE; i++)
spin_lock_init(&h->listening_hash[i].lock);
}
EXPORT_SYMBOL_GPL(inet_hashinfo_init);

Просмотреть файл

@ -97,11 +97,7 @@ struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
} }
#endif #endif
struct inet_hashinfo __cacheline_aligned tcp_hashinfo = { struct inet_hashinfo tcp_hashinfo;
.lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
.lhash_users = ATOMIC_INIT(0),
.lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
};
static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb) static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
{ {
@ -1874,15 +1870,18 @@ static void *listening_get_next(struct seq_file *seq, void *cur)
struct inet_connection_sock *icsk; struct inet_connection_sock *icsk;
struct hlist_node *node; struct hlist_node *node;
struct sock *sk = cur; struct sock *sk = cur;
struct inet_listen_hashbucket *ilb;
struct tcp_iter_state *st = seq->private; struct tcp_iter_state *st = seq->private;
struct net *net = seq_file_net(seq); struct net *net = seq_file_net(seq);
if (!sk) { if (!sk) {
st->bucket = 0; st->bucket = 0;
sk = sk_head(&tcp_hashinfo.listening_hash[0]); ilb = &tcp_hashinfo.listening_hash[0];
spin_lock_bh(&ilb->lock);
sk = sk_head(&ilb->head);
goto get_sk; goto get_sk;
} }
ilb = &tcp_hashinfo.listening_hash[st->bucket];
++st->num; ++st->num;
if (st->state == TCP_SEQ_STATE_OPENREQ) { if (st->state == TCP_SEQ_STATE_OPENREQ) {
@ -1932,8 +1931,11 @@ start_req:
} }
read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
} }
spin_unlock_bh(&ilb->lock);
if (++st->bucket < INET_LHTABLE_SIZE) { if (++st->bucket < INET_LHTABLE_SIZE) {
sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]); ilb = &tcp_hashinfo.listening_hash[st->bucket];
spin_lock_bh(&ilb->lock);
sk = sk_head(&ilb->head);
goto get_sk; goto get_sk;
} }
cur = NULL; cur = NULL;
@ -2066,12 +2068,10 @@ static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
void *rc; void *rc;
struct tcp_iter_state *st = seq->private; struct tcp_iter_state *st = seq->private;
inet_listen_lock(&tcp_hashinfo);
st->state = TCP_SEQ_STATE_LISTENING; st->state = TCP_SEQ_STATE_LISTENING;
rc = listening_get_idx(seq, &pos); rc = listening_get_idx(seq, &pos);
if (!rc) { if (!rc) {
inet_listen_unlock(&tcp_hashinfo);
st->state = TCP_SEQ_STATE_ESTABLISHED; st->state = TCP_SEQ_STATE_ESTABLISHED;
rc = established_get_idx(seq, pos); rc = established_get_idx(seq, pos);
} }
@ -2103,7 +2103,6 @@ static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
case TCP_SEQ_STATE_LISTENING: case TCP_SEQ_STATE_LISTENING:
rc = listening_get_next(seq, v); rc = listening_get_next(seq, v);
if (!rc) { if (!rc) {
inet_listen_unlock(&tcp_hashinfo);
st->state = TCP_SEQ_STATE_ESTABLISHED; st->state = TCP_SEQ_STATE_ESTABLISHED;
rc = established_get_first(seq); rc = established_get_first(seq);
} }
@ -2130,7 +2129,7 @@ static void tcp_seq_stop(struct seq_file *seq, void *v)
} }
case TCP_SEQ_STATE_LISTENING: case TCP_SEQ_STATE_LISTENING:
if (v != SEQ_START_TOKEN) if (v != SEQ_START_TOKEN)
inet_listen_unlock(&tcp_hashinfo); spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
break; break;
case TCP_SEQ_STATE_TIME_WAIT: case TCP_SEQ_STATE_TIME_WAIT:
case TCP_SEQ_STATE_ESTABLISHED: case TCP_SEQ_STATE_ESTABLISHED:
@ -2405,6 +2404,7 @@ static struct pernet_operations __net_initdata tcp_sk_ops = {
void __init tcp_v4_init(void) void __init tcp_v4_init(void)
{ {
inet_hashinfo_init(&tcp_hashinfo);
if (register_pernet_device(&tcp_sk_ops)) if (register_pernet_device(&tcp_sk_ops))
panic("Failed to create the TCP control socket.\n"); panic("Failed to create the TCP control socket.\n");
} }

Просмотреть файл

@ -25,30 +25,30 @@
void __inet6_hash(struct sock *sk) void __inet6_hash(struct sock *sk)
{ {
struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
rwlock_t *lock;
WARN_ON(!sk_unhashed(sk)); WARN_ON(!sk_unhashed(sk));
if (sk->sk_state == TCP_LISTEN) { if (sk->sk_state == TCP_LISTEN) {
struct hlist_head *list; struct inet_listen_hashbucket *ilb;
list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
lock = &hashinfo->lhash_lock; spin_lock(&ilb->lock);
inet_listen_wlock(hashinfo); __sk_add_node(sk, &ilb->head);
__sk_add_node(sk, list); spin_unlock(&ilb->lock);
} else { } else {
unsigned int hash; unsigned int hash;
struct hlist_nulls_head *list; struct hlist_nulls_head *list;
rwlock_t *lock;
sk->sk_hash = hash = inet6_sk_ehashfn(sk); sk->sk_hash = hash = inet6_sk_ehashfn(sk);
list = &inet_ehash_bucket(hashinfo, hash)->chain; list = &inet_ehash_bucket(hashinfo, hash)->chain;
lock = inet_ehash_lockp(hashinfo, hash); lock = inet_ehash_lockp(hashinfo, hash);
write_lock(lock); write_lock(lock);
__sk_nulls_add_node_rcu(sk, list); __sk_nulls_add_node_rcu(sk, list);
write_unlock(lock);
} }
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
write_unlock(lock);
} }
EXPORT_SYMBOL(__inet6_hash); EXPORT_SYMBOL(__inet6_hash);
@ -126,10 +126,11 @@ struct sock *inet6_lookup_listener(struct net *net,
const struct hlist_node *node; const struct hlist_node *node;
struct sock *result = NULL; struct sock *result = NULL;
int score, hiscore = 0; int score, hiscore = 0;
struct inet_listen_hashbucket *ilb;
read_lock(&hashinfo->lhash_lock); ilb = &hashinfo->listening_hash[inet_lhashfn(net, hnum)];
sk_for_each(sk, node, spin_lock(&ilb->lock);
&hashinfo->listening_hash[inet_lhashfn(net, hnum)]) { sk_for_each(sk, node, &ilb->head) {
if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum && if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum &&
sk->sk_family == PF_INET6) { sk->sk_family == PF_INET6) {
const struct ipv6_pinfo *np = inet6_sk(sk); const struct ipv6_pinfo *np = inet6_sk(sk);
@ -157,7 +158,7 @@ struct sock *inet6_lookup_listener(struct net *net,
} }
if (result) if (result)
sock_hold(result); sock_hold(result);
read_unlock(&hashinfo->lhash_lock); spin_unlock(&ilb->lock);
return result; return result;
} }