You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2022/11/22 07:54:40 UTC

[incubator-nuttx] branch master updated (5c1b518314 -> f8a6ec2cdf)

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

pkarashchenko pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


    from 5c1b518314 nuttx/sched: move reprioritize process to public function
     new 458ff380e5 mm/gran: Allow run-time execution of gran_reserve
     new 48b9a8d6f4 mm/shm: Move the vma allocators to separate functions
     new f8a6ec2cdf mm/shm: Fix ARCH_SHM_VEND

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 include/nuttx/addrenv.h                  |  2 +-
 include/nuttx/mm/gran.h                  |  5 ++-
 include/nuttx/mm/shm.h                   | 44 +++++++++++++++++++-
 mm/mm_gran/mm_gran.h                     |  7 ++--
 mm/mm_gran/mm_granmark.c                 | 44 +++++++++++++-------
 mm/mm_gran/mm_granreserve.c              | 18 ++++++--
 mm/mm_gran/mm_pgalloc.c                  |  3 +-
 mm/shm/Make.defs                         |  2 +-
 mm/shm/{shm_initialize.c => shm_alloc.c} | 71 ++++++++++++++------------------
 mm/shm/shmat.c                           | 18 ++++----
 mm/shm/shmdt.c                           |  4 +-
 11 files changed, 137 insertions(+), 81 deletions(-)
 copy mm/shm/{shm_initialize.c => shm_alloc.c} (61%)


[incubator-nuttx] 02/03: mm/shm: Move the vma allocators to separate functions

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 48b9a8d6f43375e9a2d2662dbb9438160c903f1d
Author: Ville Juven <vi...@unikie.com>
AuthorDate: Thu Nov 17 12:03:22 2022 +0200

    mm/shm: Move the vma allocators to separate functions
    
    This will allow changing the underlying backend, i.e. granule allocator,
    to something more efficient, if this is needed later.
    
    Also, it gives easier access to the common SHM book keeping.
---
 include/nuttx/mm/shm.h |  44 +++++++++++++++++++-
 mm/shm/Make.defs       |   2 +-
 mm/shm/shm_alloc.c     | 109 +++++++++++++++++++++++++++++++++++++++++++++++++
 mm/shm/shmat.c         |  18 ++++----
 mm/shm/shmdt.c         |   4 +-
 5 files changed, 161 insertions(+), 16 deletions(-)

diff --git a/include/nuttx/mm/shm.h b/include/nuttx/mm/shm.h
index 664c6e7843..eeb9d14e55 100644
--- a/include/nuttx/mm/shm.h
+++ b/include/nuttx/mm/shm.h
@@ -63,6 +63,12 @@
 #  define shminfo                   minfo
 #endif
 
+/****************************************************************************
+ * Public Type Definitions
+ ****************************************************************************/
+
+struct task_group_s; /* Forward declaration */
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -110,7 +116,6 @@ struct group_shm_s
  *
  ****************************************************************************/
 
-struct task_group_s; /* Forward reference */
 int shm_group_initialize(FAR struct task_group_s *group);
 
 /****************************************************************************
@@ -128,8 +133,43 @@ int shm_group_initialize(FAR struct task_group_s *group);
  *
  ****************************************************************************/
 
-struct task_group_s; /* Forward reference */
 void shm_group_release(FAR struct task_group_s *group);
 
