[RFC net-next 02/17] tls: factor out __tls_build_proto for mptcp support
From: Geliang Tang <geliang@kernel.org>
Date: 2026-06-22 10:44:19
Also in:
mptcp
Subsystem:
networking [general], networking [tls], the rest · Maintainers:
"David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, John Fastabend, Sabrina Dubroca, Linus Torvalds
From: Geliang Tang <redacted> tls_build_proto() contains duplicated logic for building IPv4 and IPv6 TLS protocol caches. Factor out the common code into a new helper __tls_build_proto(), which takes the saved protocol pointer, mutex, and IP family as parameters. This prepares for adding MPTCP support by reducing the amount of duplicated code needed when introducing additional protocol variants. No functional change intended. Co-developed-by: Gang Yan <redacted> Signed-off-by: Gang Yan <redacted> Co-developed-by: Zqiang <qiang.zhang@linux.dev> Signed-off-by: Zqiang <qiang.zhang@linux.dev> Signed-off-by: Geliang Tang <redacted> --- net/tls/tls_main.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-)
diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c
index 9675c75bc50c..be824affd1b1 100644
--- a/net/tls/tls_main.c
+++ b/net/tls/tls_main.c@@ -968,35 +968,37 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG] #endif } -static void tls_build_proto(struct sock *sk) +static void __tls_build_proto(struct sock *sk, + const struct proto *saved_prot, + struct mutex *prot_mutex, + int family) { int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4; struct proto *prot = READ_ONCE(sk->sk_prot); - /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */ - if (ip_ver == TLSV6 && - unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) { - mutex_lock(&tcpv6_prot_mutex); - if (likely(prot != saved_tcpv6_prot)) { - build_protos(tls_prots[TLSV6], prot); - build_proto_ops(tls_proto_ops[TLSV6], - sk->sk_socket->ops); - smp_store_release(&saved_tcpv6_prot, prot); + if (ip_ver == family) { + /* smp_load_acquire pairs with smp_store_release below */ + if (unlikely(prot != smp_load_acquire(&saved_prot))) { + mutex_lock(prot_mutex); + if (likely(prot != saved_prot)) { + build_protos(tls_prots[family], prot); + build_proto_ops(tls_proto_ops[family], + sk->sk_socket->ops); + /* pairs with smp_load_acquire above */ + smp_store_release(&saved_prot, prot); + } + mutex_unlock(prot_mutex); } - mutex_unlock(&tcpv6_prot_mutex); } +} - if (ip_ver == TLSV4 && - unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) { - mutex_lock(&tcpv4_prot_mutex); - if (likely(prot != saved_tcpv4_prot)) { - build_protos(tls_prots[TLSV4], prot); - build_proto_ops(tls_proto_ops[TLSV4], - sk->sk_socket->ops); - smp_store_release(&saved_tcpv4_prot, prot); - } - mutex_unlock(&tcpv4_prot_mutex); - } +static void tls_build_proto(struct sock *sk) +{ + /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */ + __tls_build_proto(sk, saved_tcpv6_prot, &tcpv6_prot_mutex, + TLSV6); + __tls_build_proto(sk, saved_tcpv4_prot, &tcpv4_prot_mutex, + TLSV4); } static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
--
2.53.0