Subject: [PATCH RFC net-next 03/11] udp: better wmem accounting on gso

From: Willem de Bruijn <[email-masked]>
Date: Tue, 17 Apr 2018 16:00:53 -0400

From: Willem de Bruijn <[email-masked]>

skb_segment by default transfers allocated wmem from the gso skb
to the tail of the segment list. This underreports real truesize
of the list, especially if the tail might be dropped.

Similar to tcp_gso_segment, update wmem_alloc with the aggregate
list truesize and make each segment responsible for its own
share by setting skb->destructor.

Clear gso_skb->destructor prior to calling skb_segment to skip
the default assignment to tail.

Signed-off-by: Willem de Bruijn <[email-masked]>
---
 net/ipv4/udp_offload.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/udp_offload.c b/net/ipv4/udp_offload.c
index 3378f8dec9c7..a69117e25e78 100644
--- a/net/ipv4/udp_offload.c
+++ b/net/ipv4/udp_offload.c
@@ -192,7 +192,9 @@ struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
 				  unsigned int mss, __sum16 check)
 {
 	struct udphdr *uh = udp_hdr(gso_skb);
-	struct sk_buff *segs;
+	struct sock *sk = gso_skb->sk;
+	struct sk_buff *segs, *seg;
+	unsigned int sum_truesize = 0;
 	unsigned int hdrlen;
 
 	if (gso_skb->len <= sizeof(*uh) + mss)
@@ -203,9 +205,23 @@ struct sk_buff *__udp_gso_segment(struct sk_buff *gso_skb,
 	skb_pull(gso_skb, sizeof(*uh));
 	hdrlen = gso_skb->data - skb_mac_header(gso_skb);
 
+	/* clear destructor to avoid skb_segment assigning it to tail */
+	WARN_ON_ONCE(gso_skb->destructor != sock_wfree);
+	gso_skb->destructor = NULL;
+
 	segs = skb_segment(gso_skb, features);
-	if (unlikely(IS_ERR_OR_NULL(segs)))
+	if (unlikely(IS_ERR_OR_NULL(segs))) {
+		gso_skb->destructor = sock_wfree;
 		return segs;
+	}
+
+	for (seg = segs; seg; seg = seg->next) {
+		seg->destructor = sock_wfree;
+		seg->sk = sk;
+		sum_truesize += seg->truesize;
+	}
+
+	refcount_add(sum_truesize - gso_skb->truesize, &sk->sk_wmem_alloc);
 
 	/* If last packet is not full, fix up its header */
 	if (segs->prev->len != hdrlen + mss) {
-- 
2.17.0.484.g0c8726318c-goog