Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 7C55CA5C for ; Thu, 20 Jul 2023 11:09:06 +0000 (UTC) Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A0C5268F for ; Thu, 20 Jul 2023 04:09:04 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-c4f27858e4eso585087276.1 for ; Thu, 20 Jul 2023 04:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689851343; x=1690456143; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=kZ/uT84dJdZNFLugauA38+ypOQoDq/O3+1F7wzLyPE8=; b=NblDY+i712KfcyQKGLSEP0wm61ROYfk+N3wUg7ZzSMahky1MOsIXbpTVbgje9MHX2T HrRU5lWAyKNDEA5tJYdYx7v/+f9pFr+gh9Kj3S4MZrIHVDwCN+/FjbCJtfMtpOmEDd0c 0PDoxsjHkmy3TUSh62sn8O+RHSyWkzMkkexGnOSOFr0E739e6n+FxB4GDyTiIvUd8j3A gmnxDyzPP4uOwrV3N7EhgYGmx5lQEevAxRzagmbxVNP/6F9CDIIa7xrlKMp+SYjQ+lWI pUhMGKswuyeCadO7KY0o4qsjyAEXQupoQMEO2An/nowRp/t01NWBdU0mA7I6MvlZO4HM o4sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689851343; x=1690456143; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=kZ/uT84dJdZNFLugauA38+ypOQoDq/O3+1F7wzLyPE8=; b=O74haYNRyJlSgVyPGDh1BH5bkdoti/ruKKRCnaVMZNYEW0X3Glalz+gpnWmcsVLUGw ZpoEW8o9smoboBC3EU96nrzbC0YDr7Aii3SOnpSkPF6Umug2tzhw477XhvRjpbFmDPad oU6ZBW1Nv7ETKTxmU2akkEAXzfOVJ+6pfOUGT9TTnKdJaRRB3Yutln4FYtHrtA/3LcUp gHYt8x8sI1V1s3mwCbynEmoCvNV7jXva9w7tca2ULtu8Kj9fM4iAXJ1HbUKu8E3GH94M 4mN6Dew/bjIZPtH3v95bfVIAbt6U1wVh+RqkjFtQ+lx8mBmQuuFY1c/GVTr61VATbr+4 SnPw== X-Gm-Message-State: ABy/qLZABuKMH24UEvEdIKnadQ/Mo/zzlh+1upwKB7HwZot9TphNld6K arOpgUaDGUpiOOKAF/yyqUcTsTC+TjZBDQ== X-Google-Smtp-Source: APBJJlFOU/AfATyKeP5vQ62Ozg+wsnvhqiQv3QtW5aydmB8OLb/RKcPQRthnPJpZi4/QULx+EaYRIUW847Jrbg== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a25:db83:0:b0:c78:c530:6345 with SMTP id g125-20020a25db83000000b00c78c5306345mr39851ybf.7.1689851343555; Thu, 20 Jul 2023 04:09:03 -0700 (PDT) Date: Thu, 20 Jul 2023 11:09:01 +0000 Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.255.g8b1d071c50-goog Message-ID: <20230720110901.83207-1-edumazet@google.com> Subject: [PATCH v2 net-next] ipv6: remove hard coded limitation on ipv6_pinfo From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet , Chao Wu , Wei Wang , Coco Li , YiFei Zhu Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net IPv6 inet sockets are supposed to have a "struct ipv6_pinfo" field at the end of their definition, so that inet6_sk_generic() can derive from socket size the offset of the "struct ipv6_pinfo". This is very fragile, and prevents adding bigger alignment in sockets, because inet6_sk_generic() does not work if the compiler adds padding after the ipv6_pinfo component. We are currently working on a patch series to reorganize TCP structures for better data locality and found issues similar to the one fixed in commit f5d547676ca0 ("tcp: fix tcp_inet6_sk() for 32bit kernels") Alternative would be to force an alignment on "struct ipv6_pinfo", greater or equal to __alignof__(any ipv6 sock) to ensure there is no padding. This does not look great. v2: fix typo in mptcp_proto_v6_init() (Paolo) Signed-off-by: Eric Dumazet Cc: Chao Wu Cc: Wei Wang Cc: Coco Li Cc: YiFei Zhu --- include/linux/ipv6.h | 15 ++++----------- include/net/sock.h | 1 + net/dccp/ipv6.c | 1 + net/dccp/ipv6.h | 4 ---- net/ipv6/af_inet6.c | 4 ++-- net/ipv6/ping.c | 1 + net/ipv6/raw.c | 1 + net/ipv6/tcp_ipv6.c | 1 + net/ipv6/udp.c | 1 + net/ipv6/udplite.c | 1 + net/l2tp/l2tp_ip6.c | 4 +--- net/mptcp/protocol.c | 1 + net/sctp/socket.c | 1 + 13 files changed, 16 insertions(+), 20 deletions(-) diff --git a/include/linux/ipv6.h b/include/linux/ipv6.h index 839247a4f48ea76b5d6daa9a54a7b87627635066..660012997f54ca12274f3c61e2ef1f42a7655ce9 100644 --- a/include/linux/ipv6.h +++ b/include/linux/ipv6.h @@ -199,14 +199,7 @@ struct inet6_cork { u8 tclass; }; -/** - * struct ipv6_pinfo - ipv6 private area - * - * In the struct sock hierarchy (tcp6_sock, upd6_sock, etc) - * this _must_ be the last member, so that inet6_sk_generic - * is able to calculate its offset from the base struct sock - * by using the struct proto->slab_obj_size member. -acme - */ +/* struct ipv6_pinfo - ipv6 private area */ struct ipv6_pinfo { struct in6_addr saddr; struct in6_pktinfo sticky_pktinfo; @@ -306,19 +299,19 @@ struct raw6_sock { __u32 offset; /* checksum offset */ struct icmp6_filter filter; __u32 ip6mr_table; - /* ipv6_pinfo has to be the last member of raw6_sock, see inet6_sk_generic */ + struct ipv6_pinfo inet6; }; struct udp6_sock { struct udp_sock udp; - /* ipv6_pinfo has to be the last member of udp6_sock, see inet6_sk_generic */ + struct ipv6_pinfo inet6; }; struct tcp6_sock { struct tcp_sock tcp; - /* ipv6_pinfo has to be the last member of tcp6_sock, see inet6_sk_generic */ + struct ipv6_pinfo inet6; }; diff --git a/include/net/sock.h b/include/net/sock.h index 2eb916d1ff64866671a2197965eb857b47b810d9..7ae44bf866af5cd788ff10021a441d96b1f8d937 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1339,6 +1339,7 @@ struct proto { struct kmem_cache *slab; unsigned int obj_size; + unsigned int ipv6_pinfo_offset; slab_flags_t slab_flags; unsigned int useroffset; /* Usercopy region offset */ unsigned int usersize; /* Usercopy region size */ diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c index 7249ef218178743ce7936fcf2f605616a419370e..e03b5331df6d71722dd4ab0a444ada7b06958ee6 100644 --- a/net/dccp/ipv6.c +++ b/net/dccp/ipv6.c @@ -1056,6 +1056,7 @@ static struct proto dccp_v6_prot = { .orphan_count = &dccp_orphan_count, .max_header = MAX_DCCP_HEADER, .obj_size = sizeof(struct dccp6_sock), + .ipv6_pinfo_offset = offsetof(struct dccp6_sock, inet6), .slab_flags = SLAB_TYPESAFE_BY_RCU, .rsk_prot = &dccp6_request_sock_ops, .twsk_prot = &dccp6_timewait_sock_ops, diff --git a/net/dccp/ipv6.h b/net/dccp/ipv6.h index 7e4c2a3b322b51377ebf7575cfae49aeb51510a2..c5d14c48def179958207c8c3d62b83176183ef74 100644 --- a/net/dccp/ipv6.h +++ b/net/dccp/ipv6.h @@ -13,10 +13,6 @@ struct dccp6_sock { struct dccp_sock dccp; - /* - * ipv6_pinfo has to be the last member of dccp6_sock, - * see inet6_sk_generic. - */ struct ipv6_pinfo inet6; }; diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c index 5d593ddc0347ebd13788b1319990f167de833d9a..9f9c4b838664a76cb4d7efbeb16056e22f12b358 100644 --- a/net/ipv6/af_inet6.c +++ b/net/ipv6/af_inet6.c @@ -102,9 +102,9 @@ bool ipv6_mod_enabled(void) } EXPORT_SYMBOL_GPL(ipv6_mod_enabled); -static __inline__ struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) +static struct ipv6_pinfo *inet6_sk_generic(struct sock *sk) { - const int offset = sk->sk_prot->obj_size - sizeof(struct ipv6_pinfo); + const int offset = sk->sk_prot->ipv6_pinfo_offset; return (struct ipv6_pinfo *)(((u8 *)sk) + offset); } diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c index f804c11e2146cba9e9b8c10337341dc3fb4e0143..2a0e8bc0739831e744bcbf47c3777f793fe710ac 100644 --- a/net/ipv6/ping.c +++ b/net/ipv6/ping.c @@ -215,6 +215,7 @@ struct proto pingv6_prot = { .get_port = ping_get_port, .put_port = ping_unhash, .obj_size = sizeof(struct raw6_sock), + .ipv6_pinfo_offset = offsetof(struct raw6_sock, inet6), }; EXPORT_SYMBOL_GPL(pingv6_prot); diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c index ac1cef094c5f200d34a45eeb06f2f7356c87ad6d..0fcf1b8908079d459e2a29822a116ff7eac06b67 100644 --- a/net/ipv6/raw.c +++ b/net/ipv6/raw.c @@ -1216,6 +1216,7 @@ struct proto rawv6_prot = { .hash = raw_hash_sk, .unhash = raw_unhash_sk, .obj_size = sizeof(struct raw6_sock), + .ipv6_pinfo_offset = offsetof(struct raw6_sock, inet6), .useroffset = offsetof(struct raw6_sock, filter), .usersize = sizeof_field(struct raw6_sock, filter), .h.raw_hash = &raw_v6_hashinfo, diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c index 40dd92a2f4807960c7939a19adccdd1b493c30b1..c9d41c77d39392a3f1f0e5c256e379cf92fc4a9e 100644 --- a/net/ipv6/tcp_ipv6.c +++ b/net/ipv6/tcp_ipv6.c @@ -2175,6 +2175,7 @@ struct proto tcpv6_prot = { .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem), .max_header = MAX_TCP_HEADER, .obj_size = sizeof(struct tcp6_sock), + .ipv6_pinfo_offset = offsetof(struct tcp6_sock, inet6), .slab_flags = SLAB_TYPESAFE_BY_RCU, .twsk_prot = &tcp6_timewait_sock_ops, .rsk_prot = &tcp6_request_sock_ops, diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index b7c972aa09a75404e0edb33f0354c53702c991f8..95c75d8f73d5144a0e8b024fc42b3d78b5d4e00b 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -1798,6 +1798,7 @@ struct proto udpv6_prot = { .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), .obj_size = sizeof(struct udp6_sock), + .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6), .h.udp_table = NULL, .diag_destroy = udp_abort, }; diff --git a/net/ipv6/udplite.c b/net/ipv6/udplite.c index 8e010d07917a7a0e20cce05251a1c49605bba757..267d491e970753a1bb16babb8fbe85cd67cd7062 100644 --- a/net/ipv6/udplite.c +++ b/net/ipv6/udplite.c @@ -67,6 +67,7 @@ struct proto udplitev6_prot = { .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min), .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min), .obj_size = sizeof(struct udp6_sock), + .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6), .h.udp_table = &udplite_table, }; diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c index b1623f9c4f921791ab541ecf93695ea19addaa5a..2eee95a00c0534b69c0e5170800ff2dedda8c5bc 100644 --- a/net/l2tp/l2tp_ip6.c +++ b/net/l2tp/l2tp_ip6.c @@ -36,9 +36,6 @@ struct l2tp_ip6_sock { u32 conn_id; u32 peer_conn_id; - /* ipv6_pinfo has to be the last member of l2tp_ip6_sock, see - * inet6_sk_generic - */ struct ipv6_pinfo inet6; }; @@ -730,6 +727,7 @@ static struct proto l2tp_ip6_prot = { .hash = l2tp_ip6_hash, .unhash = l2tp_ip6_unhash, .obj_size = sizeof(struct l2tp_ip6_sock), + .ipv6_pinfo_offset = offsetof(struct l2tp_ip6_sock, inet6), }; static const struct proto_ops l2tp_ip6_ops = { diff --git a/net/mptcp/protocol.c b/net/mptcp/protocol.c index 3613489eb6e3b0871da09f06561cc251fe2e0b80..22323b1fa18427069e9c2443fd53f70fb0f2d26d 100644 --- a/net/mptcp/protocol.c +++ b/net/mptcp/protocol.c @@ -3988,6 +3988,7 @@ int __init mptcp_proto_v6_init(void) strcpy(mptcp_v6_prot.name, "MPTCPv6"); mptcp_v6_prot.slab = NULL; mptcp_v6_prot.obj_size = sizeof(struct mptcp6_sock); + mptcp_v6_prot.ipv6_pinfo_offset = offsetof(struct mptcp6_sock, np); err = proto_register(&mptcp_v6_prot, 1); if (err) diff --git a/net/sctp/socket.c b/net/sctp/socket.c index 9388d98aebc033f195e56d5295fd998996d41f7e..6e3d28aa587cdb64f7a1ac384fa28a34d4c6739c 100644 --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -9732,6 +9732,7 @@ struct proto sctpv6_prot = { .unhash = sctp_unhash, .no_autobind = true, .obj_size = sizeof(struct sctp6_sock), + .ipv6_pinfo_offset = offsetof(struct sctp6_sock, inet6), .useroffset = offsetof(struct sctp6_sock, sctp.subscribe), .usersize = offsetof(struct sctp6_sock, sctp.initmsg) - offsetof(struct sctp6_sock, sctp.subscribe) + -- 2.41.0.255.g8b1d071c50-goog