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 42f146222db fs: add mempool for socket objects
42f146222db is described below

commit 42f146222db154659a9584812dd3d06ef8d72604
Author: ganjing <[email protected]>
AuthorDate: Thu Nov 26 21:22:22 2026 +0800

    fs: add mempool for socket objects
    
    Add dedicated mempool for socket objects to provide memory
    isolation from shared heap. Dynamic expansion uses fs_heap via
    private mempool callbacks.
    
    Signed-off-by: Gan Jing <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 Documentation/os/filesystem/file_descriptors.rst | 22 ++++++
 fs/Kconfig                                       | 20 ++++++
 fs/socket/accept.c                               |  6 +-
 fs/socket/socket.c                               | 91 ++++++++++++++++++++++--
 fs/socket/socket.h                               | 47 ++++++++++++
 fs/socket/socketpair.c                           |  6 +-
 6 files changed, 181 insertions(+), 11 deletions(-)

diff --git a/Documentation/os/filesystem/file_descriptors.rst 
b/Documentation/os/filesystem/file_descriptors.rst
index dd55094bb26..cb285615205 100644
--- a/Documentation/os/filesystem/file_descriptors.rst
+++ b/Documentation/os/filesystem/file_descriptors.rst
@@ -29,6 +29,28 @@ their own allocation policies. Caller-owned objects passed to
 ``file_open()`` are also outside this pool. Enabling this pool alone does
 not make ``open()`` free of heap allocations.
 
+Socket Object Allocation
+========================
+
+``CONFIG_FS_PREALLOC_SOCKETS`` reserves static ``struct socket`` objects.
+``CONFIG_FS_ALLOC_SOCKETS`` sets the number added from ``fs_heap`` when the
+pool is empty. Both default to zero, preserving direct heap allocation.
+A positive reserve with zero growth creates a fixed pool: allocation fails
+immediately when all objects are in use. Positive growth allows expansion
+with or without a static reserve. Expanded storage remains allocated for
+reuse, so memory use follows peak demand. Dynamic chunks use the independent
+FS heap when ``CONFIG_FS_HEAPSIZE`` is enabled, and the kernel heap otherwise.
+
+The pool serves objects created by ``socket()``, ``accept()`` and
+``socketpair()``, including copies made by file duplication. Objects return
+to the pool on close or failure rollback. ``dup()`` shares an open file
+object and does not require a separate socket object. Caller-owned sockets
+passed to the ``psock_*`` interfaces are outside this pool. Network connection
+structures, file objects and descriptor tables have separate allocation
+policies; enabling this pool alone does not eliminate heap allocations from
+socket operations. Include sockets opened during system startup when sizing
+a fixed pool.
+
 Drivers within Drivers
 ======================
 
diff --git a/fs/Kconfig b/fs/Kconfig
index c1c8c29710e..7ef5af5db97 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -270,6 +270,26 @@ config FS_ALLOC_FILES
                empty. Set to zero to disable expansion. Expanded blocks are 
retained
                for reuse, so heap use follows the peak number of open file 
objects.
 
+config FS_PREALLOC_SOCKETS
+       int "Preallocated socket objects"
+       default 0
+       range 0 65535
+       depends on NET
+       ---help---
+               Number of socket objects reserved in a static pool.  If this or
+               FS_ALLOC_SOCKETS is positive, socket objects use a dedicated 
pool.
+               When both are zero, objects are allocated directly from fs_heap.
+
+config FS_ALLOC_SOCKETS
+       int "Dynamic socket objects allocation"
+       default 0
+       range 0 65535
+       depends on NET
+       ---help---
+               Number of socket objects added to the pool from fs_heap when it 
is
+               empty.  Zero disables growth; a fixed pool fails immediately 
when
+               empty.  Expanded storage remains allocated for reuse.
+
 config FS_HEAPSIZE
        int "Independent heap bytes"
        default 0
diff --git a/fs/socket/accept.c b/fs/socket/accept.c
index c7d6ef3e085..38be1a5d3c2 100644
--- a/fs/socket/accept.c
+++ b/fs/socket/accept.c
@@ -44,7 +44,7 @@
 #include <nuttx/tls.h>
 #include <arch/irq.h>
 
