From nobody Mon Nov 25 07:42:23 2024 Received: from serv108.segi.ulg.ac.be (serv108.segi.ulg.ac.be [139.165.32.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9375C19005F; Mon, 28 Oct 2024 22:02:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.165.32.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152967; cv=none; b=MWTNHU3YSeKiiva0LRdVsdEyGJuFiAvrfCft7R1HQb9C1i0OGn7UsGq+tD+QZavKzRx+ZBy6VZRP1SlLjq79CJS4nZ2aEvTiqq4CCp93vZte7hG+C+/sjhUPAFSVuRxJs3FLww5yycF3JRS4mt7qj/swQCFioWqyoDU/+LWcKaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152967; c=relaxed/simple; bh=ilClyPQtRxrsdEANoquGAOae7YeLsPgwb/N5C57thMc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Sb0anUtjuggLVIwg8HDaFC2VXavFvDDoEu0Jjq6bmdAwPGY/seNKRaCZvBp1lbwg0rFfWhrfwTnZIcR05z3/i0T3UgHo8/kUc47aAJA47hwXZ8r38zVj6F7PK1IPnT1RAbafYn+2M5RQsAIM7E48gkvLFkbu7vM2104gIpQUfVk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be; spf=pass smtp.mailfrom=uliege.be; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b=pqmNwI9F; arc=none smtp.client-ip=139.165.32.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uliege.be Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b="pqmNwI9F" Received: from localhost.localdomain (modemcable174.147-130-66.mc.videotron.ca [66.130.147.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by serv108.segi.ulg.ac.be (Postfix) with ESMTPSA id 2097A20167C2; Mon, 28 Oct 2024 23:02:34 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 serv108.segi.ulg.ac.be 2097A20167C2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uliege.be; s=ulg20190529; t=1730152957; bh=HsRx5WoasynQ2MWTPxAcC1uxVEajhdd82qZcgSPFNK0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pqmNwI9FMFr1l9CoqOrHksyazxDPicnwwW3mSUsnElPz6jkUktHmdZTSdZt5ATMNF 67wYYjOv/4H40oLHzxf8Uj3YMWjrKRXeunPdjEA64nMuuxRTLOMybC6nt7DZ/GMKOV 3ongZvswpfM+YJkP/OppsAZShboTRjKmoYda5QBYq88Uqxg2xDLYvsi3WtjYYKYlda qwsXkvwxnnacjkYOFRdX+qHpzhjn5mStUn8YKbFpES3DV2QvonzTXYAca85+g55nWA vwi3SSGQuuMivRGf6dueNoINo/eMSSqHmn/HyEfwTU5doCmmN0UNL4SZGWs6gn5anL cZgcUuhwi0E7w== From: Justin Iurman To: netdev@vger.kernel.org Cc: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, linux-kernel@vger.kernel.org, justin.iurman@uliege.be Subject: [PATCH net-next v2 1/3] net: ipv6: ioam6_iptunnel: mitigate 2-realloc issue Date: Mon, 28 Oct 2024 23:02:10 +0100 Message-Id: <20241028220212.24132-2-justin.iurman@uliege.be> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241028220212.24132-1-justin.iurman@uliege.be> References: <20241028220212.24132-1-justin.iurman@uliege.be> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch mitigates the two-reallocations issue with ioam6_iptunnel by providing the dst_entry (in the cache) to the first call to skb_cow_head(). As a result, the very first iteration would still trigger two reallocations (i.e., empty cache), while next iterations would only trigger a single reallocation. Performance tests before/after applying this patch, which clearly shows the improvement: - inline mode: - before: https://ibb.co/LhQ8V63 - after: https://ibb.co/x5YT2bS - encap mode: - before: https://ibb.co/3Cjm5m0 - after: https://ibb.co/TwpsxTC - encap mode with tunsrc: - before: https://ibb.co/Gpy9QPg - after: https://ibb.co/PW1bZFT This patch also fixes an incorrect behavior: after the insertion, the second call to skb_cow_head() makes sure that the dev has enough headroom in the skb for layer 2 and stuff. In that case, the "old" dst_entry was used, which is now fixed. After discussing with Paolo, it appears that both patches can be merged into a single one -this one- (for the sake of readability) and target net-next. Signed-off-by: Justin Iurman --- net/ipv6/ioam6_iptunnel.c | 90 +++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/net/ipv6/ioam6_iptunnel.c b/net/ipv6/ioam6_iptunnel.c index beb6b4cfc551..07bfd557e08a 100644 --- a/net/ipv6/ioam6_iptunnel.c +++ b/net/ipv6/ioam6_iptunnel.c @@ -254,15 +254,24 @@ static int ioam6_do_fill(struct net *net, struct sk_b= uff *skb) return 0; } =20 +static inline int dev_overhead(struct dst_entry *dst, struct sk_buff *skb) +{ + if (likely(dst)) + return LL_RESERVED_SPACE(dst->dev); + + return skb->mac_len; +} + static int ioam6_do_inline(struct net *net, struct sk_buff *skb, - struct ioam6_lwt_encap *tuninfo) + struct ioam6_lwt_encap *tuninfo, + struct dst_entry *dst) { struct ipv6hdr *oldhdr, *hdr; int hdrlen, err; =20 hdrlen =3D (tuninfo->eh.hdrlen + 1) << 3; =20 - err =3D skb_cow_head(skb, hdrlen + skb->mac_len); + err =3D skb_cow_head(skb, hdrlen + dev_overhead(dst, skb)); if (unlikely(err)) return err; =20 @@ -293,16 +302,16 @@ static int ioam6_do_encap(struct net *net, struct sk_= buff *skb, struct ioam6_lwt_encap *tuninfo, bool has_tunsrc, struct in6_addr *tunsrc, - struct in6_addr *tundst) + struct in6_addr *tundst, + struct dst_entry *dst) { - struct dst_entry *dst =3D skb_dst(skb); struct ipv6hdr *hdr, *inner_hdr; int hdrlen, len, err; =20 hdrlen =3D (tuninfo->eh.hdrlen + 1) << 3; len =3D sizeof(*hdr) + hdrlen; =20 - err =3D skb_cow_head(skb, len + skb->mac_len); + err =3D skb_cow_head(skb, len + dev_overhead(dst, skb)); if (unlikely(err)) return err; =20 @@ -326,7 +335,7 @@ static int ioam6_do_encap(struct net *net, struct sk_bu= ff *skb, if (has_tunsrc) memcpy(&hdr->saddr, tunsrc, sizeof(*tunsrc)); else - ipv6_dev_get_saddr(net, dst->dev, &hdr->daddr, + ipv6_dev_get_saddr(net, skb_dst(skb)->dev, &hdr->daddr, IPV6_PREFER_SRC_PUBLIC, &hdr->saddr); =20 skb_postpush_rcsum(skb, hdr, len); @@ -336,7 +345,7 @@ static int ioam6_do_encap(struct net *net, struct sk_bu= ff *skb, =20 static int ioam6_output(struct net *net, struct sock *sk, struct sk_buff *= skb) { - struct dst_entry *dst =3D skb_dst(skb); + struct dst_entry *dst, *orig_dst =3D skb_dst(skb); struct in6_addr orig_daddr; struct ioam6_lwt *ilwt; int err =3D -EINVAL; @@ -345,7 +354,7 @@ static int ioam6_output(struct net *net, struct sock *s= k, struct sk_buff *skb) if (skb->protocol !=3D htons(ETH_P_IPV6)) goto drop; =20 - ilwt =3D ioam6_lwt_state(dst->lwtstate); + ilwt =3D ioam6_lwt_state(orig_dst->lwtstate); =20 /* Check for insertion frequency (i.e., "k over n" insertions) */ pkt_cnt =3D atomic_fetch_inc(&ilwt->pkt_cnt); @@ -354,6 +363,10 @@ static int ioam6_output(struct net *net, struct sock *= sk, struct sk_buff *skb) =20 orig_daddr =3D ipv6_hdr(skb)->daddr; =20 + local_bh_disable(); + dst =3D dst_cache_get(&ilwt->cache); + local_bh_enable(); + switch (ilwt->mode) { case IOAM6_IPTUNNEL_MODE_INLINE: do_inline: @@ -361,7 +374,7 @@ static int ioam6_output(struct net *net, struct sock *s= k, struct sk_buff *skb) if (ipv6_hdr(skb)->nexthdr =3D=3D NEXTHDR_HOP) goto out; =20 - err =3D ioam6_do_inline(net, skb, &ilwt->tuninfo); + err =3D ioam6_do_inline(net, skb, &ilwt->tuninfo, dst); if (unlikely(err)) goto drop; =20 @@ -371,7 +384,7 @@ static int ioam6_output(struct net *net, struct sock *s= k, struct sk_buff *skb) /* Encapsulation (ip6ip6) */ err =3D ioam6_do_encap(net, skb, &ilwt->tuninfo, ilwt->has_tunsrc, &ilwt->tunsrc, - &ilwt->tundst); + &ilwt->tundst, dst); if (unlikely(err)) goto drop; =20 @@ -389,45 +402,40 @@ static int ioam6_output(struct net *net, struct sock = *sk, struct sk_buff *skb) goto drop; } =20 - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; + if (unlikely(!dst)) { + struct ipv6hdr *hdr =3D ipv6_hdr(skb); + struct flowi6 fl6; + + memset(&fl6, 0, sizeof(fl6)); + fl6.daddr =3D hdr->daddr; + fl6.saddr =3D hdr->saddr; + fl6.flowlabel =3D ip6_flowinfo(hdr); + fl6.flowi6_mark =3D skb->mark; + fl6.flowi6_proto =3D hdr->nexthdr; + + dst =3D ip6_route_output(net, NULL, &fl6); + if (dst->error) { + err =3D dst->error; + dst_release(dst); + goto drop; + } =20 - if (!ipv6_addr_equal(&orig_daddr, &ipv6_hdr(skb)->daddr)) { local_bh_disable(); - dst =3D dst_cache_get(&ilwt->cache); + dst_cache_set_ip6(&ilwt->cache, dst, &fl6.saddr); local_bh_enable(); =20 - if (unlikely(!dst)) { - struct ipv6hdr *hdr =3D ipv6_hdr(skb); - struct flowi6 fl6; - - memset(&fl6, 0, sizeof(fl6)); - fl6.daddr =3D hdr->daddr; - fl6.saddr =3D hdr->saddr; - fl6.flowlabel =3D ip6_flowinfo(hdr); - fl6.flowi6_mark =3D skb->mark; - fl6.flowi6_proto =3D hdr->nexthdr; - - dst =3D ip6_route_output(net, NULL, &fl6); - if (dst->error) { - err =3D dst->error; - dst_release(dst); - goto drop; - } - - local_bh_disable(); - dst_cache_set_ip6(&ilwt->cache, dst, &fl6.saddr); - local_bh_enable(); - } + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; + } =20 - skb_dst_drop(skb); - skb_dst_set(skb, dst); + skb_dst_drop(skb); + skb_dst_set(skb, dst); =20 + if (!ipv6_addr_equal(&orig_daddr, &ipv6_hdr(skb)->daddr)) return dst_output(net, sk, skb); - } out: - return dst->lwtstate->orig_output(net, sk, skb); + return orig_dst->lwtstate->orig_output(net, sk, skb); drop: kfree_skb(skb); return err; --=20 2.34.1 From nobody Mon Nov 25 07:42:23 2024 Received: from serv108.segi.ulg.ac.be (serv108.segi.ulg.ac.be [139.165.32.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 739BD1F9ED6; Mon, 28 Oct 2024 22:02:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.165.32.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152969; cv=none; b=eriFwuDtmoaoCulFFHKlPt+RIXrP07Tij+BvdKLzOq0ecd4/r+TGgas5rSdqWJ/DIHHLMQSkf4UFJpJ0aleAi1VLKHtR3rg2Ypcuu2McuuCAtJ5uZF9ca1rn8xZS7YjNsPHnvblqm8wMW3vdEyRVFAXOXI0awQ/XTs4NeimQkTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152969; c=relaxed/simple; bh=n/APfUoHPDXAMzhjZMZiEDyF0sjfkmgoAdtmc3va7vQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u05jHyO9qw+CKiqsxac0L7eT7uNi9/WxQDXRkH6CrnmMvMQ0tm0tUL0RKbVlo2ZlD0KubFpb0Nvz/MS50jMwCmUnr1ODtGznz3UQydSRmP9Qv04cAffEXMiGuA9ssOO4RXVFL+RJNe4IBDtJUcFFeMzxhHRxGYQGrFQXzAeoSQg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be; spf=pass smtp.mailfrom=uliege.be; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b=a6tv2lLi; arc=none smtp.client-ip=139.165.32.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uliege.be Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b="a6tv2lLi" Received: from localhost.localdomain (modemcable174.147-130-66.mc.videotron.ca [66.130.147.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by serv108.segi.ulg.ac.be (Postfix) with ESMTPSA id 8D67120167CD; Mon, 28 Oct 2024 23:02:37 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 serv108.segi.ulg.ac.be 8D67120167CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uliege.be; s=ulg20190529; t=1730152959; bh=Qj+fSJuAxw4qvKvFSjZXyAvFMm1rUrcK2jLeD/MTC0g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a6tv2lLifFJrewM7ojjmz8g0bK9DXNPXlrMi+VRQrvBco4/OrWqLbK132USpbHBrH Iyo2u3s3+f/0AdryLAB8SlLEb73ZBD+yyphkV5JXBGKXQLHY5M4u/5+HVfq8V52jdx DJwVYwIpU4pStJQCr0tolUG86OauivmZUdWPKPjPJ9RMOVT1r3DWSdpicCjdwspZMk mRHQTcCLgbkFVD8toV6VGBgOwNcxAq7MybDYXv8wLlaJIjljDfGXt1tjNTDYRXJiXo ejzL0YCzFzBNUX/5qp9SsWrUVqax0/klyaWQ/ZgjguGVX/vAXDjuYJHs7sFkuxlDZh S97b1RjOAWkjQ== From: Justin Iurman To: netdev@vger.kernel.org Cc: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, linux-kernel@vger.kernel.org, justin.iurman@uliege.be, David Lebrun Subject: [PATCH net-next v2 2/3] net: ipv6: seg6_iptunnel: mitigate 2-realloc issue Date: Mon, 28 Oct 2024 23:02:11 +0100 Message-Id: <20241028220212.24132-3-justin.iurman@uliege.be> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241028220212.24132-1-justin.iurman@uliege.be> References: <20241028220212.24132-1-justin.iurman@uliege.be> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch mitigates the two-reallocations issue with seg6_iptunnel by providing the dst_entry (in the cache) to the first call to skb_cow_head(). As a result, the very first iteration would still trigger two reallocations (i.e., empty cache), while next iterations would only trigger a single reallocation. Performance tests before/after applying this patch, which clearly shows the improvement: - before: https://ibb.co/3Cg4sNH - after: https://ibb.co/8rQ350r Signed-off-by: Justin Iurman Cc: David Lebrun --- net/ipv6/seg6_iptunnel.c | 108 +++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/net/ipv6/seg6_iptunnel.c b/net/ipv6/seg6_iptunnel.c index 098632adc9b5..a915b6c94c72 100644 --- a/net/ipv6/seg6_iptunnel.c +++ b/net/ipv6/seg6_iptunnel.c @@ -124,11 +124,25 @@ static __be32 seg6_make_flowlabel(struct net *net, st= ruct sk_buff *skb, return flowlabel; } =20 +static inline int dev_overhead(struct dst_entry *dst, struct sk_buff *skb) +{ + if (likely(dst)) + return LL_RESERVED_SPACE(dst->dev); + + return skb->mac_len; +} + /* encapsulate an IPv6 packet within an outer IPv6 header with a given SRH= */ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *osrh, int p= roto) { - struct dst_entry *dst =3D skb_dst(skb); - struct net *net =3D dev_net(dst->dev); + return __seg6_do_srh_encap(skb, osrh, proto, NULL); +} +EXPORT_SYMBOL_GPL(seg6_do_srh_encap); + +static int __seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_sr_hdr *os= rh, + int proto, struct dst_entry *dst) +{ + struct net *net =3D dev_net(skb_dst(skb)->dev); struct ipv6hdr *hdr, *inner_hdr; struct ipv6_sr_hdr *isrh; int hdrlen, tot_len, err; @@ -137,7 +151,7 @@ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_= sr_hdr *osrh, int proto) hdrlen =3D (osrh->hdrlen + 1) << 3; tot_len =3D hdrlen + sizeof(*hdr); =20 - err =3D skb_cow_head(skb, tot_len + skb->mac_len); + err =3D skb_cow_head(skb, tot_len + dev_overhead(dst, skb)); if (unlikely(err)) return err; =20 @@ -181,7 +195,7 @@ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv6_= sr_hdr *osrh, int proto) isrh->nexthdr =3D proto; =20 hdr->daddr =3D isrh->segments[isrh->first_segment]; - set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr); + set_tun_src(net, skb_dst(skb)->dev, &hdr->daddr, &hdr->saddr); =20 #ifdef CONFIG_IPV6_SEG6_HMAC if (sr_has_hmac(isrh)) { @@ -197,15 +211,14 @@ int seg6_do_srh_encap(struct sk_buff *skb, struct ipv= 6_sr_hdr *osrh, int proto) =20 return 0; } -EXPORT_SYMBOL_GPL(seg6_do_srh_encap); =20 /* encapsulate an IPv6 packet within an outer IPv6 header with reduced SRH= */ static int seg6_do_srh_encap_red(struct sk_buff *skb, - struct ipv6_sr_hdr *osrh, int proto) + struct ipv6_sr_hdr *osrh, int proto, + struct dst_entry *dst) { __u8 first_seg =3D osrh->first_segment; - struct dst_entry *dst =3D skb_dst(skb); - struct net *net =3D dev_net(dst->dev); + struct net *net =3D dev_net(skb_dst(skb)->dev); struct ipv6hdr *hdr, *inner_hdr; int hdrlen =3D ipv6_optlen(osrh); int red_tlv_offset, tlv_offset; @@ -230,7 +243,7 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, =20 tot_len =3D red_hdrlen + sizeof(struct ipv6hdr); =20 - err =3D skb_cow_head(skb, tot_len + skb->mac_len); + err =3D skb_cow_head(skb, tot_len + dev_overhead(dst, skb)); if (unlikely(err)) return err; =20 @@ -263,7 +276,7 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, if (skip_srh) { hdr->nexthdr =3D proto; =20 - set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr); + set_tun_src(net, skb_dst(skb)->dev, &hdr->daddr, &hdr->saddr); goto out; } =20 @@ -299,7 +312,7 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, =20 srcaddr: isrh->nexthdr =3D proto; - set_tun_src(net, dst->dev, &hdr->daddr, &hdr->saddr); + set_tun_src(net, skb_dst(skb)->dev, &hdr->daddr, &hdr->saddr); =20 #ifdef CONFIG_IPV6_SEG6_HMAC if (unlikely(!skip_srh && sr_has_hmac(isrh))) { @@ -319,6 +332,13 @@ static int seg6_do_srh_encap_red(struct sk_buff *skb, =20 /* insert an SRH within an IPv6 packet, just after the IPv6 header */ int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *osrh) +{ + return __seg6_do_srh_inline(skb, osrh, NULL); +} +EXPORT_SYMBOL_GPL(seg6_do_srh_inline); + +static int __seg6_do_srh_inline(struct sk_buff *skb, struct ipv6_sr_hdr *o= srh, + struct dst_entry *dst) { struct ipv6hdr *hdr, *oldhdr; struct ipv6_sr_hdr *isrh; @@ -326,7 +346,7 @@ int seg6_do_srh_inline(struct sk_buff *skb, struct ipv6= _sr_hdr *osrh) =20 hdrlen =3D (osrh->hdrlen + 1) << 3; =20 - err =3D skb_cow_head(skb, hdrlen + skb->mac_len); + err =3D skb_cow_head(skb, hdrlen + dev_overhead(dst, skb)); if (unlikely(err)) return err; =20 @@ -369,22 +389,20 @@ int seg6_do_srh_inline(struct sk_buff *skb, struct ip= v6_sr_hdr *osrh) =20 return 0; } -EXPORT_SYMBOL_GPL(seg6_do_srh_inline); =20 -static int seg6_do_srh(struct sk_buff *skb) +static int seg6_do_srh(struct sk_buff *skb, struct dst_entry *dst) { - struct dst_entry *dst =3D skb_dst(skb); struct seg6_iptunnel_encap *tinfo; int proto, err =3D 0; =20 - tinfo =3D seg6_encap_lwtunnel(dst->lwtstate); + tinfo =3D seg6_encap_lwtunnel(skb_dst(skb)->lwtstate); =20 switch (tinfo->mode) { case SEG6_IPTUN_MODE_INLINE: if (skb->protocol !=3D htons(ETH_P_IPV6)) return -EINVAL; =20 - err =3D seg6_do_srh_inline(skb, tinfo->srh); + err =3D __seg6_do_srh_inline(skb, tinfo->srh, dst); if (err) return err; break; @@ -402,9 +420,9 @@ static int seg6_do_srh(struct sk_buff *skb) return -EINVAL; =20 if (tinfo->mode =3D=3D SEG6_IPTUN_MODE_ENCAP) - err =3D seg6_do_srh_encap(skb, tinfo->srh, proto); + err =3D __seg6_do_srh_encap(skb, tinfo->srh, proto, dst); else - err =3D seg6_do_srh_encap_red(skb, tinfo->srh, proto); + err =3D seg6_do_srh_encap_red(skb, tinfo->srh, proto, dst); =20 if (err) return err; @@ -425,11 +443,11 @@ static int seg6_do_srh(struct sk_buff *skb) skb_push(skb, skb->mac_len); =20 if (tinfo->mode =3D=3D SEG6_IPTUN_MODE_L2ENCAP) - err =3D seg6_do_srh_encap(skb, tinfo->srh, - IPPROTO_ETHERNET); + err =3D __seg6_do_srh_encap(skb, tinfo->srh, + IPPROTO_ETHERNET, dst); else err =3D seg6_do_srh_encap_red(skb, tinfo->srh, - IPPROTO_ETHERNET); + IPPROTO_ETHERNET, dst); =20 if (err) return err; @@ -453,36 +471,37 @@ static int seg6_input_finish(struct net *net, struct = sock *sk, static int seg6_input_core(struct net *net, struct sock *sk, struct sk_buff *skb) { - struct dst_entry *orig_dst =3D skb_dst(skb); - struct dst_entry *dst =3D NULL; + struct dst_entry *dst; struct seg6_lwt *slwt; int err; =20 - err =3D seg6_do_srh(skb); - if (unlikely(err)) - goto drop; - - slwt =3D seg6_lwt_lwtunnel(orig_dst->lwtstate); + slwt =3D seg6_lwt_lwtunnel(skb_dst(skb)->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&slwt->cache); + local_bh_enable(); + + err =3D seg6_do_srh(skb, dst); + if (unlikely(err)) + goto drop; =20 if (!dst) { ip6_route_input(skb); dst =3D skb_dst(skb); if (!dst->error) { + local_bh_disable(); dst_cache_set_ip6(&slwt->cache, dst, &ipv6_hdr(skb)->saddr); + local_bh_enable(); } + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } else { skb_dst_drop(skb); skb_dst_set(skb, dst); } - local_bh_enable(); - - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; =20 if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, @@ -523,21 +542,20 @@ static int seg6_input(struct sk_buff *skb) static int seg6_output_core(struct net *net, struct sock *sk, struct sk_buff *skb) { - struct dst_entry *orig_dst =3D skb_dst(skb); - struct dst_entry *dst =3D NULL; + struct dst_entry *dst; struct seg6_lwt *slwt; int err; =20 - err =3D seg6_do_srh(skb); - if (unlikely(err)) - goto drop; - - slwt =3D seg6_lwt_lwtunnel(orig_dst->lwtstate); + slwt =3D seg6_lwt_lwtunnel(skb_dst(skb)->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&slwt->cache); local_bh_enable(); =20 + err =3D seg6_do_srh(skb, dst); + if (unlikely(err)) + goto drop; + if (unlikely(!dst)) { struct ipv6hdr *hdr =3D ipv6_hdr(skb); struct flowi6 fl6; @@ -559,15 +577,15 @@ static int seg6_output_core(struct net *net, struct s= ock *sk, local_bh_disable(); dst_cache_set_ip6(&slwt->cache, dst, &fl6.saddr); local_bh_enable(); + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } =20 skb_dst_drop(skb); skb_dst_set(skb, dst); =20 - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; - if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled)) return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb, NULL, skb_dst(skb)->dev, dst_output); --=20 2.34.1 From nobody Mon Nov 25 07:42:23 2024 Received: from serv108.segi.ulg.ac.be (serv108.segi.ulg.ac.be [139.165.32.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E1AB1E0DA7; Mon, 28 Oct 2024 22:02:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=139.165.32.111 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152966; cv=none; b=mtjXvAwhLqDSDQu+JaMYC3lPhRTWI6x2yiZrewSdoMNdRPtAc/8qjelf/3WUZxefCH/Ru5eMuG+WNrgxa2RtLlB8Mqrco6pxtuXlM/OOGys1BXd4E4aNahdG+mh5ZJJ3RahM8fP8Wf0wf6I27zXN6KDCez4VGF9i8HetR5bYmh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730152966; c=relaxed/simple; bh=fcb6/ZXqWJIFRDmjDhyI8ULDpI0uIWrmsfpnD3tx5PU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PrIyrmxggPPjTatwd+MgDI77IQWrzpq/9e4Tq4QUQ10Wo/snkjn/Is90i077DvsbWd70NnuoEgq1uhzG4jF39zNfjvGICw4dAcFCPnajXU+/0TiQDJecNeNSUPRkElm4j7iOsfCFyLyBV0h5EcL0KdtahxJ86CTzFywbOtodB8Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be; spf=pass smtp.mailfrom=uliege.be; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b=woZRg5x7; arc=none smtp.client-ip=139.165.32.111 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=uliege.be Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=uliege.be Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=uliege.be header.i=@uliege.be header.b="woZRg5x7" Received: from localhost.localdomain (modemcable174.147-130-66.mc.videotron.ca [66.130.147.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by serv108.segi.ulg.ac.be (Postfix) with ESMTPSA id CDE7D20167C3; Mon, 28 Oct 2024 23:02:39 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 serv108.segi.ulg.ac.be CDE7D20167C3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uliege.be; s=ulg20190529; t=1730152961; bh=tYfpbkPNAjaZpZFMkRWmpEDluZdW9RukyEp0aeELqDs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=woZRg5x7nZwMXHRrx0h8EzaobhN3BZNB9x3+KRi9HF7MdBllXDf8JSKPVXCtOIHqu TMLY6jnoQk8g+4qiZsAZjxUVbHg84g3jWRuT7M5sU/OGMV+TmYWn1rkGwlcBBmg4ft b58x6f5X8vh+N4Afojs2ZdVaGDxiaXAUTMPo2LUss5/jyK/IC5tqenTVJC7DRzhSCQ VTXnCDlKl45XGSPkY0kiGwxiH/JiY5EpLuXWjmtC3635JlVLF4J61a7KsOWjt3Fc1i 0O8JKOn7bw6GLOPMhKmVhVFmdowQgX1yJAPO9NC1O7Ki3UIU5m8HCedkTyfPNCChZp /2pYbk3b9VKTA== From: Justin Iurman To: netdev@vger.kernel.org Cc: davem@davemloft.net, dsahern@kernel.org, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, linux-kernel@vger.kernel.org, justin.iurman@uliege.be, Alexander Aring Subject: [PATCH net-next v2 3/3] net: ipv6: rpl_iptunnel: mitigate 2-realloc issue Date: Mon, 28 Oct 2024 23:02:12 +0100 Message-Id: <20241028220212.24132-4-justin.iurman@uliege.be> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241028220212.24132-1-justin.iurman@uliege.be> References: <20241028220212.24132-1-justin.iurman@uliege.be> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch mitigates the two-reallocations issue with rpl_iptunnel by providing the dst_entry (in the cache) to the first call to skb_cow_head(). As a result, the very first iteration would still trigger two reallocations (i.e., empty cache), while next iterations would only trigger a single reallocation. Performance tests before/after applying this patch, which clearly shows there is no impact (it even shows improvement): - before: https://ibb.co/nQJhqwc - after: https://ibb.co/4ZvW6wV Signed-off-by: Justin Iurman Cc: Alexander Aring --- net/ipv6/rpl_iptunnel.c | 67 +++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/net/ipv6/rpl_iptunnel.c b/net/ipv6/rpl_iptunnel.c index db3c19a42e1c..c518728460a2 100644 --- a/net/ipv6/rpl_iptunnel.c +++ b/net/ipv6/rpl_iptunnel.c @@ -124,8 +124,17 @@ static void rpl_destroy_state(struct lwtunnel_state *l= wt) dst_cache_destroy(&rpl_lwt_lwtunnel(lwt)->cache); } =20 +static inline int dev_overhead(struct dst_entry *dst, struct sk_buff *skb) +{ + if (likely(dst)) + return LL_RESERVED_SPACE(dst->dev); + + return skb->mac_len; +} + static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rl= wt, - const struct ipv6_rpl_sr_hdr *srh) + const struct ipv6_rpl_sr_hdr *srh, + struct dst_entry *dst) { struct ipv6_rpl_sr_hdr *isrh, *csrh; const struct ipv6hdr *oldhdr; @@ -153,7 +162,7 @@ static int rpl_do_srh_inline(struct sk_buff *skb, const= struct rpl_lwt *rlwt, =20 hdrlen =3D ((csrh->hdrlen + 1) << 3); =20 - err =3D skb_cow_head(skb, hdrlen + skb->mac_len); + err =3D skb_cow_head(skb, hdrlen + dev_overhead(dst, skb)); if (unlikely(err)) { kfree(buf); return err; @@ -186,36 +195,35 @@ static int rpl_do_srh_inline(struct sk_buff *skb, con= st struct rpl_lwt *rlwt, return 0; } =20 -static int rpl_do_srh(struct sk_buff *skb, const struct rpl_lwt *rlwt) +static int rpl_do_srh(struct sk_buff *skb, const struct rpl_lwt *rlwt, + struct dst_entry *dst) { - struct dst_entry *dst =3D skb_dst(skb); struct rpl_iptunnel_encap *tinfo; =20 if (skb->protocol !=3D htons(ETH_P_IPV6)) return -EINVAL; =20 - tinfo =3D rpl_encap_lwtunnel(dst->lwtstate); + tinfo =3D rpl_encap_lwtunnel(skb_dst(skb)->lwtstate); =20 - return rpl_do_srh_inline(skb, rlwt, tinfo->srh); + return rpl_do_srh_inline(skb, rlwt, tinfo->srh, dst); } =20 static int rpl_output(struct net *net, struct sock *sk, struct sk_buff *sk= b) { - struct dst_entry *orig_dst =3D skb_dst(skb); - struct dst_entry *dst =3D NULL; + struct dst_entry *dst; struct rpl_lwt *rlwt; int err; =20 - rlwt =3D rpl_lwt_lwtunnel(orig_dst->lwtstate); - - err =3D rpl_do_srh(skb, rlwt); - if (unlikely(err)) - goto drop; + rlwt =3D rpl_lwt_lwtunnel(skb_dst(skb)->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&rlwt->cache); local_bh_enable(); =20 + err =3D rpl_do_srh(skb, rlwt, dst); + if (unlikely(err)) + goto drop; + if (unlikely(!dst)) { struct ipv6hdr *hdr =3D ipv6_hdr(skb); struct flowi6 fl6; @@ -237,15 +245,15 @@ static int rpl_output(struct net *net, struct sock *s= k, struct sk_buff *skb) local_bh_disable(); dst_cache_set_ip6(&rlwt->cache, dst, &fl6.saddr); local_bh_enable(); + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } =20 skb_dst_drop(skb); skb_dst_set(skb, dst); =20 - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; - return dst_output(net, sk, skb); =20 drop: @@ -255,36 +263,37 @@ static int rpl_output(struct net *net, struct sock *s= k, struct sk_buff *skb) =20 static int rpl_input(struct sk_buff *skb) { - struct dst_entry *orig_dst =3D skb_dst(skb); - struct dst_entry *dst =3D NULL; + struct dst_entry *dst; struct rpl_lwt *rlwt; int err; =20 - rlwt =3D rpl_lwt_lwtunnel(orig_dst->lwtstate); - - err =3D rpl_do_srh(skb, rlwt); - if (unlikely(err)) - goto drop; + rlwt =3D rpl_lwt_lwtunnel(skb_dst(skb)->lwtstate); =20 local_bh_disable(); dst =3D dst_cache_get(&rlwt->cache); + local_bh_enable(); + + err =3D rpl_do_srh(skb, rlwt, dst); + if (unlikely(err)) + goto drop; =20 if (!dst) { ip6_route_input(skb); dst =3D skb_dst(skb); if (!dst->error) { + local_bh_disable(); dst_cache_set_ip6(&rlwt->cache, dst, &ipv6_hdr(skb)->saddr); + local_bh_enable(); } + + err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); + if (unlikely(err)) + goto drop; } else { skb_dst_drop(skb); skb_dst_set(skb, dst); } - local_bh_enable(); - - err =3D skb_cow_head(skb, LL_RESERVED_SPACE(dst->dev)); - if (unlikely(err)) - goto drop; =20 return dst_input(skb); =20 --=20 2.34.1