You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/11/18 12:47:18 UTC

[GitHub] [incubator-nuttx] pkarashchenko commented on a diff in pull request #7618: mm/shm: Implement a generic utility API for creating / destroying shm mappings

pkarashchenko commented on code in PR #7618:
URL: https://github.com/apache/incubator-nuttx/pull/7618#discussion_r1026383346


##########
mm/mm_gran/mm_granmark.c:
##########
@@ -48,18 +48,20 @@
  *   ngranules - The number of granules allocated
  *
  * Returned Value:
- *   None
+ *   Zero (OK) is returned on success; a negated errno value is returned
+ *   on failure.
  *
  ****************************************************************************/
 
-void gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
+int gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
                          unsigned int ngranules)

Review Comment:
   ```suggestion
   int gran_mark_allocated(FAR struct gran_s *priv, uintptr_t alloc,
                           unsigned int ngranules)
   ```



##########
mm/shm/shm_alloc.c:
##########
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * 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, uintptr_t vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+
+  if (group->tg_shm.gs_handle)
+    {
+      if (!vaddr)

Review Comment:
   Optional
   ```suggestion
         if (vaddr == 0)
   ```



##########
mm/shm/shm_alloc.c:
##########
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * 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, uintptr_t vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+
+  if (group->tg_shm.gs_handle)
+    {
+      if (!vaddr)
+        {
+          ret = gran_alloc(group->tg_shm.gs_handle, size);
+        }
+      else if (gran_reserve(group->tg_shm.gs_handle, vaddr, size) == OK)
+        {
+          ret = (FAR void *)vaddr;
+        }
+    }
+
+  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, uintptr_t vaddr, size_t size)
+{
+  if (group->tg_shm.gs_handle)

Review Comment:
   Optional
   ```suggestion
     if (group->tg_shm.gs_handle != NULL)
   ```



##########
mm/mm_gran/mm_granmark.c:
##########
@@ -75,35 +77,49 @@ 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))
+        {
+          ret = -ENOMEM;
+          goto err_nomem;
+        }
 
-      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;

Review Comment:
   The code can be easily reworked not to use reverse logic so we can avoid usage of goto:
   ```
         if (((priv->gat[gatidx] & gatmask) == 0) &&
             ((priv->gat[gatidx + 1] & gatmask2) == 0))
           {
             priv->gat[gatidx] |= gatmask;
             priv->gat[gatidx + 1] |= gatmask2;
           }
         else
           {
             ret = -ENOMEM;
           }
   ```



##########
mm/mm_gran/mm_pgalloc.c:
##########
@@ -123,7 +123,7 @@ 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);
+  DEBUGASSERT(gran_reserve(g_pgalloc, start, size) == 0);

Review Comment:
   ```suggestion
     DEBUGVERIFY(gran_reserve(g_pgalloc, start, size));
   ```



##########
mm/shm/shm_alloc.c:
##########
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * 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, uintptr_t vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+
+  if (group->tg_shm.gs_handle)

Review Comment:
   Optional
   ```suggestion
     if (group->tg_shm.gs_handle != NULL)
   ```



##########
mm/shm/shm_alloc.c:
##########
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * 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, uintptr_t vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+

Review Comment:
   do we need to sanitize `group` parameter with `DEBUGASSERT(group != NULL);`?



##########
mm/mm_gran/mm_granmark.c:
##########
@@ -75,35 +77,49 @@ 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))
+        {
+          ret = -ENOMEM;
+          goto err_nomem;
+        }
 
-      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)
+        {
+          ret = -ENOMEM;
+          goto err_nomem;
+        }
+
+      /* Mark bits in a single GAT entry */
 
       priv->gat[gatidx] |= gatmask;

Review Comment:
   ditto:
   ```
   if ((priv->gat[gatidx] & gatmask) != 0)
     {
       priv->gat[gatidx] |= gatmask;
     }
   else
     {
       ret = -ENOMEM;
     }
   ```



##########
mm/shm/shm_alloc.c:
##########
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * 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, uintptr_t vaddr,
+                    size_t size)
+{
+  FAR void *ret = NULL;
+
+  if (group->tg_shm.gs_handle)
+    {
+      if (!vaddr)
+        {
+          ret = gran_alloc(group->tg_shm.gs_handle, size);
+        }
+      else if (gran_reserve(group->tg_shm.gs_handle, vaddr, size) == OK)
+        {
+          ret = (FAR void *)vaddr;
+        }
+    }
+
+  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, uintptr_t vaddr, size_t size)

Review Comment:
   do we need to sanitize `group` parameter with `DEBUGASSERT(group != NULL);`?



##########
mm/mm_gran/mm_granmark.c:
##########
@@ -75,35 +77,49 @@ 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))
+        {
+          ret = -ENOMEM;
+          goto err_nomem;
+        }
 
-      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)
+        {
+          ret = -ENOMEM;
+          goto err_nomem;
+        }
+
+      /* Mark bits in a single GAT entry */
 
       priv->gat[gatidx] |= gatmask;
-      return;
     }
+
+err_nomem:

Review Comment:
   may be not needed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org