-#include "fs_heap.h"
+#include "socket.h"
 
 /****************************************************************************
  * Private Functions
@@ -209,7 +209,7 @@ int accept4(int sockfd, FAR struct sockaddr *addr, FAR 
socklen_t *addrlen,
 
   tls_cleanup_push(tls_get_info(), accept4_cleanup, filep);
 
-  newsock = fs_heap_zalloc(sizeof(*newsock));
+  newsock = sock_alloc();
   if (newsock == NULL)
     {
       errcode = ENOMEM;
@@ -266,7 +266,7 @@ errout_with_psock:
   psock_close(newsock);
 
 errout_with_alloc:
-  fs_heap_free(newsock);
+  sock_free(newsock);
 
 errout_with_filep:
   tls_cleanup_pop(tls_get_info(), 0);
diff --git a/fs/socket/socket.c b/fs/socket/socket.c
index 32f6c0255df..4f993a14e55 100644
--- a/fs/socket/socket.c
+++ b/fs/socket/socket.c
@@ -31,6 +31,7 @@
 #include <nuttx/fs/fs.h>
 #include <nuttx/fs/uio.h>
 #include <nuttx/mm/mm.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/mm/map.h>
 
 #include <sys/socket.h>
@@ -42,6 +43,11 @@
 
 #include "inode/inode.h"
 #include "fs_heap.h"
+#include "socket.h"
+
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+#  define SOCKET_POOL_BLOCKSIZE ALIGN_UP(sizeof(struct socket), MM_ALIGN)
+#endif
 
 /****************************************************************************
  * Private Functions Prototypes
@@ -65,10 +71,36 @@ static ssize_t sock_file_readv(FAR struct file *filep,
 static ssize_t sock_file_writev(FAR struct file *filep,
                                 FAR struct uio *uio);
 
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+static FAR void *socket_pool_alloc(FAR struct mempool_s *pool, size_t size);
+static void socket_pool_free(FAR struct mempool_s *pool, FAR void *addr);
+static void socket_pool_check(FAR struct mempool_s *pool, FAR void *addr);
+#endif
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+static char g_socket_pool_buffer[CONFIG_FS_PREALLOC_SOCKETS *
+  MEMPOOL_REALBLOCKSIZE(SOCKET_POOL_BLOCKSIZE) + MEMPOOL_HEADER_SIZE]
+  aligned_data(MM_ALIGN);
+
+static struct mempool_s g_socket_mempool =
+{
+  .initialbase = g_socket_pool_buffer,
+  .initialsize = sizeof(g_socket_pool_buffer),
+  .blocksize   = SOCKET_POOL_BLOCKSIZE,
+  .expandsize  = CONFIG_FS_ALLOC_SOCKETS *
+                 MEMPOOL_REALBLOCKSIZE(SOCKET_POOL_BLOCKSIZE) +
+                 MEMPOOL_HEADER_SIZE,
+  .name        = "socket",
+  .alloc       = socket_pool_alloc,
+  .free        = socket_pool_free,
+  .check       = socket_pool_check,
+};
+#endif
+
 static const struct file_operations g_sock_fileops =
 {
   sock_file_open,     /* open */
