From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2AB81E2852 for ; Tue, 11 Mar 2025 02:51:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741661479; cv=none; b=sR7OLxeKKAYv65FpDuGmmB7RG5WqISLLddyE098XckItFb+196RnUOhsdUzrjQIQZ1cF5IxNRwQcO2TyRfpZCqv47I9/TzOI3lrTTaK3Am3DtMHT24W4kXu1whUoBHV/CTbReIJH+VPDrSYg91Y2iHlCUkzcUxjzQgt93FcHtrw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741661479; c=relaxed/simple; bh=HuChv44+qS7p5GEMDRAJkr89Crrk0/yUnOuJ4wNsB94=; h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject: Mime-Version:Content-Type; b=t/Fun1ZVO0ZsEpW/sr6ruKAdd8zDei0Y7m4CorPF5k+AhsIjAajuPV1id9mImyOT+ywQvSGHFsXo7U4bLqZSi2gC93CUK2wkG3F368HmZ3UqwfyFU6EEMxJEOzjqMf4k53XAKzVilObeJScVwIEjljAmF7Uea6r3oJqUxQ+EI+0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PwOPzOni; arc=none smtp.client-ip=209.85.160.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PwOPzOni" Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-475a77bbd8cso48284521cf.3 for ; Mon, 10 Mar 2025 19:51:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741661476; x=1742266276; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=QWI+6olb4mBRRsRuCZ3YDJ0QJoIiUYcy9vvrFY/gGuE=; b=PwOPzOni4I5MU1Gs2PZ83HiPE9x5GNs3zog96Sqe15BzDf8CDVltBHpPxEB5eMoAu0 ICIp9ZLMIdHVq5AHk1nN540FxSwRddxD+PevV4cWTDCmeOxdhP2CsJo8IVIz76alu5W7 CvMragR1W6tD2BDFWUNJIf4tqmoLmZHOpH36OMJusvgCNwmJtMZCKToRDN1JBj85/+Yb afq6gzUt/qbH4mTmdOuaguI6oLnmWd1EYe+NONvQas+vxRi8kvGKoA6uxPgqAoyyxqhE Aji6IMxm4hcKVAct47HY/OlprMxAlRYzVIjJ11meVAGoswlQHDrUgv8ejWxlwuAYHZWv bGHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741661476; x=1742266276; h=content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=QWI+6olb4mBRRsRuCZ3YDJ0QJoIiUYcy9vvrFY/gGuE=; b=RAdX024UZzzFKuw7p1fPBuUeSgxH3BbxTSJD3gn7oNOe+hfp1qto1st5p660tCTe+w yq3qzDp+PI8bLGJFeyryYabO954gM9rDveNqFqtpSjuhQyJCHPnwSW3MwiD+66dX7o6v +e+RvmlXui/MMIAFPHPYNnCdOdRHyRyXsRZDvjLsWpQ+Mx7zpGTZk5JfBU0BL72fMmax I0BMmxyWD7qi1loGEnDctnArSBK5RNEsJjeYhYdwk/GJW/QQl4H5XVLLTRIdpTP9Glfa ejffAYK7cJQOEDdsE1CyCtq5XH7gxopwc5Nws1r97SwV+HexCZvtoljpWe/Z5Qo+5y0v uArA== X-Forwarded-Encrypted: i=1; AJvYcCWUPitMpPapNM7G0srrXmMROCKInFglU4CKFetSy8Mpi5Jeyw69XNZVj983Vm188GWmWB/kG2A=@vger.kernel.org X-Gm-Message-State: AOJu0YwNmfjSxs1AiC0uck5cLMh9OuERqNGlBwaqtAHZkjJ5PPm8wAEi vDk5GUS7C3o16Xdrtv7e/PHYgkv+JRd5jEJlkcnaIm1Wo+eF59gifbm0Eg== X-Gm-Gg: ASbGncswmTrpg23kqsg3aUw1qt9sj0DjxqGYB5xzJKZKfMzQr3OlQYyBTKyP1yWwP2X U7xGfyU4j8cDW+glmUpnpFBDYT1kKTMBLVeAth3sw1QU55kpLl3JlmNQI0iRvL2Id7RIeO/2+Ck IfWgFpvj4jvD9S5gd8P5f1j8VhcV8CqekM6HpG0AX02kmc0ego005bBQWalSULze8ImeKZdE/eq YE5shMpkBtshPS0g6/nKoRrAwD9QO8gpm4PKKtdDjaX+r/3n8pur4ml0HVCIfA3dIzQ6ZLHm0aR PArrZfFzI+BhPp5+tqEZCLwUYFCtPd2YKcbMzYVmAfzjsgsNgYyS2dOXj0M4lmsaO+21mCMNE1k CiUxvhWV4HAA9MeV8+wBrLw== X-Google-Smtp-Source: AGHT+IGBg+z0HkESzOCbLYzuhZ00ClouhB+WhQhxdLU4t9oCNGUe/0AKhQ/vyshZRB1n6CnDoiVCSQ== X-Received: by 2002:a05:622a:7ace:b0:471:fa92:9237 with SMTP id d75a77b69052e-47618adda5emr162483751cf.37.1741661476502; Mon, 10 Mar 2025 19:51:16 -0700 (PDT) Received: from localhost (234.207.85.34.bc.googleusercontent.com. [34.85.207.234]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-47663e9eb4esm40834691cf.76.2025.03.10.19.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 19:51:15 -0700 (PDT) Date: Mon, 10 Mar 2025 22:51:15 -0400 From: Willem de Bruijn To: Paolo Abeni , netdev@vger.kernel.org Cc: Willem de Bruijn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Simon Horman , David Ahern , kuniyu@amazon.com Message-ID: <67cfa5236c212_28a0b329453@willemb.c.googlers.com.notmuch> In-Reply-To: References: Subject: Re: [PATCH v3 net-next 2/2] udp_tunnel: use static call for GRO hooks when possible Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Paolo Abeni wrote: > It's quite common to have a single UDP tunnel type active in the > whole system. In such a case we can replace the indirect call for > the UDP tunnel GRO callback with a static call. > > Add the related accounting in the control path and switch to static > call when possible. To keep the code simple use a static array for > the registered tunnel types, and size such array based on the kernel > config. > > Signed-off-by: Paolo Abeni > --- > v2 -> v3: > - avoid unneeded checks in udp_tunnel_update_gro_rcv() > > v1 -> v2: > - fix UDP_TUNNEL=n build > --- > include/net/udp_tunnel.h | 4 ++ > net/ipv4/udp_offload.c | 137 ++++++++++++++++++++++++++++++++++++- > net/ipv4/udp_tunnel_core.c | 2 + > 3 files changed, 142 insertions(+), 1 deletion(-) > > diff --git a/include/net/udp_tunnel.h b/include/net/udp_tunnel.h > index eda0f3e2f65fa..a7b230867eb14 100644 > --- a/include/net/udp_tunnel.h > +++ b/include/net/udp_tunnel.h > @@ -205,9 +205,11 @@ static inline void udp_tunnel_encap_enable(struct sock *sk) > > #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL) > void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add); > +void udp_tunnel_update_gro_rcv(struct sock *sk, bool add); > #else > static inline void udp_tunnel_update_gro_lookup(struct net *net, > struct sock *sk, bool add) {} > +static inline void udp_tunnel_update_gro_rcv(struct sock *sk, bool add) {} > #endif > > static inline void udp_tunnel_cleanup_gro(struct sock *sk) > @@ -215,6 +217,8 @@ static inline void udp_tunnel_cleanup_gro(struct sock *sk) > struct udp_sock *up = udp_sk(sk); > struct net *net = sock_net(sk); > > + udp_tunnel_update_gro_rcv(sk, false); > + > if (!up->tunnel_list.pprev) > return; > > diff --git a/net/ipv4/udp_offload.c b/net/ipv4/udp_offload.c > index 054d4d4a8927f..500b2a20053cd 100644 > --- a/net/ipv4/udp_offload.c > +++ b/net/ipv4/udp_offload.c > @@ -15,6 +15,39 @@ > #include > > #if IS_ENABLED(CONFIG_NET_UDP_TUNNEL) > + > +/* > + * Dummy GRO tunnel callback; should never be invoked, exists > + * mainly to avoid dangling/NULL values for the udp tunnel > + * static call. > + */ > +static struct sk_buff *dummy_gro_rcv(struct sock *sk, > + struct list_head *head, > + struct sk_buff *skb) > +{ > + WARN_ON_ONCE(1); > + NAPI_GRO_CB(skb)->flush = 1; > + return NULL; > +} > + > +typedef struct sk_buff *(*udp_tunnel_gro_rcv_t)(struct sock *sk, > + struct list_head *head, > + struct sk_buff *skb); > + > +struct udp_tunnel_type_entry { > + udp_tunnel_gro_rcv_t gro_receive; > + refcount_t count; > +}; > + > +#define UDP_MAX_TUNNEL_TYPES (IS_ENABLED(CONFIG_GENEVE) + \ > + IS_ENABLED(CONFIG_VXLAN) * 2 + \ > + IS_ENABLED(CONFIG_FOE) * 2) > + > +DEFINE_STATIC_CALL(udp_tunnel_gro_rcv, dummy_gro_rcv); > +static DEFINE_STATIC_KEY_FALSE(udp_tunnel_static_call); > +static struct mutex udp_tunnel_gro_type_lock; > +static struct udp_tunnel_type_entry udp_tunnel_gro_types[UDP_MAX_TUNNEL_TYPES]; > +static unsigned int udp_tunnel_gro_type_nr; > static DEFINE_SPINLOCK(udp_tunnel_gro_lock); > > void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add) > @@ -43,6 +76,106 @@ void udp_tunnel_update_gro_lookup(struct net *net, struct sock *sk, bool add) > spin_unlock(&udp_tunnel_gro_lock); > } > EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_lookup); > + > +void udp_tunnel_update_gro_rcv(struct sock *sk, bool add) > +{ > + struct udp_tunnel_type_entry *cur = NULL, *avail = NULL; > + struct udp_sock *up = udp_sk(sk); > + int i, old_gro_type_nr; > + > + if (!up->gro_receive) > + return; > + > + mutex_lock(&udp_tunnel_gro_type_lock); > + for (i = 0; i < UDP_MAX_TUNNEL_TYPES; i++) { > + if (!refcount_read(&udp_tunnel_gro_types[i].count)) Optionally: && !avail, to fill the list from the front. And on delete avoid gaps. For instance, like __fanout_link/__fanout_unlink. Can stop sooner then. And list length is then implicit as i once found the first [i].count == zero. Then again, this list is always short. I can imagine you prefer to leave as is. > + avail = &udp_tunnel_gro_types[i]; > + else if (udp_tunnel_gro_types[i].gro_receive == up->gro_receive) > + cur = &udp_tunnel_gro_types[i]; > + } > + old_gro_type_nr = udp_tunnel_gro_type_nr; > + if (add) { > + /* > + * Update the matching entry, if found, or add a new one > + * if needed > + */ > + if (cur) { > + refcount_inc(&cur->count); > + goto out; > + } > + > + if (unlikely(!avail)) { > + pr_err_once("Too many UDP tunnel types, please increase UDP_MAX_TUNNEL_TYPES\n"); > + /* Ensure static call will never be enabled */ > + udp_tunnel_gro_type_nr = UDP_MAX_TUNNEL_TYPES + 2; > + goto out; > + } > + > + refcount_set(&avail->count, 1); > + avail->gro_receive = up->gro_receive; > + udp_tunnel_gro_type_nr++; > + } else { > + /* > + * The stack cleanups only successfully added tunnel, the > + * lookup on removal should never fail. > + */ > + if (WARN_ON_ONCE(!cur)) > + goto out; > + > + if (!refcount_dec_and_test(&cur->count)) > + goto out; > + udp_tunnel_gro_type_nr--; > + } > + > + if (udp_tunnel_gro_type_nr == 1) { > + for (i = 0; i < UDP_MAX_TUNNEL_TYPES; i++) { > + cur = &udp_tunnel_gro_types[i]; > + if (refcount_read(&cur->count)) { > + static_call_update(udp_tunnel_gro_rcv, > + cur->gro_receive); > + static_branch_enable(&udp_tunnel_static_call); > + } > + } > + } else if (old_gro_type_nr == 1) { > + static_branch_disable(&udp_tunnel_static_call); > + static_call_update(udp_tunnel_gro_rcv, dummy_gro_rcv); These operations must not be reorderd, or dummy_gro_rcv might get hit. If static calls are not configured, the last call is just a WRITE_ONCE. Similar for static_branch_disable if !CONFIG_JUMP_LABEL. > + } > + > +out: > + mutex_unlock(&udp_tunnel_gro_type_lock); > +} > +EXPORT_SYMBOL_GPL(udp_tunnel_update_gro_rcv); > + > +static void udp_tunnel_gro_init(void) > +{ > + mutex_init(&udp_tunnel_gro_type_lock); > +} > + > +static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk, > + struct list_head *head, > + struct sk_buff *skb) > +{ > + if (static_branch_likely(&udp_tunnel_static_call)) { > + if (unlikely(gro_recursion_inc_test(skb))) { > + NAPI_GRO_CB(skb)->flush |= 1; > + return NULL; > + } > + return static_call(udp_tunnel_gro_rcv)(sk, head, skb); > + } > + return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb); > +} > + > +#else > + > +static void udp_tunnel_gro_init(void) {} > + > +static struct sk_buff *udp_tunnel_gro_rcv(struct sock *sk, > + struct list_head *head, > + struct sk_buff *skb) > +{ > + return call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb); > +} > + > #endif > > static struct sk_buff *__skb_udp_tunnel_segment(struct sk_buff *skb, > @@ -654,7 +787,7 @@ struct sk_buff *udp_gro_receive(struct list_head *head, struct sk_buff *skb, > > skb_gro_pull(skb, sizeof(struct udphdr)); /* pull encapsulating udp header */ > skb_gro_postpull_rcsum(skb, uh, sizeof(struct udphdr)); > - pp = call_gro_receive_sk(udp_sk(sk)->gro_receive, sk, head, skb); > + pp = udp_tunnel_gro_rcv(sk, head, skb); > > out: > skb_gro_flush_final(skb, pp, flush); > @@ -804,5 +937,7 @@ int __init udpv4_offload_init(void) > .gro_complete = udp4_gro_complete, > }, > }; > + > + udp_tunnel_gro_init(); > return inet_add_offload(&net_hotdata.udpv4_offload, IPPROTO_UDP); > } > diff --git a/net/ipv4/udp_tunnel_core.c b/net/ipv4/udp_tunnel_core.c > index b5695826e57ad..c49fceea83139 100644 > --- a/net/ipv4/udp_tunnel_core.c > +++ b/net/ipv4/udp_tunnel_core.c > @@ -90,6 +90,8 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock, > > udp_tunnel_encap_enable(sk); > > + udp_tunnel_update_gro_rcv(sock->sk, true); > + > if (!sk->sk_dport && !sk->sk_bound_dev_if && sk_saddr_any(sock->sk)) > udp_tunnel_update_gro_lookup(net, sock->sk, true); > } > -- > 2.48.1 >