Message ID | 20120131194718.GA9834@dev3310.snc6.facebook.com |
---|---|
State | Changes Requested, archived |
Delegated to: | David Miller |
Headers | show |
On Tue, 2012-01-31 at 11:47 -0800, Arun Sharma wrote: > On Tue, Jan 31, 2012 at 10:50:57AM -0800, Joe Perches wrote: > > It might be useful to use a generic routine. [] > diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c [] > @@ -2014,11 +2014,14 @@ adjudge_to_death: [] > + too_many_orphans = tcp_too_many_orphans(sk, 0); > + out_of_socket_memory = tcp_out_of_memory(sk); > + tcp_log_oom(too_many_orphans, out_of_socket_memory); [] > diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c [] > @@ -77,10 +78,10 @@ static int tcp_out_of_resources(struct sock *sk, int do_reset) [] > + too_many_orphans = tcp_too_many_orphans(sk, shift); > + out_of_socket_memory = tcp_out_of_memory(sk); > + tcp_log_oom(too_many_orphans, out_of_socket_memory); > + if (too_many_orphans || out_of_socket_memory) { Perhaps these repeated three lines should be a routine like: bool tcp_check_oom(struct sock *sk, int shift) { bool tcp_orphans = tcp_too_many_orphans(sk, shift); bool tcp_oom = tcp_out_of_memory(sk); printks... return tcp_orphans || tcp_oom; } -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On 1/31/12 12:09 PM, Joe Perches wrote: >> + too_many_orphans = tcp_too_many_orphans(sk, shift); >> + out_of_socket_memory = tcp_out_of_memory(sk); >> + tcp_log_oom(too_many_orphans, out_of_socket_memory); >> + if (too_many_orphans || out_of_socket_memory) { > > Perhaps these repeated three lines should be a routine like: > > bool tcp_check_oom(struct sock *sk, int shift) > { > bool tcp_orphans = tcp_too_many_orphans(sk, shift); > bool tcp_oom = tcp_out_of_memory(sk); > > printks... > > return tcp_orphans || tcp_oom; > } I like your previous suggestion better. It preserves the ability to write: if (too_many_orphans) { do_something(); } if (out_of_socket_memory) { do_something_else(); } -Arun -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Tue, 2012-01-31 at 12:46 -0800, Arun Sharma wrote: > On 1/31/12 12:09 PM, Joe Perches wrote: > >> + too_many_orphans = tcp_too_many_orphans(sk, shift); > >> + out_of_socket_memory = tcp_out_of_memory(sk); > >> + tcp_log_oom(too_many_orphans, out_of_socket_memory); > >> + if (too_many_orphans || out_of_socket_memory) { > > Perhaps these repeated three lines should be a routine like: > > bool tcp_check_oom(struct sock *sk, int shift) > > { > > bool tcp_orphans = tcp_too_many_orphans(sk, shift); > > bool tcp_oom = tcp_out_of_memory(sk); > > > > printks... > > > > return tcp_orphans || tcp_oom; > > } > I like your previous suggestion better. It preserves the ability to write: > if (too_many_orphans) { > do_something(); > } > if (out_of_socket_memory) { > do_something_else(); > } shrug. That isn't currently used and tcp_too_many_orphans and tcp_out_of_memory could still be checked. I think the routine could be moved out-of-line. -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
From: Joe Perches <joe@perches.com> Date: Tue, 31 Jan 2012 12:55:41 -0800 > On Tue, 2012-01-31 at 12:46 -0800, Arun Sharma wrote: >> On 1/31/12 12:09 PM, Joe Perches wrote: >> >> + too_many_orphans = tcp_too_many_orphans(sk, shift); >> >> + out_of_socket_memory = tcp_out_of_memory(sk); >> >> + tcp_log_oom(too_many_orphans, out_of_socket_memory); >> >> + if (too_many_orphans || out_of_socket_memory) { >> > Perhaps these repeated three lines should be a routine like: >> > bool tcp_check_oom(struct sock *sk, int shift) >> > { >> > bool tcp_orphans = tcp_too_many_orphans(sk, shift); >> > bool tcp_oom = tcp_out_of_memory(sk); >> > >> > printks... >> > >> > return tcp_orphans || tcp_oom; >> > } >> I like your previous suggestion better. It preserves the ability to write: >> if (too_many_orphans) { >> do_something(); >> } >> if (out_of_socket_memory) { >> do_something_else(); >> } > > shrug. That isn't currently used and > tcp_too_many_orphans and tcp_out_of_memory > could still be checked. > > I think the routine could be moved out-of-line. Indeed, and make the out-of-line combined routine (that does the test as well as the conditional logging) return a boolean that determines if the "if (too_many_orphans || out_of_socket_memory)" test should pass. -- To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
diff --git a/include/net/tcp.h b/include/net/tcp.h index 0118ea9..0dba9d6 100644 --- a/include/net/tcp.h +++ b/include/net/tcp.h @@ -273,6 +273,14 @@ static inline int between(__u32 seq1, __u32 seq2, __u32 seq3) return seq3 - seq2 >= seq1 - seq2; } +static inline bool tcp_out_of_memory(struct sock *sk) +{ + if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF && + sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2)) + return true; + return false; +} + static inline bool tcp_too_many_orphans(struct sock *sk, int shift) { struct percpu_counter *ocp = sk->sk_prot->orphan_count; @@ -283,13 +291,17 @@ static inline bool tcp_too_many_orphans(struct sock *sk, int shift) if (orphans << shift > sysctl_tcp_max_orphans) return true; } - - if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF && - sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2)) - return true; return false; } +static inline void tcp_log_oom(bool orphans, bool socket_memory) +{ + if (orphans && net_ratelimit()) + pr_info("TCP: too many orphaned sockets\n"); + if (socket_memory && net_ratelimit()) + pr_info("TCP: out of memory -- consider tuning tcp_mem\n"); +} + /* syncookies: remember time of last synqueue overflow */ static inline void tcp_synq_overflow(struct sock *sk) { diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index 9bcdec3..9c38b3a 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -2014,11 +2014,14 @@ adjudge_to_death: } } if (sk->sk_state != TCP_CLOSE) { + bool too_many_orphans, out_of_socket_memory; + sk_mem_reclaim(sk); - if (tcp_too_many_orphans(sk, 0)) { - if (net_ratelimit()) - printk(KERN_INFO "TCP: too many of orphaned " - "sockets\n"); + too_many_orphans = tcp_too_many_orphans(sk, 0); + out_of_socket_memory = tcp_out_of_memory(sk); + tcp_log_oom(too_many_orphans, out_of_socket_memory); + + if (too_many_orphans || out_of_socket_memory) { tcp_set_state(sk, TCP_CLOSE); tcp_send_active_reset(sk, GFP_ATOMIC); NET_INC_STATS_BH(sock_net(sk), diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c index a516d1e..c47fcb6 100644 --- a/net/ipv4/tcp_timer.c +++ b/net/ipv4/tcp_timer.c @@ -67,6 +67,7 @@ static int tcp_out_of_resources(struct sock *sk, int do_reset) { struct tcp_sock *tp = tcp_sk(sk); int shift = 0; + bool too_many_orphans, out_of_socket_memory; /* If peer does not open window for long time, or did not transmit * anything for long time, penalize it. */ @@ -77,10 +78,10 @@ static int tcp_out_of_resources(struct sock *sk, int do_reset) if (sk->sk_err_soft) shift++; - if (tcp_too_many_orphans(sk, shift)) { - if (net_ratelimit()) - printk(KERN_INFO "Out of socket memory\n"); - + too_many_orphans = tcp_too_many_orphans(sk, shift); + out_of_socket_memory = tcp_out_of_memory(sk); + tcp_log_oom(too_many_orphans, out_of_socket_memory); + if (too_many_orphans || out_of_socket_memory) { /* Catch exceptional cases, when connection requires reset. * 1. Last segment was sent recently. */ if ((s32)(tcp_time_stamp - tp->lsndtime) <= TCP_TIMEWAIT_LEN ||