Skip to content

Commit dd24c00

Browse files
Eric Dumazetdavem330
authored andcommitted
net: Use a percpu_counter for orphan_count
Instead of using one atomic_t per protocol, use a percpu_counter for "orphan_count", to reduce cache line contention on heavy duty network servers. Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1748376 commit dd24c00

File tree

8 files changed

+24
-18
lines changed

8 files changed

+24
-18
lines changed

include/net/sock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ struct proto {
666666
unsigned int obj_size;
667667
int slab_flags;
668668

669-
atomic_t *orphan_count;
669+
struct percpu_counter *orphan_count;
670670

671671
struct request_sock_ops *rsk_prot;
672672
struct timewait_sock_ops *twsk_prot;

include/net/tcp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
extern struct inet_hashinfo tcp_hashinfo;
4848

49-
extern atomic_t tcp_orphan_count;
49+
extern struct percpu_counter tcp_orphan_count;
5050
extern void tcp_time_wait(struct sock *sk, int state, int timeo);
5151

5252
#define MAX_TCP_HEADER (128 + MAX_HEADER)

net/dccp/dccp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extern int dccp_debug;
4949

5050
extern struct inet_hashinfo dccp_hashinfo;
5151

52-
extern atomic_t dccp_orphan_count;
52+
extern struct percpu_counter dccp_orphan_count;
5353

5454
extern void dccp_time_wait(struct sock *sk, int state, int timeo);
5555

net/dccp/proto.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ DEFINE_SNMP_STAT(struct dccp_mib, dccp_statistics) __read_mostly;
4040

4141
EXPORT_SYMBOL_GPL(dccp_statistics);
4242

43-
atomic_t dccp_orphan_count = ATOMIC_INIT(0);
44-
43+
struct percpu_counter dccp_orphan_count;
4544
EXPORT_SYMBOL_GPL(dccp_orphan_count);
4645

4746
struct inet_hashinfo dccp_hashinfo;
@@ -1000,7 +999,7 @@ void dccp_close(struct sock *sk, long timeout)
1000999
state = sk->sk_state;
10011000
sock_hold(sk);
10021001
sock_orphan(sk);
1003-
atomic_inc(sk->sk_prot->orphan_count);
1002+
percpu_counter_inc(sk->sk_prot->orphan_count);
10041003

10051004
/*
10061005
* It is the last release_sock in its life. It will remove backlog.
@@ -1064,18 +1063,21 @@ static int __init dccp_init(void)
10641063
{
10651064
unsigned long goal;
10661065
int ehash_order, bhash_order, i;
1067-
int rc = -ENOBUFS;
1066+
int rc;
10681067

10691068
BUILD_BUG_ON(sizeof(struct dccp_skb_cb) >
10701069
FIELD_SIZEOF(struct sk_buff, cb));
1071-
1070+
rc = percpu_counter_init(&dccp_orphan_count, 0);
1071+
if (rc)
1072+
goto out;
1073+
rc = -ENOBUFS;
10721074
inet_hashinfo_init(&dccp_hashinfo);
10731075
dccp_hashinfo.bind_bucket_cachep =
10741076
kmem_cache_create("dccp_bind_bucket",
10751077
sizeof(struct inet_bind_bucket), 0,
10761078
SLAB_HWCACHE_ALIGN, NULL);
10771079
if (!dccp_hashinfo.bind_bucket_cachep)
1078-
goto out;
1080+
goto out_free_percpu;
10791081

10801082
/*
10811083
* Size and allocate the main established and bind bucket
@@ -1168,6 +1170,8 @@ static int __init dccp_init(void)
11681170
out_free_bind_bucket_cachep:
11691171
kmem_cache_destroy(dccp_hashinfo.bind_bucket_cachep);
11701172
dccp_hashinfo.bind_bucket_cachep = NULL;
1173+
out_free_percpu:
1174+
percpu_counter_destroy(&dccp_orphan_count);
11711175
goto out;
11721176
}
11731177

net/ipv4/inet_connection_sock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ void inet_csk_destroy_sock(struct sock *sk)
561561

562562
sk_refcnt_debug_release(sk);
563563

564-
atomic_dec(sk->sk_prot->orphan_count);
564+
percpu_counter_dec(sk->sk_prot->orphan_count);
565565
sock_put(sk);
566566
}
567567

@@ -641,7 +641,7 @@ void inet_csk_listen_stop(struct sock *sk)
641641

642642
sock_orphan(child);
643643

644-
atomic_inc(sk->sk_prot->orphan_count);
644+
percpu_counter_inc(sk->sk_prot->orphan_count);
645645

646646
inet_csk_destroy_sock(child);
647647

net/ipv4/proc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static int sockstat_seq_show(struct seq_file *seq, void *v)
5454
socket_seq_show(seq);
5555
seq_printf(seq, "TCP: inuse %d orphan %d tw %d alloc %d mem %d\n",
5656
sock_prot_inuse_get(net, &tcp_prot),
57-
atomic_read(&tcp_orphan_count),
57+
(int)percpu_counter_sum_positive(&tcp_orphan_count),
5858
tcp_death_row.tw_count,
5959
(int)percpu_counter_sum_positive(&tcp_sockets_allocated),
6060
atomic_read(&tcp_memory_allocated));

net/ipv4/tcp.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@
277277

278278
int sysctl_tcp_fin_timeout __read_mostly = TCP_FIN_TIMEOUT;
279279

280-
atomic_t tcp_orphan_count = ATOMIC_INIT(0);
281-
280+
struct percpu_counter tcp_orphan_count;
282281
EXPORT_SYMBOL_GPL(tcp_orphan_count);
283282

284283
int sysctl_tcp_mem[3] __read_mostly;
@@ -1837,7 +1836,7 @@ void tcp_close(struct sock *sk, long timeout)
18371836
state = sk->sk_state;
18381837
sock_hold(sk);
18391838
sock_orphan(sk);
1840-
atomic_inc(sk->sk_prot->orphan_count);
1839+
percpu_counter_inc(sk->sk_prot->orphan_count);
18411840

18421841
/* It is the last release_sock in its life. It will remove backlog. */
18431842
release_sock(sk);
@@ -1888,9 +1887,11 @@ void tcp_close(struct sock *sk, long timeout)
18881887
}
18891888
}
18901889
if (sk->sk_state != TCP_CLOSE) {
1890+
int orphan_count = percpu_counter_read_positive(
1891+
sk->sk_prot->orphan_count);
1892+
18911893
sk_mem_reclaim(sk);
1892-
if (tcp_too_many_orphans(sk,
1893-
atomic_read(sk->sk_prot->orphan_count))) {
1894+
if (tcp_too_many_orphans(sk, orphan_count)) {
18941895
if (net_ratelimit())
18951896
printk(KERN_INFO "TCP: too many of orphaned "
18961897
"sockets\n");
@@ -2689,6 +2690,7 @@ void __init tcp_init(void)
26892690
BUILD_BUG_ON(sizeof(struct tcp_skb_cb) > sizeof(skb->cb));
26902691

26912692
percpu_counter_init(&tcp_sockets_allocated, 0);
2693+
percpu_counter_init(&tcp_orphan_count, 0);
26922694
tcp_hashinfo.bind_bucket_cachep =
26932695
kmem_cache_create("tcp_bind_bucket",
26942696
sizeof(struct inet_bind_bucket), 0,

net/ipv4/tcp_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void tcp_write_err(struct sock *sk)
6565
static int tcp_out_of_resources(struct sock *sk, int do_reset)
6666
{
6767
struct tcp_sock *tp = tcp_sk(sk);
68-
int orphans = atomic_read(&tcp_orphan_count);
68+
int orphans = percpu_counter_read_positive(&tcp_orphan_count);
6969

7070
/* If peer does not open window for long time, or did not transmit
7171
* anything for long time, penalize it. */

0 commit comments

Comments
 (0)