+/****************************************************************************
+ * Name: shm_alloc
+ *
+ * Description:
+ *   Allocate virtual memory region from the shared memory pool.
+ *
+ * Input Parameters:
+ *   group - A reference to the group structure to be un-initialized.
+ *   vaddr - Virtual start address where the allocation starts, if NULL, will
+ *           seek and return an address that satisfies the 'size' parameter
+ *   size - Size of the area to allocate
+ *
+ * Returned Value:
+ *   Pointer to reserved vaddr, or NULL if out-of-memory
+ *
+ ****************************************************************************/
+
+FAR void *shm_alloc(FAR struct task_group_s *group, FAR void *vaddr,
+                    size_t size);
+
+/****************************************************************************
+ * Name: shm_free
+ *
+ * Description:
+ *   Free a previously allocated virtual memory region back to the shared
+ *   memory pool.
+ *
+ * Input Parameters:
+ *   group - A reference to the group structure to be un-initialized.
+ *   vaddr - Virtual start address where the allocation starts.
+ *   size - Size of the allocated area.
+ *
+ ****************************************************************************/
+
+void shm_free(FAR struct task_group_s *group, FAR void *vaddr, size_t size);
+
 #endif /* CONFIG_MM_SHM */
 #endif /* __INCLUDE_NUTTX_MM_SHM_H */
diff --git a/mm/shm/Make.defs b/mm/shm/Make.defs
index dbac590a20..bb93057d05 100644
--- a/mm/shm/Make.defs
+++ b/mm/shm/Make.defs
@@ -21,7 +21,7 @@
 # Shared memory allocator
 
 ifeq ($(CONFIG_MM_SHM),y)
-CSRCS += shm_initialize.c
+CSRCS += shm_initialize.c shm_alloc.c
 CSRCS += shmat.c shmctl.c shmdt.c shmget.c
 
 # Add the shared memory directory to the build
diff --git a/mm/shm/shm_alloc.c b/mm/shm/shm_alloc.c
new file mode 100644
index 0000000000..494c6c64bf
--- /dev/null
+++ b/mm/shm/shm_alloc.c
@@ -0,0 +1,109 @@
+/****************************************************************************
+ * mm/shm/shm_alloc.c
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <nuttx/addrenv.h>
+#include <nuttx/sched.h>
+#include <nuttx/mm/gran.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/mm/shm.h>
+
+#include "shm/shm.h"
+
+#ifdef CONFIG_MM_SHM
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: shm_alloc
+ *
+ * Description:
+ *   Allocate virtual memory region from the shared memory pool.
+ *
+ * Input Parameters:
+ *   group - A reference to the group structure to be un-initialized.
+ *   vaddr - Virtual start address where the allocation starts, if NULL, will
+ *           seek and return an address that satisfies the 'size' parameter
+ *   size - Size of the area to allocate
+ *
+ * Returned Value:
+ *   Pointer to reserved vaddr, or NULL if out-of-memory
+ *
+ ****************************************************************************/
+
+FAR void *shm_alloc(FAR struct task_group_s *group, FAR void *vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+
+  DEBUGASSERT(group != NULL);
+
+  if (group->tg_shm.gs_handle != NULL)
+    {
+      if (vaddr == NULL)
+        {
+          ret = gran_alloc(group->tg_shm.gs_handle, size);
+        }
+      else
+        {
+          ret = gran_reserve(group->tg_shm.gs_handle, (uintptr_t)vaddr,
+                             size);
+        }
+    }
+
+  return ret;
+}
+
+/****************************************************************************
+ * Name: shm_free
+ *
+ * Description:
+ *   Free a previously allocated virtual memory region back to the shared
+ *   memory pool.
+ *
+ * Input Parameters:
+ *   group - A reference to the group structure to be un-initialized.
+ *   vaddr - Virtual start address where the allocation starts.
+ *   size - Size of the allocated area.
+ *
+ ****************************************************************************/
+
+void shm_free(FAR struct task_group_s *group, FAR void *vaddr, size_t size)
+{
+  DEBUGASSERT(group != NULL);
+
+  if (group->tg_shm.gs_handle != NULL)
+    {
+      gran_free(group->tg_shm.gs_handle, vaddr, size);
+    }
+}
+
+#endif /* CONFIG_MM_SHM */
diff --git a/mm/shm/shmat.c b/mm/shm/shmat.c
index 2433f3a420..590f68cdd3 100644
--- a/mm/shm/shmat.c
+++ b/mm/shm/shmat.c
@@ -100,7 +100,7 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
   FAR struct shm_region_s *region;
   FAR struct task_group_s *group;
   FAR struct tcb_s *tcb;
