From 5bbcb5dc799cddc732c1cdc966d8d206e9120986 Mon Sep 17 00:00:00 2001 From: geniusgogo <2041245+geniusgogo@users.noreply.github.com> Date: Wed, 12 Apr 2023 13:12:27 +0800 Subject: [PATCH] fix tmpfs spinlock error. (#7216) --- .../dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c | 46 +++++++++---------- .../dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.h | 1 + include/rthw.h | 7 ++- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c index f8a4a8bc58..d22eb0e016 100644 --- a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c +++ b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.c @@ -84,8 +84,7 @@ static int _free_subdir(struct tmpfs_file *dfile) { struct tmpfs_file *file; rt_list_t *list, *temp_list; - - RT_DEFINE_SPINLOCK(lock); + struct tmpfs_sb *superblock; RT_ASSERT(dfile->type == TMPFS_TYPE_DIR); @@ -101,9 +100,14 @@ static int _free_subdir(struct tmpfs_file *dfile) /* TODO: fix for rt-smart */ rt_free(file->data); } - rt_hw_spin_lock(&lock); + + superblock = file->sb; + RT_ASSERT(superblock != NULL); + + rt_spin_lock(&superblock->lock); rt_list_remove(&(file->sibling)); - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); + rt_free(file); } return 0; @@ -126,6 +130,8 @@ int dfs_tmpfs_mount(struct dfs_filesystem *fs, rt_list_init(&superblock->root.sibling); rt_list_init(&superblock->root.subdirs); + rt_spin_lock_init(&superblock->lock); + fs->data = superblock; return RT_EOK; @@ -208,8 +214,6 @@ struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock, struct tmpfs_file *file, *curfile; rt_list_t *list; - RT_DEFINE_SPINLOCK(lock); - subpath = path; while (*subpath == '/' && *subpath) subpath ++; @@ -234,7 +238,7 @@ find_subpath: memset(subdir_name, 0, TMPFS_NAME_MAX); _get_subdir(curpath, subdir_name); - rt_hw_spin_lock(&lock); + rt_spin_lock(&superblock->lock); rt_list_for_each(list, &curfile->subdirs) { @@ -245,7 +249,7 @@ find_subpath: { *size = file->size; - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); return file; } } @@ -254,11 +258,11 @@ find_subpath: *size = file->size; curpath = subpath; curfile = file; - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); goto find_subpath; } } - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); /* not found */ return NULL; } @@ -357,8 +361,6 @@ int dfs_tmpfs_open(struct dfs_file *file) struct dfs_filesystem *fs; char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX]; - RT_DEFINE_SPINLOCK(lock); - RT_ASSERT(file->vnode->ref_count > 0); if (file->vnode->ref_count > 1) { @@ -419,9 +421,9 @@ int dfs_tmpfs_open(struct dfs_file *file) { d_file->type = TMPFS_TYPE_FILE; } - rt_hw_spin_lock(&lock); + rt_spin_lock(&superblock->lock); rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling)); - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); } } /* Creates a new file. @@ -559,8 +561,6 @@ int dfs_tmpfs_unlink(struct dfs_filesystem *fs, const char *path) struct tmpfs_sb *superblock; struct tmpfs_file *d_file; - RT_DEFINE_SPINLOCK(lock); - superblock = (struct tmpfs_sb *)fs->data; RT_ASSERT(superblock != NULL); @@ -568,9 +568,9 @@ int dfs_tmpfs_unlink(struct dfs_filesystem *fs, const char *path) if (d_file == NULL) return -ENOENT; - rt_hw_spin_lock(&lock); + rt_spin_lock(&superblock->lock); rt_list_remove(&(d_file->sibling)); - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); if (d_file->data != NULL) rt_free(d_file->data); @@ -588,8 +588,6 @@ int dfs_tmpfs_rename(struct dfs_filesystem *fs, rt_size_t size; char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX]; - RT_DEFINE_SPINLOCK(lock); - superblock = (struct tmpfs_sb *)fs->data; RT_ASSERT(superblock != NULL); @@ -609,15 +607,15 @@ int dfs_tmpfs_rename(struct dfs_filesystem *fs, p_file = dfs_tmpfs_lookup(superblock, parent_path, &size); RT_ASSERT(p_file != NULL); - rt_hw_spin_lock(&lock); + rt_spin_lock(&superblock->lock); rt_list_remove(&(d_file->sibling)); - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); strncpy(d_file->name, file_name, TMPFS_NAME_MAX); - rt_hw_spin_lock(&lock); + rt_spin_lock(&superblock->lock); rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling)); - rt_hw_spin_unlock(&lock); + rt_spin_unlock(&superblock->lock); return RT_EOK; } diff --git a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.h b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.h index 37911bfd1b..1c9fbbb648 100644 --- a/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.h +++ b/components/dfs/dfs_v1/filesystems/tmpfs/dfs_tmpfs.h @@ -39,6 +39,7 @@ struct tmpfs_sb struct tmpfs_file root; /* root dir */ rt_size_t df_size; /* df size */ rt_list_t sibling; /* sb sibling list */ + struct rt_spinlock lock; /* tmpfs lock */ }; int dfs_tmpfs_init(void); diff --git a/include/rthw.h b/include/rthw.h index 247b4b3d2d..602866b095 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -207,8 +207,11 @@ void rt_hw_secondary_cpu_idle_exec(void); #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable() #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock)) -typedef int rt_spinlock_t; - +typedef rt_ubase_t rt_spinlock_t; +struct rt_spinlock +{ + rt_spinlock_t lock; +}; #endif #ifdef RT_USING_CACHE -- GitLab