From nobody Mon Feb 9 02:12:35 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 0CFA71CF7D5 for ; Wed, 24 Dec 2025 00:28:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766536139; cv=none; b=DeCVqZHhSrxlltu/G8UWn9DNwrOOw3ksnFGCuz/FYwLheLAS7rQM7KPW/aC4k2GmBBR09FcTtc35xrGvFcFtcxUSvrkjMlP0t1CFT046OmfDEjof5mLvEpziD3Et84BnTDD9R2MmHETYRpDkQrn5xtCuEElDZdQKT7ppq+lsG+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766536139; c=relaxed/simple; bh=7wDYgNx+BgqVnuE0T2X8agHCrv8JLpc6NaCVH5tP4l0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TnuE2TQW2GsuAfaZdwhHDDMkdoaJdKZsRXZAYrF1hTJPf3W6823ODZnaqshuTokJclommEYdiPUI4VnTXr5vqmZsXbmuSsHZeiaAU70KwOxi18QacHTxP2Q/o9lucIr6AMSQU9hyrfZCMEmccVZgmJa2wNaG2Sv4ZoO4awDkS0k= 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=CWh3lOE+; arc=none smtp.client-ip=209.85.210.174 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="CWh3lOE+" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7bab7c997eeso6237617b3a.0 for ; Tue, 23 Dec 2025 16:28:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766536136; x=1767140936; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=1idDMvZTSBBVNr5LBgZDXh7c7n41TkvVwZ0J4jmyayA=; b=CWh3lOE+nnquhJ6m0WJ/7svTap0DUEs+RT3Knkla5u85yK06YmHw4g3Zpsl57lpQEU 2FO3AvfWrxmqFrtbIkGYW8jAOOe6atIp8AF40SSipgsbvi73MOUf8OgUAwajLpPPylgN 37DjAf3PBUTyWV69KYgDl2NEEjXZgfxgmLklrBk2GsUsoSml8yoEvmwhEi8R29wZX3fl tjsTRKJjeMQqfNOn9jGPRRQlg1zotuX/Q6YRgIKK4H2ltTK9SV0eto4cpqrEpVIW/XXd aQJqNFH5P9aX0FMmAAewYLTRbyZ3jQj7NtGfSYjxuKwyAnwbpLHSb/w1U+26JJRUDTAO R35Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766536136; x=1767140936; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1idDMvZTSBBVNr5LBgZDXh7c7n41TkvVwZ0J4jmyayA=; b=Kqtlly0QZ81eOYJlbz4fopa0cxt86MM6XerwUHhtf338dL44//aqcGNICQUDpOq1dc IvOnjrpqB+AKvwCyL6bBMqT69PZFSYrmnwfyhIfEix7Vo+3FtX/aXwgmBA4HobFv3ogm YZPxJ7UtsQwWH7+4JNXrLmHdElEjwlN26xyx9VmM2Ub3ADrtd5RH7LbWCTz8cQbF32fg yd1Jl4c7QfsMi80n72uWIg+6SRXCSJV1EwsVtk1ErVLvLS56VgMpAvUARoFjBASMwoII G/gQ2FaZllJxfosKxvnHxeXFwNCoDkDQeS1U6IUs/SvxHSUc8l9zXajrLO9ZC516XGBH VHIg== X-Gm-Message-State: AOJu0Yyabz7wPRb8NazW3QPUZYkMZ0x79QMRo9XReCUC6z1hrobBLTiH 6Nkp1j2Yz99vDad81VCMJVMGSl+0NUcFx4U5FDTR8OmnrfDlJ69YOWUv X-Gm-Gg: AY/fxX70cgps7Z+RuBZDpI69hV3oI04IuwuQPxDlw2OPPkrdzwWBBr8Y9wvASkP8qfr pEmg3eNOBEtz16Gqk8dhRXEgXA5d0VBbIM3lpwEkZuqpIzmOLDA9/MoCLGBmYv0KfUPIOrAB0HP OP57cRabLbWFhJ0dLu4uKXn1jX8yI9qRsj8D67fBvD4ZJWkpmL9ALc42vdX3+Ehq90PTihor94E 3ZLJKBSJ0DbK8LhoI6pEXjhE6zY7YmMKAn+nk7tfan2+Yzhx135nU0ocOobejrP0YgxtT3h1Nia ac/unLQge/hCI9/3dz/Yvyir64yvQSkF6AxlJTfi1nGxqizYOs6S8ux6Y9uvBPYoXLmmOpS2fCi mjSdn2g2Z70nWz4yf93WXmQV7+cy2+OoIiIMqmmkS2AzmqdM8TC+trHe2lzjq7eklpDVMTAEciJ lRGLxEJsv+yxQTzXfpjSA= X-Google-Smtp-Source: AGHT+IFf9PGpVgIYi53KgiXyi1OO/xQjeYSDD1E1/gFj625d4hHvxV/oBQyq3/8jVDfT3E7MmlVH9w== X-Received: by 2002:a05:6a00:8013:b0:7e8:4471:ae61 with SMTP id d2e1a72fcca58-7ff67654e35mr13734006b3a.45.1766536136203; Tue, 23 Dec 2025 16:28:56 -0800 (PST) Received: from localhost ([2a03:2880:2ff:6::]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7ff7e8947a1sm14718466b3a.67.2025.12.23.16.28.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Dec 2025 16:28:55 -0800 (PST) From: Bobby Eshleman Date: Tue, 23 Dec 2025 16:28:35 -0800 Subject: [PATCH RFC net-next v13 01/13] vsock: add per-net vsock NS mode state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251223-vsock-vmtest-v13-1-9d6db8e7c80b@meta.com> References: <20251223-vsock-vmtest-v13-0-9d6db8e7c80b@meta.com> In-Reply-To: <20251223-vsock-vmtest-v13-0-9d6db8e7c80b@meta.com> To: Stefano Garzarella , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Stefan Hajnoczi , "Michael S. Tsirkin" , Jason Wang , =?utf-8?q?Eugenio_P=C3=A9rez?= , Xuan Zhuo , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , Broadcom internal kernel review list , Shuah Khan , Long Li Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, netdev@vger.kernel.org, kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kselftest@vger.kernel.org, berrange@redhat.com, Sargun Dhillon , Bobby Eshleman , Bobby Eshleman X-Mailer: b4 0.14.3 From: Bobby Eshleman Add the per-net vsock NS mode state. This only adds the structure for holding the mode and some of the functions for setting/getting and checking the mode, but does not integrate the functionality yet. Future patches add the uAPI and transport-specific usage of these structures and helpers. Signed-off-by: Bobby Eshleman --- Changes in v13: - remove net_mode because net->vsock.mode becomes immutable, no need to save the mode when vsocks are created. - add the new helpers for child_ns_mode to support ns_mode inheriting the mode from child_ns_mode. - because ns_mode is immutable and child_ns_mode can be changed multiple times, remove the write-once lock. - simplify vsock_net_check_mode() to no longer take mode arguments since the mode can be accessed via the net pointers without fear of the mode changing. - add logic in vsock_net_check_mode() to infer VSOCK_NET_MODE_GLOBAL from NULL namespaces in order to allow only net pointers to be passed to vsock_net_check_mode(), while still allowing namespace-unaware transports to force global mode. Changes in v10: - change mode_locked to int (Stefano) Changes in v9: - use xchg(), WRITE_ONCE(), READ_ONCE() for mode and mode_locked (Stefano) - clarify mode0/mode1 meaning in vsock_net_check_mode() comment - remove spin lock in net->vsock (not used anymore) - change mode from u8 to enum vsock_net_mode in vsock_net_write_mode() Changes in v7: - clarify vsock_net_check_mode() comments - change to `orig_net_mode =3D=3D VSOCK_NET_MODE_GLOBAL && orig_net_mode = =3D=3D vsk->orig_net_mode` - remove extraneous explanation of `orig_net_mode` - rename `written` to `mode_locked` - rename `vsock_hdr` to `sysctl_hdr` - change `orig_net_mode` to `net_mode` - make vsock_net_check_mode() more generic by taking just net pointers and modes, instead of a vsock_sock ptr, for reuse by transports (e.g., vhost_vsock) Changes in v6: - add orig_net_mode to store mode at creation time which will be used to avoid breakage when namespace changes mode during socket/VM lifespan Changes in v5: - use /proc/sys/net/vsock/ns_mode instead of /proc/net/vsock_ns_mode - change from net->vsock.ns_mode to net->vsock.mode - change vsock_net_set_mode() to vsock_net_write_mode() - vsock_net_write_mode() returns bool for write success to avoid need to use vsock_net_mode_can_set() - remove vsock_net_mode_can_set() --- MAINTAINERS | 1 + include/net/af_vsock.h | 42 ++++++++++++++++++++++++++++++++++++++++++ include/net/net_namespace.h | 4 ++++ include/net/netns/vsock.h | 17 +++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 454b8ed119e9..38d24e5a957c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -27516,6 +27516,7 @@ L: netdev@vger.kernel.org S: Maintained F: drivers/vhost/vsock.c F: include/linux/virtio_vsock.h +F: include/net/netns/vsock.h F: include/uapi/linux/virtio_vsock.h F: net/vmw_vsock/virtio_transport.c F: net/vmw_vsock/virtio_transport_common.c diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index d40e978126e3..6f5bc9dbefa5 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -10,6 +10,7 @@ =20 #include #include +#include #include #include =20 @@ -256,4 +257,45 @@ static inline bool vsock_msgzerocopy_allow(const struc= t vsock_transport *t) { return t->msgzerocopy_allow && t->msgzerocopy_allow(); } + +static inline enum vsock_net_mode vsock_net_mode(struct net *net) +{ + return READ_ONCE(net->vsock.mode); +} + +static inline void vsock_net_set_child_mode(struct net *net, + enum vsock_net_mode mode) +{ + WRITE_ONCE(net->vsock.child_ns_mode, mode); +} + +static inline enum vsock_net_mode vsock_net_child_mode(struct net *net) +{ + return READ_ONCE(net->vsock.child_ns_mode); +} + +/* Return true if two namespaces pass the mode rules. Otherwise, return fa= lse. + * + * A NULL namespace is treated as VSOCK_NET_MODE_GLOBAL. + * + * Read more about modes in the comment header of net/vmw_vsock/af_vsock.c. + */ +static inline bool vsock_net_check_mode(struct net *ns0, struct net *ns1) +{ + enum vsock_net_mode mode0, mode1; + + /* Any vsocks within the same network namespace are always reachable, + * regardless of the mode. + */ + if (net_eq(ns0, ns1)) + return true; + + mode0 =3D ns0 ? vsock_net_mode(ns0) : VSOCK_NET_MODE_GLOBAL; + mode1 =3D ns1 ? vsock_net_mode(ns1) : VSOCK_NET_MODE_GLOBAL; + + /* Different namespaces are only reachable if they are both + * global mode. + */ + return mode0 =3D=3D VSOCK_NET_MODE_GLOBAL && mode0 =3D=3D mode1; +} #endif /* __AF_VSOCK_H__ */ diff --git a/include/net/net_namespace.h b/include/net/net_namespace.h index cb664f6e3558..66d3de1d935f 100644 --- a/include/net/net_namespace.h +++ b/include/net/net_namespace.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -196,6 +197,9 @@ struct net { /* Move to a better place when the config guard is removed. */ struct mutex rtnl_mutex; #endif +#if IS_ENABLED(CONFIG_VSOCKETS) + struct netns_vsock vsock; +#endif } __randomize_layout; =20 #include diff --git a/include/net/netns/vsock.h b/include/net/netns/vsock.h new file mode 100644 index 000000000000..e2325e2d6ec5 --- /dev/null +++ b/include/net/netns/vsock.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __NET_NET_NAMESPACE_VSOCK_H +#define __NET_NET_NAMESPACE_VSOCK_H + +#include + +enum vsock_net_mode { + VSOCK_NET_MODE_GLOBAL, + VSOCK_NET_MODE_LOCAL, +}; + +struct netns_vsock { + struct ctl_table_header *sysctl_hdr; + enum vsock_net_mode mode; + enum vsock_net_mode child_ns_mode; +}; +#endif /* __NET_NET_NAMESPACE_VSOCK_H */ --=20 2.47.3