-  uintptr_t vaddr;
+  FAR void *vaddr;
   unsigned int npages;
   int ret;
 
@@ -129,11 +129,10 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
 
   /* Set aside a virtual address space to span this physical region */
 
-  vaddr = (uintptr_t)gran_alloc(group->tg_shm.gs_handle,
-                                region->sr_ds.shm_segsz);
-  if (vaddr == 0)
+  vaddr = shm_alloc(group, NULL, region->sr_ds.shm_segsz);
+  if (vaddr == NULL)
     {
-      shmerr("ERROR: gran_alloc() failed\n");
+      shmerr("ERROR: shm_alloc() failed\n");
       ret = -ENOMEM;
       goto errout_with_lock;
     }
@@ -144,7 +143,7 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
 
   /* Attach, i.e, map, on shared memory region to the user virtual address. */
 
-  ret = up_shmat(region->sr_pages, npages, vaddr);
+  ret = up_shmat(region->sr_pages, npages, (uintptr_t)vaddr);
   if (ret < 0)
     {
       shmerr("ERROR: up_shmat() failed\n");
@@ -156,7 +155,7 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
    * detach, we need to get the region table index.
    */
 
-  group->tg_shm.gs_vaddr[shmid] = vaddr;
+  group->tg_shm.gs_vaddr[shmid] = (uintptr_t)vaddr;
 
   /* Increment the count of processes attached to this region */
 
@@ -173,11 +172,10 @@ FAR void *shmat(int shmid, FAR const void *shmaddr, int shmflg)
   /* Release our lock on the entry */
 
   nxmutex_unlock(&region->sr_lock);
-  return (FAR void *)vaddr;
+  return vaddr;
 
 errout_with_vaddr:
-  gran_free(group->tg_shm.gs_handle, (FAR void *)vaddr,
-            region->sr_ds.shm_segsz);
+  shm_free(group, vaddr, region->sr_ds.shm_segsz);
 
 errout_with_lock:
   nxmutex_unlock(&region->sr_lock);
diff --git a/mm/shm/shmdt.c b/mm/shm/shmdt.c
index d1f02a80f5..1f24f2e55f 100644
--- a/mm/shm/shmdt.c
+++ b/mm/shm/shmdt.c
@@ -81,7 +81,6 @@ int shmdt(FAR const void *shmaddr)
   tcb = nxsched_self();
   DEBUGASSERT(tcb && tcb->group);
   group = tcb->group;
-  DEBUGASSERT(group->tg_shm.gs_handle != NULL);
 
   /* Perform the reverse lookup to get the shmid corresponding to this
    * shmaddr.
@@ -116,8 +115,7 @@ int shmdt(FAR const void *shmaddr)
 
   /* Free the virtual address space */
 
-  gran_free(group->tg_shm.gs_handle, (FAR void *)shmaddr,
-            region->sr_ds.shm_segsz);
+  shm_free(group, (FAR void *)shmaddr, region->sr_ds.shm_segsz);
 
   /* Convert the region size to pages */
 


[incubator-nuttx] 03/03: mm/shm: Fix ARCH_SHM_VEND

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit f8a6ec2cdfb4665d848ac6f9ca2912ef0135f9ac
Author: Ville Juven <vi...@unikie.com>
AuthorDate: Fri Nov 18 13:34:28 2022 +0200

    mm/shm: Fix ARCH_SHM_VEND
    
    The last address is base+size-1
---
 include/nuttx/addrenv.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/nuttx/addrenv.h b/include/nuttx/addrenv.h
index 1e5e2d4fe9..43a0ea92ce 100644
--- a/include/nuttx/addrenv.h
+++ b/include/nuttx/addrenv.h
@@ -187,7 +187,7 @@
 #  define ARCH_SHM_MAXPAGES   (CONFIG_ARCH_SHM_NPAGES * CONFIG_ARCH_SHM_MAXREGIONS)
 #  define ARCH_SHM_REGIONSIZE (CONFIG_ARCH_SHM_NPAGES * CONFIG_MM_PGSIZE)
 #  define ARCH_SHM_SIZE       (CONFIG_ARCH_SHM_MAXREGIONS * ARCH_SHM_REGIONSIZE)
-#  define ARCH_SHM_VEND       (CONFIG_ARCH_SHM_VBASE + ARCH_SHM_SIZE)
+#  define ARCH_SHM_VEND       (CONFIG_ARCH_SHM_VBASE + ARCH_SHM_SIZE - 1)
 
 #  define ARCH_SCRATCH_VBASE   ARCH_SHM_VEND
 #else


[incubator-nuttx] 01/03: mm/gran: Allow run-time execution of gran_reserve

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 458ff380e5fd7578db5faf74c82d4dccceb8a8c4
Author: Ville Juven <vi...@unikie.com>
AuthorDate: Thu Nov 17 13:39:17 2022 +0200

    mm/gran: Allow run-time execution of gran_reserve
    
    User can ask for specific granules to be allocated.
    
    This is useful for one thing only: when mmap() is called for a specific
    vaddr. The parameter itself is non-sensical, this is just to satisfy
    the POSIX standard.
---
 include/nuttx/mm/gran.h     |  5 +++--
 mm/mm_gran/mm_gran.h        |  7 ++++---
 mm/mm_gran/mm_granmark.c    | 44 ++++++++++++++++++++++++++++----------------
 mm/mm_gran/mm_granreserve.c | 18 +++++++++++++++---
 mm/mm_gran/mm_pgalloc.c     |  3 ++-
 5 files changed, 52 insertions(+), 25 deletions(-)

diff --git a/include/nuttx/mm/gran.h b/include/nuttx/mm/gran.h
index d515a1c3ca..30940efb84 100644
--- a/include/nuttx/mm/gran.h
+++ b/include/nuttx/mm/gran.h
@@ -170,11 +170,12 @@ void gran_release(GRAN_HANDLE handle);
  *   size   - The size of the region to be reserved
  *
  * Returned Value:
- *   None
+ *   On success, a non-NULL pointer to the allocated memory is returned;
+ *   NULL is returned on failure.
  *
  ****************************************************************************/
 
-void gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size);
+FAR void *gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size);
 
 /****************************************************************************
  * Name: gran_alloc
diff --git a/mm/mm_gran/mm_gran.h b/mm/mm_gran/mm_gran.h
index b9fe04beba..3ef67916d3 100644
--- a/mm/mm_gran/mm_gran.h
+++ b/mm/mm_gran/mm_gran.h
@@ -109,11 +109,12 @@ void gran_leave_critical(FAR struct gran_s *priv);
  *   ngranules - The number of granules allocated
  *
  * Returned Value:
- *   None
+ *   On success, a non-NULL pointer to the allocated memory is returned;
+ *   NULL is returned on failure.
  *
  ****************************************************************************/
 
