This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 3ed0090e23c net/vsock: keep the socket layer state per CPU in BMP mode
3ed0090e23c is described below

commit 3ed0090e23c9d413cf843483db95286a4192dafa
Author: Yongrong Wang <[email protected]>
AuthorDate: Thu Nov 26 00:00:46 2026 +0800

    net/vsock: keep the socket layer state per CPU in BMP mode
    
    Under CONFIG_BMP every CPU runs its own vsock layer over its own
    virtio and vhost buses, so the port counter, the work queue, the
    connection and transport lists with their locks, and the virtio and
    vhost driver structures must be private to each CPU. Define them with
    DEFINE_PER_CPU_BMP(), which leaves them plain globals when BMP is off.
    
    A per-CPU list head cannot be initialised statically to point at
    itself, so vsock_initialize() initialises both lists before it
    registers the transports.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 net/vsock/vsock_sockif.c | 27 ++++++++++++++++++++-------
 net/vsock/vsock_vhost.c  |  4 +++-
 net/vsock/vsock_virtio.c |  4 +++-
 3 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/net/vsock/vsock_sockif.c b/net/vsock/vsock_sockif.c
index 14bafb34193..d6d75dd2b35 100644
--- a/net/vsock/vsock_sockif.c
+++ b/net/vsock/vsock_sockif.c
@@ -36,6 +36,7 @@
 #include <nuttx/circbuf.h>
 #include <nuttx/fs/uio.h>
 #include <nuttx/nuttx.h>
+#include <nuttx/percpu.h>
 #include <nuttx/spinlock.h>
 
 #include "socket/socket.h"
@@ -251,18 +252,27 @@ const struct sock_intf_s g_vsock_sockif =
  * Private Data
  ****************************************************************************/
 
-static atomic_t g_vsock_port = VSOCK_LAST_RESERVED_PORT;
+static DEFINE_PER_CPU_BMP(atomic_t, g_vsock_port) =
+  VSOCK_LAST_RESERVED_PORT;
+#define g_vsock_port this_cpu_var_bmp(g_vsock_port)
 
-static FAR struct kwork_wqueue_s *g_vsock_wqueue = NULL;
+static DEFINE_PER_CPU_BSS_BMP(FAR struct kwork_wqueue_s *, g_vsock_wqueue);
+#define g_vsock_wqueue this_cpu_var_bmp(g_vsock_wqueue)
 
 /* List and their locks */
 
-static struct list_node g_vsock_conn = LIST_INITIAL_VALUE(g_vsock_conn);
-static struct list_node g_vsock_transport =
-  LIST_INITIAL_VALUE(g_vsock_transport);
+static DEFINE_PER_CPU_BSS_BMP(struct list_node, g_vsock_conn);
+#define g_vsock_conn this_cpu_var_bmp(g_vsock_conn)
 
-static spinlock_t g_vsock_conn_lock = SP_UNLOCKED;
-static spinlock_t g_vsock_transport_lock = SP_UNLOCKED;
+static DEFINE_PER_CPU_BSS_BMP(struct list_node, g_vsock_transport);
+#define g_vsock_transport this_cpu_var_bmp(g_vsock_transport)
+
+static DEFINE_PER_CPU_BMP(spinlock_t, g_vsock_conn_lock) = SP_UNLOCKED;
+#define g_vsock_conn_lock this_cpu_var_bmp(g_vsock_conn_lock)
+
+static DEFINE_PER_CPU_BMP(spinlock_t, g_vsock_transport_lock) =
+  SP_UNLOCKED;
+#define g_vsock_transport_lock this_cpu_var_bmp(g_vsock_transport_lock)
 
 /****************************************************************************
  * Private Functions
@@ -2546,6 +2556,9 @@ int vsock_initialize(void)
 {
   int ret = OK;
 
+  list_initialize(&g_vsock_conn);
+  list_initialize(&g_vsock_transport);
+
   g_vsock_wqueue = work_queue_create("vsock_work",
                                      CONFIG_NET_VSOCK_WORK_PRIORITY,
                                      NULL,
diff --git a/net/vsock/vsock_vhost.c b/net/vsock/vsock_vhost.c
index 604cf997cc1..6cb162a1414 100644
--- a/net/vsock/vsock_vhost.c
+++ b/net/vsock/vsock_vhost.c
@@ -30,6 +30,7 @@
 #include <sys/param.h>
 
 #include <nuttx/nuttx.h>
+#include <nuttx/percpu.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/vhost/vhost.h>
@@ -87,12 +88,13 @@ static ssize_t vsock_vhost_send_pkt(FAR struct 
vsock_transport_s *t,
  * Private Data
  ****************************************************************************/
 
-static struct vhost_driver g_vsock_vhost_driver =
+static DEFINE_PER_CPU_BMP(struct vhost_driver, g_vsock_vhost_driver) =
 {
   VIRTIO_ID_VSOCK,                               /* Device id */
   vsock_vhost_probe,                             /* Probe */
   vsock_vhost_remove,                            /* Remove */
 };
+#define g_vsock_vhost_driver this_cpu_var_bmp(g_vsock_vhost_driver)
 
 static const struct vsock_transport_ops_s g_vsock_vhost_transport_ops =
 {
diff --git a/net/vsock/vsock_virtio.c b/net/vsock/vsock_virtio.c
index 1e94e6108ef..cdf705e6ef0 100644
--- a/net/vsock/vsock_virtio.c
+++ b/net/vsock/vsock_virtio.c
@@ -33,6 +33,7 @@
 #include <nuttx/event.h>
 #include <nuttx/list.h>
 #include <nuttx/nuttx.h>
+#include <nuttx/percpu.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/virtio/virtio.h>
@@ -108,13 +109,14 @@ static ssize_t vsock_virtio_send_pkt(FAR struct 
vsock_transport_s *t,
  * Private Data
  ****************************************************************************/
 
-static struct virtio_driver g_vsock_virtio_driver =
+static DEFINE_PER_CPU_BMP(struct virtio_driver, g_vsock_virtio_driver) =
 {
   VIRTIO_ID_VSOCK,                                /* device id */
   vsock_virtio_probe,                             /* probe */
   vsock_virtio_remove,                            /* remove */
   vsock_virtio_dump,                              /* dump */
 };
+#define g_vsock_virtio_driver this_cpu_var_bmp(g_vsock_virtio_driver)
 
 static const struct vsock_transport_ops_s g_vsock_virtio_transport_ops =
 {

Reply via email to