net: ip: avoid OOM kills with large UDP sends over loopback
Subsystems (3)
Jakub Kicinski
maintainerPATCH v3 1/1 net: ip: avoid OOM kills with large UDP sends over loopback
Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer.
This is entirely avoidable, we can use an skb with page frags.
af_unix solves a similar problem by limiting the head length to SKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back.
--- net/ipv4/ip_output.c | 4 +++- net/ipv6/ip6_output.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
net/ipv4/ip_output.c +3 -1
@@ -1077,7 +1077,9 @@ static int __ip_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) +else if (!paged && + (fraglen + hh_len +15 <SKB_MAX_ALLOC || + !(rt->dst.dev->features &NETIF_F_SG ))) alloclen = fraglen;else { alloclen =min_t (int, fraglen,MAX_HEADER );
net/ipv6/ip6_output.c +3 -1
@@ -1585,7 +1585,9 @@ static int __ip6_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) +else if (!paged && + (fraglen + hh_len <SKB_MAX_ALLOC || + !(rt->dst.dev->features &NETIF_F_SG ))) alloclen = fraglen;else { alloclen =min_t (int, fraglen,MAX_HEADER );
Signature · 1 line
2.31.1
Quoted 18 lines · Jakub Kicinski
Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer.
This is entirely avoidable, we can use an skb with page frags.
af_unixsolves a similar problem by limiting the head length toSKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back.
Quoted 3 lines
Reported-by Dave Jones <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>---
Thanks !
Quoted 21 lines · Eric Dumazet
Quoted 18 lines · Jakub Kicinski
Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer.
This is entirely avoidable, we can use an skb with page frags.
af_unixsolves a similar problem by limiting the head length toSKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back.
Quoted 8 lines
Quoted 3 lines
Reported-by Dave Jones <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>---
Reviewed-by Eric Dumazet <[email protected]>Thanks !
I am taking this back.
IPv6 side also needs to account for sizeof(struct frag_hdr) ?
Quoted 30 lines · On Wed, 23 Jun 2021 09:23:28 -0700 Jakub Kicinski <[email protected]> wrote:
Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer.
This is entirely avoidable, we can use an skb with page frags.
af_unixsolves a similar problem by limiting the head length toSKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back.Reported-by Dave Jones <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>--- net/ipv4/ip_output.c | 4 +++- net/ipv6/ip6_output.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)net/ipv4/ip_output.c +0
-0
net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 1081
Quoted 6 lines
net/ipv4/ip_output.c +2
-1@@ -1077,7 +1077,9 @@ static int __ip_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len + 15 < SKB_MAX_ALLOC ||
What does the number 15 represent here?
Quoted 9 lines
net/ipv4/ip_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );net/ipv6/ip6_output.c +0
-0
net/ipv6/ip6_output.c static int __ip6_append_data(struct sock *sk, · line 1589
Quoted 6 lines
net/ipv6/ip6_output.c +2
-1@@ -1585,7 +1585,9 @@ static int __ip6_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len < SKB_MAX_ALLOC ||
The number 15 is not use here.
Quoted 4 lines
net/ipv6/ip6_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );
Signature · 4 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Principal Kernel Engineer at Red Hat LinkedIn: http://www.linkedin.com/in/brouer
Jakub Kicinski
maintainerQuoted 5 lines · On Wed, 23 Jun 2021 21:45:55 +0200 Jesper Dangaard Brouer wrote:
Quoted 5 lines
net/ipv4/ip_output.c +0
-0@@ -1077,7 +1077,9 @@ static int __ip_append_data(struct sock *sk,
Quoted 8 lines
net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 1081
Quoted 6 lines
net/ipv4/ip_output.c +2
-1if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len + 15 < SKB_MAX_ALLOC ||What does the number 15 represent here?
No idea, it's there on the allocation line, so I need to include it on the size check.
Looking at super old code (2.4.x) it looks like it may have gotten copy & pasted mistakenly? The hard headers are rounded up to 16B, and there is code which does things like:
skb_alloc(size + dev->hard_header_len + 15); skb_reserve(skb, (dev->hard_header_len + 15) & ~15);
in other spots. So if I was to guess I'd say someone decided to add the 15B "to be safe" even though hh_len already includes the round up here.
But that's just my guess. I can't get this simple patch right, so take that with a grain of salt :/
Quoted 22 lines
Quoted 9 lines
net/ipv4/ip_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );net/ipv6/ip6_output.c +0
-0net/ipv6/ip6_output.c static int __ip6_append_data(struct sock *sk, · line 1589
Quoted 6 lines
net/ipv6/ip6_output.c +2
-1@@ -1585,7 +1585,9 @@ static int __ip6_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len < SKB_MAX_ALLOC ||The number 15 is not use here.
Quoted 4 lines
net/ipv6/ip6_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );
Quoted 28 lines · Jesper Dangaard Brouer
Quoted 25 lines · On Wed, 23 Jun 2021 09:23:28 -0700 Jakub Kicinski <[email protected]> wrote:
Dave observed number of machines hitting OOM on the UDP send path. The workload seems to be sending large UDP packets over loopback. Since loopback has MTU of 64k kernel will try to allocate an skb with up to 64k of head space. This has a good chance of failing under memory pressure. What's worse if the message length is <32k the allocation may trigger an OOM killer.
This is entirely avoidable, we can use an skb with page frags.
af_unixsolves a similar problem by limiting the head length toSKB_MAX_ALLOC. This seems like a good and simple approach. It means that UDP messages > 16kB will now use fragments if underlying device supports SG, if extra allocator pressure causes regressions in real workloads we can switch to trying the large allocation first and falling back.Reported-by Dave Jones <[email protected]>Signed-off-by Jakub Kicinski <[email protected]>--- net/ipv4/ip_output.c | 4 +++- net/ipv6/ip6_output.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
Quoted 13 lines
net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 1081
Quoted 11 lines
net/ipv4/ip_output.c +2
-1@@ -1077,7 +1077,9 @@ static int __ip_append_data(struct sock *sk,if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len + 15 < SKB_MAX_ALLOC ||What does the number 15 represent here?
Just look at the existing code, few lines below
skb = alloc_skb(alloclen + hh_len + 15, ...
Quoted 10 lines
Quoted 10 lines
net/ipv4/ip_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );net/ipv6/ip6_output.c +0
-0@@ -1585,7 +1585,9 @@ static int __ip6_append_data(struct sock *sk,
Quoted 8 lines
net/ipv6/ip6_output.c static int __ip6_append_data(struct sock *sk, · line 1589
Quoted 6 lines
net/ipv6/ip6_output.c +2
-1if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len < SKB_MAX_ALLOC ||The number 15 is not use here.
Because the alloc_skb() done later does not use + 15
Quoted 8 lines
Quoted 5 lines
net/ipv6/ip6_output.c +1
-0+ !(rt->dst.dev->features & NETIF_F_SG))) alloclen = fraglen; else { alloclen =min_t (int, fraglen,MAX_HEADER );
Quoted 23 lines · Jakub Kicinski
Quoted 5 lines · On Wed, 23 Jun 2021 21:45:55 +0200 Jesper Dangaard Brouer wrote:
Quoted 5 lines
net/ipv4/ip_output.c +0
-0@@ -1077,7 +1077,9 @@ static int __ip_append_data(struct sock *sk,Quoted 8 lines
net/ipv4/ip_output.c static int __ip_append_data(struct sock *sk, · line 1081
Quoted 6 lines
net/ipv4/ip_output.c +2
-1if ((flags &MSG_MORE ) && !(rt->dst.dev->features&NETIF_F_SG )) alloclen = mtu; -else if (!paged) + else if (!paged && + (fraglen + hh_len + 15 < SKB_MAX_ALLOC ||What does the number 15 represent here?
No idea, it's there on the allocation line, so I need to include it on the size check.
Looking at super old code (2.4.x) it looks like it may have gotten copy & pasted mistakenly? The hard headers are rounded up to 16B, and there is code which does things like:
Quoted 5 lines
skb_alloc(size + dev->hard_header_len + 15); skb_reserve(skb, (dev->hard_header_len + 15) & ~15);in other spots. So if I was to guess I'd say someone decided to add the 15B "to be safe" even though
hh_lenalready includes the round up here.
The 15 seems to come from alignment indeed. Not sure when it was introduced, but until 56951b54e87a there is also this
/*
* Get the memory we require with some space left
for alignment.
*/
skb = sock_alloc_send_skb(sk, fraglen+hh_len+15, 0,flags&MSG_DONTWAIT, &err);