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

xiaoxiang781216 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 09fd446f4a6 fs/tmpfs: keep the data buffer in place while the file is 
mapped
09fd446f4a6 is described below

commit 09fd446f4a69cd8bf8e1747e20bbeb7a5655668c
Author: jingfei <[email protected]>
AuthorDate: Tue Nov 24 11:38:35 2026 +0800

    fs/tmpfs: keep the data buffer in place while the file is mapped
    
    tmpfs_mmap() hands out a pointer into tfo_data, but a later write(),
    ftruncate() or open(O_TRUNC) goes through tmpfs_realloc_file(), which
    may realloc() or free that buffer and leave the mapping pointing at
    freed memory.
    
    Count the mappings of a file in tfo_maps.  While it is non-zero,
    tmpfs_realloc_file() only changes the file size within the current
    allocation and fails with -ENOMEM when more space is needed.  The
    count lives in the file object, so mappings from any process are
    covered, and process exit drops it through tmpfs_unmap().
    
    tmpfs_mmap() now takes the mm_map lock before the file lock, the
    order munmap() uses, and counts a mapping only once it is in the
    list; it fails with -ENOMEM when the reference count would overflow.
    tmpfs_unmap() takes the file lock before it removes the entry, so the
    counts always go away together with the mapping.
    
    Signed-off-by: jingfei <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 102 +++++++++++++++++++++++++++++++++++++++++-----------
 fs/tmpfs/fs_tmpfs.h |   1 +
 2 files changed, 82 insertions(+), 21 deletions(-)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 181c56b382e..e081ba1c7f4 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -360,6 +360,22 @@ static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
   size_t allocsize;
   size_t delta;
 
+  /* A mapping points into tfo_data, so the buffer must not move or be
+   * freed while the file is mapped.
+   */
+
+  if (tfo->tfo_maps > 0)
+    {
+      if (newsize > tfo->tfo_alloc)
+        {
+          return -ENOMEM;
+        }
+
+      memset(tfo->tfo_data + newsize, 0, tfo->tfo_alloc - newsize);
+      tfo->tfo_size = newsize;
+      return OK;
+    }
+
   /* Are we growing or shrinking the object? */
 
   if (newsize <= tfo->tfo_alloc)
@@ -2186,13 +2202,26 @@ static int tmpfs_unmap(FAR struct task_group_s *group,
 
   if (length >= entry->length)
     {
-      /* Then remove the mapping from the list */
+      /* Lock the file first: once the entry is removed, the counts must
+       * be dropped.
+       */
+
+      ret = tmpfs_lock_file(tfo);
+      if (ret < 0)
+        {
+          return ret;
+        }
 
       ret = mm_map_remove(get_group_mm(group), entry);
-      if (ret >= 0)
+      if (ret < 0)
         {
-          ret = tmpfs_release_file(tfo);
+          tmpfs_unlock_file(tfo);
+          return ret;
         }
+
+      DEBUGASSERT(tfo->tfo_maps > 0);
+      tfo->tfo_maps--;
+      tmpfs_release_lockedfile(tfo);
     }
 
   /* No.. We have been asked to "unmap' only a portion of the memory
@@ -2241,7 +2270,7 @@ static int tmpfs_msync(FAR struct mm_map_entry_s *entry, 
FAR void *start,
 static int tmpfs_mmap(FAR struct file *filep, FAR struct mm_map_entry_s *map)
 {
   FAR struct tmpfs_file_s *tfo;
-  int ret = -EINVAL;
+  int ret;
 
   DEBUGASSERT(filep->f_priv != NULL);
 
@@ -2251,29 +2280,60 @@ static int tmpfs_mmap(FAR struct file *filep, FAR 
struct mm_map_entry_s *map)
 
   DEBUGASSERT(tfo != NULL);
 
-  if (map->offset >= 0 && map->offset < tfo->tfo_size &&
-      map->length && map->offset + map->length <= tfo->tfo_size)
+  /* munmap() holds the mm_map lock while tmpfs_unmap() takes the file
+   * lock, so take them in the same order.
+   */
+
+  ret = mm_map_lock();
+  if (ret < 0)
     {
-      map->vaddr = tfo->tfo_data + map->offset;
-      map->priv.p = tfo;
-      map->munmap = tmpfs_unmap;
-      map->msync = tmpfs_msync;
-      ret = mm_map_add(get_current_mm(), map);
+      return ret;
+    }
 
-      if (ret >= 0)
-        {
-          tmpfs_lock_file(tfo);
-          tfo->tfo_refs++;
+  ret = tmpfs_lock_file(tfo);
+  if (ret < 0)
+    {
+      goto errout_with_mmlock;
+    }
 
-          /* POSIX marks st_atime for update on the first access to the
-           * mapping.  tmpfs cannot trap that access, so mark it here.
-           */
+  if (map->offset < 0 || map->offset >= tfo->tfo_size ||
+      map->length == 0 || map->offset + map->length > tfo->tfo_size)
+    {
+      ret = -EINVAL;
+      goto errout_with_lock;
+    }
 
-          tmpfs_update_atime((FAR struct tmpfs_object_s *)tfo);
-          tmpfs_unlock_file(tfo);
-        }
+  /* Each mapping also holds a reference, so tfo_maps <= tfo_refs */
+
+  if (tfo->tfo_refs == UINT8_MAX)
+    {
+      ret = -ENOMEM;
+      goto errout_with_lock;
     }
 
+  map->vaddr = tfo->tfo_data + map->offset;
+  map->priv.p = tfo;
+  map->munmap = tmpfs_unmap;
+  map->msync = tmpfs_msync;
+
+  ret = mm_map_add(get_current_mm(), map);
+  if (ret >= 0)
+    {
+      tfo->tfo_refs++;
+      tfo->tfo_maps++;
+
+      /* POSIX marks st_atime for update on the first access to the
+       * mapping.  tmpfs cannot trap that access, so mark it here.
+       */
+
+      tmpfs_update_atime((FAR struct tmpfs_object_s *)tfo);
+    }
+
+errout_with_lock:
+  tmpfs_unlock_file(tfo);
+
+errout_with_mmlock:
+  mm_map_unlock();
   return ret;
 }
 
diff --git a/fs/tmpfs/fs_tmpfs.h b/fs/tmpfs/fs_tmpfs.h
index 7b1192f7d29..610dbdba402 100644
--- a/fs/tmpfs/fs_tmpfs.h
+++ b/fs/tmpfs/fs_tmpfs.h
@@ -165,6 +165,7 @@ struct tmpfs_file_s
   /* Remaining fields are unique to a directory object */
 
   uint8_t       tfo_flags; /* See TFO_FLAG_* definitions */
+  uint8_t       tfo_maps;  /* Number of mappings of tfo_data */
   size_t        tfo_size;  /* Valid file size */
   FAR uint8_t  *tfo_data;  /* File data starts here */
 };

Reply via email to