@@ -100,12 +132,41 @@ static struct inode g_sock_inode =
  * Private Functions
  ****************************************************************************/
 
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+/****************************************************************************
+ * Name: socket_pool_alloc
+ ****************************************************************************/
+
+static FAR void *socket_pool_alloc(FAR struct mempool_s *pool, size_t size)
+{
+  return fs_heap_malloc(size);
+}
+
+/****************************************************************************
+ * Name: socket_pool_free
+ ****************************************************************************/
+
+static void socket_pool_free(FAR struct mempool_s *pool, FAR void *addr)
+{
+  fs_heap_free(addr);
+}
+
+/****************************************************************************
+ * Name: socket_pool_check
+ ****************************************************************************/
+
+static void socket_pool_check(FAR struct mempool_s *pool, FAR void *addr)
+{
+  DEBUGASSERT(fs_heap_member(addr));
+}
+#endif
+
 static int sock_file_open(FAR struct file *filep)
 {
   FAR struct socket *psock;
   int ret;
 
-  psock = fs_heap_zalloc(sizeof(*psock));
+  psock = sock_alloc();
   if (psock == NULL)
     {
       return -ENOMEM;
@@ -118,7 +179,7 @@ static int sock_file_open(FAR struct file *filep)
     }
   else
     {
-      fs_heap_free(psock);
+      sock_free(psock);
     }
 
   return ret;
@@ -127,7 +188,7 @@ static int sock_file_open(FAR struct file *filep)
 static int sock_file_close(FAR struct file *filep)
 {
   psock_close(filep->f_priv);
-  fs_heap_free(filep->f_priv);
+  sock_free(filep->f_priv);
   return 0;
 }
 
@@ -201,6 +262,26 @@ static ssize_t sock_file_writev(FAR struct file *filep,
  * Public Functions
  ****************************************************************************/
 
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+/****************************************************************************
+ * Name: sock_alloc
+ ****************************************************************************/
+
+FAR struct socket *sock_alloc(void)
+{
+  return mempool_zallocate(&g_socket_mempool, 0);
+}
+
+/****************************************************************************
+ * Name: sock_free
+ ****************************************************************************/
+
+void sock_free(FAR struct socket *psock)
+{
+  mempool_release(&g_socket_mempool, psock);
+}
+#endif
+
 /****************************************************************************
  * Name: sockfd_allocate
  *
@@ -323,7 +404,7 @@ int socket(int domain, int type, int protocol)
       oflags |= O_NONBLOCK;
     }
 
-  psock = fs_heap_zalloc(sizeof(*psock));
+  psock = sock_alloc();
   if (psock == NULL)
     {
       ret = -ENOMEM;
@@ -355,7 +436,7 @@ errout_with_psock:
   psock_close(psock);
 
 errout_with_alloc:
-  fs_heap_free(psock);
+  sock_free(psock);
 
 errout:
   set_errno(-ret);
diff --git a/fs/socket/socket.h b/fs/socket/socket.h
new file mode 100644
index 00000000000..b8afffd1cb3
--- /dev/null
+++ b/fs/socket/socket.h
@@ -0,0 +1,47 @@
+/****************************************************************************
+ * fs/socket/socket.h
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __FS_SOCKET_SOCKET_H
+#define __FS_SOCKET_SOCKET_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+#include <nuttx/net/net.h>
+
+#include "fs_heap.h"
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+#if CONFIG_FS_PREALLOC_SOCKETS > 0 || CONFIG_FS_ALLOC_SOCKETS > 0
+FAR struct socket *sock_alloc(void);
+void sock_free(FAR struct socket *psock);
+#else
+#  define sock_alloc() fs_heap_zalloc(sizeof(struct socket))
+#  define sock_free(p) fs_heap_free(p)
+#endif
+
+#endif /* __FS_SOCKET_SOCKET_H */
diff --git a/fs/socket/socketpair.c b/fs/socket/socketpair.c
index 91304f8112c..5f73bd94bbd 100644
--- a/fs/socket/socketpair.c
+++ b/fs/socket/socketpair.c
@@ -38,7 +38,7 @@
 #include <nuttx/debug.h>
 
 #include "inode/inode.h"
-#include "fs_heap.h"
+#include "socket.h"
 
 /****************************************************************************
  * Public Functions
@@ -80,7 +80,7 @@ int socketpair(int domain, int type, int protocol, int sv[2])
 
   for (k = 0; k < 2; k++)
     {
-      psocks[k] = fs_heap_zalloc(sizeof(*psocks[k]));
+      psocks[k] = sock_alloc();
       if (psocks[k] == NULL)
         {
           ret = -ENOMEM;
@@ -132,7 +132,7 @@ errout_with_psock:
 errout_with_alloc:
   for (i = j; i < k; i++)
     {
-      fs_heap_free(psocks[i]);
+      sock_free(psocks[i]);
     }
 
 errout:

Reply via email to