xiaoxiang781216 commented on code in PR #8026: URL: https://github.com/apache/nuttx/pull/8026#discussion_r1065281454
########## mm/map/mm_map.c: ########## @@ -0,0 +1,315 @@ +/**************************************************************************** + * mm/map/mm_map.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/mm/map.h> +#include <stdbool.h> +#include <stddef.h> +#include <nuttx/sched.h> +#include <nuttx/kmalloc.h> +#include <assert.h> +#include <debug.h> + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool in_range(FAR const void *start, size_t length, + FAR const void *range_start, size_t range_length) +{ + FAR char *u_start = (FAR char *)start; Review Comment: cast to const char * ########## mm/map/mm_map.c: ########## @@ -0,0 +1,315 @@ +/**************************************************************************** + * mm/map/mm_map.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/mm/map.h> +#include <stdbool.h> +#include <stddef.h> +#include <nuttx/sched.h> +#include <nuttx/kmalloc.h> +#include <assert.h> +#include <debug.h> + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool in_range(FAR const void *start, size_t length, + FAR const void *range_start, size_t range_length) +{ + FAR char *u_start = (FAR char *)start; + FAR char *u_end = u_start + length; + FAR char *r_start = (FAR char *)range_start; + FAR char *r_end = r_start + range_length; + + return (u_start >= r_start && u_start < r_end && /* start is in range */ + u_end >= r_start && u_end <= r_end); /* end is in range */ +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mm_map_lock + * + * Description: + * Get exclusive access to task_group's mm_map + * + ****************************************************************************/ + +int mm_map_lock(void) +{ + return nxrmutex_lock(&get_current_mm()->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_unlock + * + * Description: + * Relinquish exclusive access to task_group's mm_map + * + ****************************************************************************/ + +void mm_map_unlock(void) +{ + DEBUGVERIFY(nxrmutex_unlock(&get_current_mm()->mm_map_mutex)); +} + +/**************************************************************************** + * Name: mm_map_initialize + * + * Description: + * Allocates a task group specific mm_map stucture. Called when the group + * is initialized + * + ****************************************************************************/ + +void mm_map_initialize(FAR struct mm_map_s *mm) +{ + sq_init(&mm->mm_map_sq); + nxrmutex_init(&mm->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_destroy + * + * Description: + * De-allocates a task group specific mm_map stucture and the mm_map_mutex + * + ****************************************************************************/ + +void mm_map_destroy(FAR struct mm_map_s *mm) +{ + FAR struct mm_map_entry_s *entry; + + while ((entry = (FAR struct mm_map_entry_s *)sq_remfirst(&mm->mm_map_sq))) + { + /* Pass null as group argument to indicate that actual MMU mappings + * must not be touched. The process is being deleted and we don't + * know in which context we are. Only kernel memory allocations + * need to be freed by drivers + */ + + /* Unmap the whole region */ + + if (entry->munmap) + { + if (entry->munmap(NULL, entry, entry->vaddr, entry->length) < 0) + { + /* This would be an error in the driver. It has defined munmap, + * but is not able to munmap the full area which it has mapped + */ + + merr("Driver munmap failed\n"); + } + } + + kmm_free(entry); + } + + nxrmutex_destroy(&mm->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_add + * + * Description: + * Add a mapping to task group's mm_map list + * + ****************************************************************************/ + +int mm_map_add(FAR struct mm_map_entry_s *entry) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *new_entry; + int ret; + + if (!entry) + { + return -EINVAL; + } + + /* Copy the provided mapping and add to the list */ + + new_entry = kmm_malloc(sizeof(struct mm_map_entry_s)); + if (!new_entry) + { + return -EINVAL; + } + + *new_entry = *entry; + + ret = nxrmutex_lock(&mm->mm_map_mutex); + if (ret < 0) + { + kmm_free(new_entry); + return ret; + } + + sq_addfirst((sq_entry_t *)new_entry, &mm->mm_map_sq); + + nxrmutex_unlock(&mm->mm_map_mutex); + + return OK; +} + +/**************************************************************************** + * Name: mm_map_next + * + * Description: + * Returns the next mapping in the list. + * + ****************************************************************************/ + +FAR struct mm_map_entry_s *mm_map_next( + FAR const struct mm_map_entry_s *entry) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *next_entry = NULL; + + if (nxrmutex_lock(&mm->mm_map_mutex) == OK) + { + if (entry == NULL) + { + next_entry = (struct mm_map_entry_s *)sq_peek(&mm->mm_map_sq); + } + else + { + next_entry = (struct mm_map_entry_s *) + sq_next(((sq_entry_t *)entry)); + } + + nxrmutex_unlock(&mm->mm_map_mutex); + } + + return next_entry; +} + +/**************************************************************************** + * Name: mm_map_find + * + * Description: + * Find the first mapping containing the range from the task group's list + * + ****************************************************************************/ + +FAR struct mm_map_entry_s *mm_map_find(FAR const void *vaddr, size_t length) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *found_entry = NULL; + + if (nxrmutex_lock(&mm->mm_map_mutex) == OK) + { + found_entry = (struct mm_map_entry_s *)sq_peek(&mm->mm_map_sq); + + while (found_entry && !in_range(vaddr, length, + found_entry->vaddr, + found_entry->length)) + { + found_entry = (struct mm_map_entry_s *) + sq_next(((sq_entry_t *)found_entry)); + } + + nxrmutex_unlock(&mm->mm_map_mutex); + } + + return found_entry; +} + +/**************************************************************************** + * Name: mm_map_remove + * + * Description: + * Remove a mapping from the task group's list + * + ****************************************************************************/ + +int mm_map_remove(FAR struct mm_map_s *mm, + FAR struct mm_map_entry_s **entry) +{ + FAR struct mm_map_entry_s *prev_entry; + FAR struct mm_map_entry_s *removed_entry = NULL; + + if (!mm || !entry) + { + return OK; + } + + int ret = nxrmutex_lock(&mm->mm_map_mutex); Review Comment: move the declaration of ret to the beginning. ########## fs/mmap/fs_rammap.c: ########## @@ -81,15 +149,14 @@ struct fs_allmaps_s g_rammaps = * ****************************************************************************/ -int rammap(FAR struct file *filep, size_t length, - off_t offset, bool kernel, FAR void **mapped) +int rammap(FAR struct file *filep, FAR struct mm_map_entry_s *entry, + bool kernel) { - FAR struct fs_rammap_s *map; - FAR uint8_t *alloc; FAR uint8_t *rdbuffer; ssize_t nread; off_t fpos; int ret; + size_t length = entry->length; Review Comment: remove? let use entry->length directly like entry->offset ########## fs/mmap/Kconfig: ########## @@ -25,3 +25,9 @@ config FS_RAMMAP if FS_RAMMAP Review Comment: remove the empty if/endif ########## include/nuttx/sched.h: ########## @@ -180,6 +181,14 @@ # define TCB_REG_OFF(reg) (reg * sizeof(uint32_t)) #endif +/* Get a pointer to the process' memory map struct from the task_group */ + +#define group_get_mm(group) (group ? &group->tg_mm_map : NULL) Review Comment: group_get_mm->get_group_mm ########## fs/mmap/fs_munmap.c: ########## @@ -158,7 +159,40 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) nxmutex_unlock(&g_rammaps.lock); return ret; #else - return OK; + + FAR struct tcb_s *tcb = nxsched_self(); + FAR struct task_group_s *group = tcb->group; + FAR struct mm_map_entry_s *entry = NULL; + int ret = OK; + + /* Iterate through all the mappings and call the underlying + * unmap for every mapping where "start" lies + * break loop on any errors. + * + * Get exclusive access to mm_map for this + */ + + ret = mm_map_lock(); + if (ret == OK) + { + while (ret == OK && (entry = mm_map_find(start, length))) + { + if (entry->munmap) Review Comment: let's debug assert, it's programming error not runtime error if entry->munmap is NULL ########## mm/map/mm_map.c: ########## @@ -0,0 +1,315 @@ +/**************************************************************************** + * mm/map/mm_map.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/mm/map.h> +#include <stdbool.h> +#include <stddef.h> +#include <nuttx/sched.h> +#include <nuttx/kmalloc.h> +#include <assert.h> +#include <debug.h> + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool in_range(FAR const void *start, size_t length, + FAR const void *range_start, size_t range_length) +{ + FAR char *u_start = (FAR char *)start; + FAR char *u_end = u_start + length; + FAR char *r_start = (FAR char *)range_start; + FAR char *r_end = r_start + range_length; + + return (u_start >= r_start && u_start < r_end && /* start is in range */ + u_end >= r_start && u_end <= r_end); /* end is in range */ +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: mm_map_lock + * + * Description: + * Get exclusive access to task_group's mm_map + * + ****************************************************************************/ + +int mm_map_lock(void) +{ + return nxrmutex_lock(&get_current_mm()->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_unlock + * + * Description: + * Relinquish exclusive access to task_group's mm_map + * + ****************************************************************************/ + +void mm_map_unlock(void) +{ + DEBUGVERIFY(nxrmutex_unlock(&get_current_mm()->mm_map_mutex)); +} + +/**************************************************************************** + * Name: mm_map_initialize + * + * Description: + * Allocates a task group specific mm_map stucture. Called when the group + * is initialized + * + ****************************************************************************/ + +void mm_map_initialize(FAR struct mm_map_s *mm) +{ + sq_init(&mm->mm_map_sq); + nxrmutex_init(&mm->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_destroy + * + * Description: + * De-allocates a task group specific mm_map stucture and the mm_map_mutex + * + ****************************************************************************/ + +void mm_map_destroy(FAR struct mm_map_s *mm) +{ + FAR struct mm_map_entry_s *entry; + + while ((entry = (FAR struct mm_map_entry_s *)sq_remfirst(&mm->mm_map_sq))) + { + /* Pass null as group argument to indicate that actual MMU mappings + * must not be touched. The process is being deleted and we don't + * know in which context we are. Only kernel memory allocations + * need to be freed by drivers + */ + + /* Unmap the whole region */ + + if (entry->munmap) + { + if (entry->munmap(NULL, entry, entry->vaddr, entry->length) < 0) + { + /* This would be an error in the driver. It has defined munmap, + * but is not able to munmap the full area which it has mapped + */ + + merr("Driver munmap failed\n"); + } + } + + kmm_free(entry); + } + + nxrmutex_destroy(&mm->mm_map_mutex); +} + +/**************************************************************************** + * Name: mm_map_add + * + * Description: + * Add a mapping to task group's mm_map list + * + ****************************************************************************/ + +int mm_map_add(FAR struct mm_map_entry_s *entry) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *new_entry; + int ret; + + if (!entry) + { + return -EINVAL; + } + + /* Copy the provided mapping and add to the list */ + + new_entry = kmm_malloc(sizeof(struct mm_map_entry_s)); + if (!new_entry) + { + return -EINVAL; + } + + *new_entry = *entry; + + ret = nxrmutex_lock(&mm->mm_map_mutex); + if (ret < 0) + { + kmm_free(new_entry); + return ret; + } + + sq_addfirst((sq_entry_t *)new_entry, &mm->mm_map_sq); + + nxrmutex_unlock(&mm->mm_map_mutex); + + return OK; +} + +/**************************************************************************** + * Name: mm_map_next + * + * Description: + * Returns the next mapping in the list. + * + ****************************************************************************/ + +FAR struct mm_map_entry_s *mm_map_next( + FAR const struct mm_map_entry_s *entry) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *next_entry = NULL; + + if (nxrmutex_lock(&mm->mm_map_mutex) == OK) + { + if (entry == NULL) + { + next_entry = (struct mm_map_entry_s *)sq_peek(&mm->mm_map_sq); + } + else + { + next_entry = (struct mm_map_entry_s *) + sq_next(((sq_entry_t *)entry)); + } + + nxrmutex_unlock(&mm->mm_map_mutex); + } + + return next_entry; +} + +/**************************************************************************** + * Name: mm_map_find + * + * Description: + * Find the first mapping containing the range from the task group's list + * + ****************************************************************************/ + +FAR struct mm_map_entry_s *mm_map_find(FAR const void *vaddr, size_t length) +{ + FAR struct mm_map_s *mm = get_current_mm(); + FAR struct mm_map_entry_s *found_entry = NULL; + + if (nxrmutex_lock(&mm->mm_map_mutex) == OK) + { + found_entry = (struct mm_map_entry_s *)sq_peek(&mm->mm_map_sq); + + while (found_entry && !in_range(vaddr, length, + found_entry->vaddr, + found_entry->length)) + { + found_entry = (struct mm_map_entry_s *) + sq_next(((sq_entry_t *)found_entry)); + } + + nxrmutex_unlock(&mm->mm_map_mutex); + } + + return found_entry; +} + +/**************************************************************************** + * Name: mm_map_remove + * + * Description: + * Remove a mapping from the task group's list + * + ****************************************************************************/ + +int mm_map_remove(FAR struct mm_map_s *mm, + FAR struct mm_map_entry_s **entry) +{ + FAR struct mm_map_entry_s *prev_entry; + FAR struct mm_map_entry_s *removed_entry = NULL; + + if (!mm || !entry) + { + return OK; + } + + int ret = nxrmutex_lock(&mm->mm_map_mutex); + if (ret < 0) + { + return ret; + } + + prev_entry = (struct mm_map_entry_s *)sq_peek(&mm->mm_map_sq); + + /* Check if the list was empty */ + + if (!prev_entry) + { + nxrmutex_unlock(&mm->mm_map_mutex); + return -ENOENT; + } + + /* Check if removing the first item */ + + if (*entry == prev_entry) + { + sq_remfirst(&mm->mm_map_sq); + removed_entry = prev_entry; + } + else + { + /* Loop through the remaining items to find the one to be removed */ + + while ((removed_entry = (struct mm_map_entry_s *) + sq_next(((sq_entry_t *)prev_entry)))) + { + if (*entry == removed_entry) + { + sq_remafter((sq_entry_t *)prev_entry, &mm->mm_map_sq); + break; + } + + prev_entry = removed_entry; + } + } + + nxrmutex_unlock(&mm->mm_map_mutex); + + /* If the item was removed, also delete the entry struct */ + + if (removed_entry) + { + kmm_free(removed_entry); + *entry = NULL; Review Comment: why not change the declaration to: ``` int mm_map_remove(FAR struct mm_map_s *mm, FAR struct mm_map_entry_s *entry) ``` ########## fs/mmap/fs_rammap.h: ########## @@ -53,29 +35,28 @@ * - There are not access privileges. */ -struct fs_rammap_s -{ - struct fs_rammap_s *flink; /* Implements a singly linked list */ - FAR void *addr; /* Start of allocated memory */ - size_t length; /* Length of region */ - off_t offset; /* File offset */ -}; +#ifndef __FS_MMAP_FS_RAMMAP_H +#define __FS_MMAP_FS_RAMMAP_H -/* This structure defines all "mapped" files */ +/**************************************************************************** + * Included Files + ****************************************************************************/ -struct fs_allmaps_s -{ - mutex_t lock; /* Provides exclusive access the list */ - struct fs_rammap_s *head; /* List of mapped files */ -}; +#include <nuttx/config.h> + +#include <sys/types.h> +#include <nuttx/mutex.h> Review Comment: remove mutex.h ########## fs/mmap/fs_munmap.c: ########## @@ -158,7 +159,40 @@ static int file_munmap_(FAR void *start, size_t length, bool kernel) nxmutex_unlock(&g_rammaps.lock); return ret; #else - return OK; + + FAR struct tcb_s *tcb = nxsched_self(); + FAR struct task_group_s *group = tcb->group; + FAR struct mm_map_entry_s *entry = NULL; + int ret = OK; + + /* Iterate through all the mappings and call the underlying + * unmap for every mapping where "start" lies + * break loop on any errors. + * + * Get exclusive access to mm_map for this + */ + + ret = mm_map_lock(); + if (ret == OK) Review Comment: remove if, already checked by while ########## fs/mmap/fs_rammap.c: ########## @@ -44,12 +41,83 @@ * Public Data ****************************************************************************/ -/* This is the list of all mapped files */ +/**************************************************************************** + * Private Functions + ****************************************************************************/ -struct fs_allmaps_s g_rammaps = +static int unmap_rammap(FAR struct task_group_s *group, + FAR struct mm_map_entry_s *entry, + FAR void *start, + size_t length) { - NXMUTEX_INITIALIZER -}; + FAR void *newaddr; + unsigned int offset; + bool kernel = entry->priv.i != 0 ? true : false; + int ret; + + /* Get the offset from the beginning of the region and the actual number + * of bytes to "unmap". All mappings must extend to the end of the region. + * There is no support for freeing a block of memory but leaving a block of + * memory at the end. This is a consequence of using kumm_realloc() to + * simulate the unmapping. + */ + + offset = start - entry->vaddr; + if (offset + length < entry->length) + { + ferr("ERROR: Cannot umap without unmapping to the end\n"); + return -ENOSYS; + } + + /* Okay.. the region is being unmapped to the end. Make sure the length + * indicates that. + */ + + length = entry->length - offset; + + /* Are we unmapping the entire region (offset == 0)? */ + + if (length >= entry->length) + { + /* Free the region */ + + if (kernel) + { + kmm_free(entry->vaddr); + } + else + { + kumm_free(entry->vaddr); + } + + /* Then remove the mapping from the list */ + + ret = mm_map_remove(group_get_mm(group), &entry); Review Comment: should we let mm_map_remove return void? it's very hard to do anything if it returns fail here. ########## mm/map/mm_map.c: ########## @@ -0,0 +1,315 @@ +/**************************************************************************** + * mm/map/mm_map.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/mm/map.h> +#include <stdbool.h> +#include <stddef.h> +#include <nuttx/sched.h> +#include <nuttx/kmalloc.h> +#include <assert.h> +#include <debug.h> + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static bool in_range(FAR const void *start, size_t length, + FAR const void *range_start, size_t range_length) +{ + FAR char *u_start = (FAR char *)start; + FAR char *u_end = u_start + length; + FAR char *r_start = (FAR char *)range_start; + FAR char *r_end = r_start + range_length; + + return (u_start >= r_start && u_start < r_end && /* start is in range */ Review Comment: start/end -> Start/End ########## mm/shm/shmat.c: ########## @@ -32,11 +32,39 @@ #include <nuttx/sched.h> #include <nuttx/arch.h> #include <nuttx/pgalloc.h> +#include <nuttx/mm/map.h> #include "shm/shm.h" #ifdef CONFIG_MM_SHM +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +int shmdt_priv(FAR struct task_group_s *group, FAR const void *shmaddr, Review Comment: move to mm/shm/shm.h ########## include/nuttx/sched.h: ########## @@ -180,6 +181,14 @@ # define TCB_REG_OFF(reg) (reg * sizeof(uint32_t)) #endif +/* Get a pointer to the process' memory map struct from the task_group */ + +#define group_get_mm(group) (group ? &group->tg_mm_map : NULL) + +/* Get a pointer to current the process' memory map struct */ + +#define get_current_mm() (group_get_mm(nxsched_self()->group)) Review Comment: the change need merge to the second patch to avoid the build break. ########## mm/shm/shmdt.c: ########## @@ -33,32 +33,34 @@ #include <nuttx/sched.h> #include <nuttx/mm/shm.h> #include <nuttx/pgalloc.h> +#include <nuttx/mm/map.h> #include "shm/shm.h" #ifdef CONFIG_MM_SHM /**************************************************************************** - * Public Functions + * Private Functions Review Comment: shmdt_priv isn't static function -- 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-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org