From fcd4a421e341d3f6fa3b9fb48acc3d52faf37805 Mon Sep 17 00:00:00 2001
From: Xiang Xiao <xiaoxiang@xiaomi.com>
Date: Wed, 11 Jan 2023 00:51:35 +0800
Subject: [PATCH] fs/mmap: Support the no backing file for anonymous mapping

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
---
 fs/mmap/fs_anonmap.h |  2 +-
 fs/mmap/fs_mmap.c    | 33 +++++++++++++++++++--------------
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/fs/mmap/fs_anonmap.h b/fs/mmap/fs_anonmap.h
index 70725c5623..1413d53bd5 100644
--- a/fs/mmap/fs_anonmap.h
+++ b/fs/mmap/fs_anonmap.h
@@ -55,7 +55,7 @@
 #ifdef CONFIG_FS_ANONMAP
 int map_anonymous(FAR struct mm_map_entry_s *entry, bool kernel);
 #else
-#define map_anonymous(entry,kernel) (-ENOSYS)
+#  define map_anonymous(entry, kernel) (-ENOSYS)
 #endif /* CONFIG_FS_ANONMAP */
 
 #endif /* __FS_MMAP_FS_ANONMAP_H */
diff --git a/fs/mmap/fs_mmap.c b/fs/mmap/fs_mmap.c
index a533602f76..8d7a3f6d2a 100644
--- a/fs/mmap/fs_mmap.c
+++ b/fs/mmap/fs_mmap.c
@@ -94,6 +94,22 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
     }
 #endif /* CONFIG_DEBUG_FEATURES */
 
+  /* Check if we are just be asked to allocate memory, i.e., MAP_ANONYMOUS
+   * set meaning that the memory is not backed up from a file.  The file
+   * descriptor should be -1 (or refer to opened /dev/zero) in this case.
+   * The file descriptor is ignored in either case.
+   */
+
+  if ((flags & MAP_ANONYMOUS) != 0)
+    {
+      return map_anonymous(&entry, kernel);
+    }
+
+  if (filep == NULL)
+    {
+      return -EBADF;
+    }
+
   if ((filep->f_oflags & O_WROK) == 0 && prot == PROT_WRITE)
     {
       ferr("ERROR: Unsupported options for read-only file descriptor,"
@@ -107,17 +123,6 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
       return -EACCES;
     }
 
-  /* Check if we are just be asked to allocate memory, i.e., MAP_ANONYMOUS
-   * set meaning that the memory is not backed up from a file.  The file
-   * descriptor should be -1 (or refer to opened /dev/zero) in this case.
-   * The file descriptor is ignored in either case.
-   */
-
-  if ((flags & MAP_ANONYMOUS) != 0)
-    {
-      return map_anonymous(&entry, kernel);
-    }
-
   /* Call driver's mmap to get the base address of the file in 'mapped'
    * in memory.
    */
@@ -218,7 +223,7 @@ int file_mmap(FAR struct file *filep, FAR void *start, size_t length,
  *           MAP_NORESERVE  - Ignored
  *           MAP_POPULATE   - Ignored
  *           MAP_NONBLOCK   - Ignored
- *   fd      file descriptor of the backing file -- required.
+ *   fd      file descriptor of the backing file.
  *   offset  The offset into the file to map
  *
  * Returned Value:
@@ -247,11 +252,11 @@ int file_mmap(FAR struct file *filep, FAR void *start, size_t length,
 FAR void *mmap(FAR void *start, size_t length, int prot, int flags,
                int fd, off_t offset)
 {
-  FAR struct file *filep;
+  FAR struct file *filep = NULL;
   FAR void *mapped;
   int ret;
 
-  if (fs_getfilep(fd, &filep) < 0)
+  if (fd != -1 && fs_getfilep(fd, &filep) < 0)
     {
       ferr("ERROR: Invalid file descriptor, fd=%d\n", fd);
       ret = -EBADF;