-void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
-                         unsigned int ngranules);
+FAR void *gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
+                              unsigned int ngranules);
 
 #endif /* __MM_MM_GRAN_MM_GRAN_H */
diff --git a/mm/mm_gran/mm_granmark.c b/mm/mm_gran/mm_granmark.c
index 05d60f9ae0..634778b414 100644
--- a/mm/mm_gran/mm_granmark.c
+++ b/mm/mm_gran/mm_granmark.c
@@ -48,12 +48,13 @@
  *   ngranules - The number of granules allocated
  *
  * Returned Value:
- *   None
+ *   On success, a non-NULL pointer to the allocated memory is returned;
+ *   NULL is returned on failure.
  *
  ****************************************************************************/
 
-void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
-                         unsigned int ngranules)
+FAR void *gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
+                              unsigned int ngranules)
 {
   unsigned int granno;
   unsigned int gatidx;
@@ -75,35 +76,46 @@ void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
   avail = 32 - gatbit;
   if (ngranules > avail)
     {
-      /* Mark bits in the first GAT entry */
+      uint32_t gatmask2;
 
-      gatmask = 0xffffffff << gatbit;
-      DEBUGASSERT((priv->gat[gatidx] & gatmask) == 0);
-
-      priv->gat[gatidx] |= gatmask;
+      gatmask    = 0xffffffff << gatbit;
       ngranules -= avail;
+      gatmask2   = 0xffffffff >> (32 - ngranules);
+
+      /* Check that the area is free, from both mask words */
 
-      /* Mark bits in the second GAT entry */
+      if (((priv->gat[gatidx] & gatmask) != 0) ||
+          ((priv->gat[gatidx + 1] & gatmask2) != 0))
+        {
+          return NULL;
+        }
 
-      gatmask = 0xffffffff >> (32 - ngranules);
-      DEBUGASSERT((priv->gat[gatidx + 1] & gatmask) == 0);
+      /* Mark bits in the first and second GAT entry */
 
-      priv->gat[gatidx + 1] |= gatmask;
+      priv->gat[gatidx] |= gatmask;
+      priv->gat[gatidx + 1] |= gatmask2;
     }
 
   /* Handle the case where where all of the granules come from one entry */
 
   else
     {
-      /* Mark bits in a single GAT entry */
-
       gatmask   = 0xffffffff >> (32 - ngranules);
       gatmask <<= gatbit;
-      DEBUGASSERT((priv->gat[gatidx] & gatmask) == 0);
+
+      /* Check that the area is free */
+
+      if ((priv->gat[gatidx] & gatmask) != 0)
+        {
+          return NULL;
+        }
+
+      /* Mark bits in a single GAT entry */
 
       priv->gat[gatidx] |= gatmask;
-      return;
     }
+
+  return (FAR void *)alloc;
 }
 
 #endif /* CONFIG_GRAN */
diff --git a/mm/mm_gran/mm_granreserve.c b/mm/mm_gran/mm_granreserve.c
index cbb3dad3e3..4930b02d08 100644
--- a/mm/mm_gran/mm_granreserve.c
+++ b/mm/mm_gran/mm_granreserve.c
@@ -54,13 +54,15 @@
  *   size   - The size of the region to be reserved
  *
  * Returned Value:
- *   None
+ *   On success, a non-NULL pointer to the allocated memory is returned;
+ *   NULL is returned on failure.
  *
  ****************************************************************************/
 
-void gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size)
+FAR void *gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size)
 {
   FAR struct gran_s *priv = (FAR struct gran_s *)handle;
+  FAR void *ret = NULL;
 
   DEBUGASSERT(priv != NULL);
 
@@ -81,10 +83,20 @@ void gran_reserve(GRAN_HANDLE handle, uintptr_t start, size_t size)
 
       ngranules = ((end - start) >> priv->log2gran) + 1;
 
+      /* Must lock the granule allocator */
+
+      if (gran_enter_critical(priv) < 0)
+        {
+          return NULL;
+        }
+
       /* And reserve the granules */
 
-      gran_mark_allocated(priv, start, ngranules);
+      ret = gran_mark_allocated(priv, start, ngranules);
+      gran_leave_critical(priv);
     }
+
+  return ret;
 }
 
 #endif /* CONFIG_GRAN */
diff --git a/mm/mm_gran/mm_pgalloc.c b/mm/mm_gran/mm_pgalloc.c
index 915e2419e4..369b057de9 100644
--- a/mm/mm_gran/mm_pgalloc.c
+++ b/mm/mm_gran/mm_pgalloc.c
@@ -123,7 +123,8 @@ void mm_pginitialize(FAR void *heap_start, size_t heap_size)
 
 void mm_pgreserve(uintptr_t start, size_t size)
 {
-  gran_reserve(g_pgalloc, start, size);
+  FAR void * ret = gran_reserve(g_pgalloc, start, size);
+  DEBUGASSERT(ret != NULL);
 }
 
 /****************************************************************************