This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 55a759c00d20850370cd39b8e8a7fb76e971604d Author: fangxinyong <[email protected]> AuthorDate: Thu Nov 19 00:17:16 2026 +0800 fs/shm: replace inode_lock with per-object lock Replace the global inode_lock with a per-object mutex_t lock that guards paddr/length against concurrent ftruncate vs read/write/mmap. Create the object at shm_open via shmfs_alloc_object(0) so i_private stays valid from open until release/unlink; paddr is NULL and allocated on demand by the first ftruncate. shm_open O_TRUNC drops the backing store via shmfs_free_buffer under object->lock but keeps the object shell. This removes the alloc-if-NULL logic from shmfs_truncate that previously needed inode_lock; the first ftruncate now allocates the buffer under object->lock. Resize is not yet supported. Signed-off-by: fangxinyong <[email protected]> Signed-off-by: jingfei <[email protected]> --- fs/shm/shm_open.c | 22 ++++++++++-- fs/shm/shmfs.c | 95 +++++++++++++++++++++++++++++++++++++--------------- fs/shm/shmfs.h | 7 ++++ fs/shm/shmfs_alloc.c | 4 +++ 4 files changed, 98 insertions(+), 30 deletions(-) diff --git a/fs/shm/shm_open.c b/fs/shm/shm_open.c index 3e9fb035973..b148e8f5b20 100644 --- a/fs/shm/shm_open.c +++ b/fs/shm/shm_open.c @@ -43,6 +43,7 @@ static int file_shm_open(FAR struct file *shm, FAR const char *name, int oflags, mode_t mode) { + FAR struct shmfs_object_s *object; FAR struct inode *inode; struct inode_search_s desc; char fullpath[sizeof(CONFIG_FS_SHMFS_VFS_PATH) + CONFIG_NAME_MAX + 2]; @@ -130,8 +131,10 @@ static int file_shm_open(FAR struct file *shm, FAR const char *name, if ((oflags & O_TRUNC) == O_TRUNC && inode->i_private != NULL) { - shmfs_free_object(inode->i_private); - inode->i_private = NULL; + object = inode->i_private; + nxmutex_lock(&object->lock); + shmfs_free_buffer(object); + nxmutex_unlock(&object->lock); inode->i_size = 0; } } @@ -147,17 +150,30 @@ static int file_shm_open(FAR struct file *shm, FAR const char *name, goto errout_with_lock; } + /* The object lives from shm_open() until the last reference after + * unlink, so the file operations always find it. ftruncate() + * allocates its backing store. + */ + + object = shmfs_alloc_object(0); + if (object == NULL) + { + ret = -ENOMEM; + goto errout_with_lock; + } + /* Create an inode in the pseudo-filesystem at this path */ ret = inode_reserve(fullpath, mode, &inode); if (ret < 0) { + shmfs_free_object(object); goto errout_with_lock; } INODE_SET_SHM(inode); inode->u.i_ops = &g_shmfs_operations; - inode->i_private = NULL; + inode->i_private = object; inode_addref(inode); } diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c index 34818fb957b..81a6a7c0dde 100644 --- a/fs/shm/shmfs.c +++ b/fs/shm/shmfs.c @@ -159,9 +159,16 @@ static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer, sho = filep->f_inode->i_private; + nread = nxmutex_lock(&sho->lock); + if (nread < 0) + { + return nread; + } + if (filep->f_pos > sho->length) { - return 0; + nread = 0; + goto out; } /* Handle attempts to read beyond the end of the file. */ @@ -188,6 +195,8 @@ static ssize_t shmfs_read(FAR struct file *filep, FAR char *buffer, DEBUGASSERT(sho->length == 0 && nread == 0); } +out: + nxmutex_unlock(&sho->lock); return nread; } @@ -207,6 +216,12 @@ static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer, sho = filep->f_inode->i_private; + nwritten = nxmutex_lock(&sho->lock); + if (nwritten < 0) + { + return nwritten; + } + /* Handle attempts to write beyond the end of the file */ startpos = filep->f_pos; @@ -217,7 +232,8 @@ static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer, if (endpos > sho->length) { - return -EFBIG; + nwritten = -EFBIG; + goto out; } /* Copy data from the user buffer to the memory object */ @@ -232,6 +248,8 @@ static ssize_t shmfs_write(FAR struct file *filep, FAR const char *buffer, DEBUGASSERT(sho->length == 0 && nwritten == 0); } +out: + nxmutex_unlock(&sho->lock); return nwritten; } @@ -275,26 +293,33 @@ static int shmfs_close(FAR struct file *filep) static int shmfs_truncate(FAR struct file *filep, off_t length) { FAR struct shmfs_object_s *object; - int ret = 0; + int ret; if (length == 0) { return -EINVAL; } - inode_lock(); object = filep->f_inode->i_private; - if (!object) + DEBUGASSERT(object != NULL); + + ret = nxmutex_lock(&object->lock); + if (ret < 0) + { + return ret; + } + + if (object->paddr == NULL) { - filep->f_inode->i_private = shmfs_alloc_object(length); - if (!filep->f_inode->i_private) + object->paddr = shmfs_alloc_buffer(length); + if (object->paddr != NULL) { - filep->f_inode->i_size = 0; - ret = -EFAULT; + object->length = length; + filep->f_inode->i_size = length; } else { - filep->f_inode->i_size = length; + ret = -ENOMEM; } } else if (object->length != length) @@ -304,7 +329,7 @@ static int shmfs_truncate(FAR struct file *filep, off_t length) ret = -EINVAL; } - inode_unlock(); + nxmutex_unlock(&object->lock); return ret; } @@ -403,36 +428,52 @@ static int shmfs_mmap(FAR struct file *filep, FAR struct shmfs_object_s *object; int ret = -EINVAL; - /* We don't support offset at the moment, just mapping the whole object - * object is NULL if it hasn't been truncated yet - */ + /* We don't support offset at the moment, just mapping the whole object */ if (entry->offset != 0) { return ret; } + object = filep->f_inode->i_private; + DEBUGASSERT(object != NULL); + /* Keep the inode when mmapped, increase refcount */ inode_addref(filep->f_inode); - object = filep->f_inode->i_private; - if (object) + + ret = nxmutex_lock(&object->lock); + if (ret < 0) + { + inode_release(filep->f_inode); + return ret; + } + + /* Not truncated yet */ + + if (object->paddr == NULL) { + ret = -EINVAL; + goto out; + } + #ifndef CONFIG_BUILD_KERNEL - /* Without an MMU mapping the object address is returned directly, so - * a mapping longer than the page-rounded backing would expose - * adjacent heap memory. - */ + /* Without an MMU mapping the object address is returned directly, so + * a mapping longer than the page-rounded backing would expose adjacent + * heap memory. + */ - if (entry->length > ALIGN_UP(object->length, sysconf(_SC_PAGESIZE))) - { - inode_release(filep->f_inode); - return -EINVAL; - } + if (entry->length > ALIGN_UP(object->length, sysconf(_SC_PAGESIZE))) + { + ret = -EINVAL; + goto out; + } #endif - ret = shmfs_map_object(object, &entry->vaddr); - } + ret = shmfs_map_object(object, &entry->vaddr); + +out: + nxmutex_unlock(&object->lock); if (ret < 0 || (ret = shmfs_add_map(entry, filep->f_inode)) < 0) diff --git a/fs/shm/shmfs.h b/fs/shm/shmfs.h index 31b2da7ee63..88a08db1668 100644 --- a/fs/shm/shmfs.h +++ b/fs/shm/shmfs.h @@ -28,6 +28,7 @@ ****************************************************************************/ #include <nuttx/fs/fs.h> +#include <nuttx/mutex.h> /**************************************************************************** * Public Data @@ -41,6 +42,12 @@ extern const struct file_operations g_shmfs_operations; struct shmfs_object_s { + /* Protects length and paddr against concurrent ftruncate, O_TRUNC, + * read, write and mmap. + */ + + mutex_t lock; + /* Total number of bytes needed from physical memory. */ size_t length; diff --git a/fs/shm/shmfs_alloc.c b/fs/shm/shmfs_alloc.c index 40d5ef750b6..623ef8d5c42 100644 --- a/fs/shm/shmfs_alloc.c +++ b/fs/shm/shmfs_alloc.c @@ -53,11 +53,14 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) return NULL; } + nxmutex_init(&object->lock); + if (length > 0) { object->paddr = shmfs_alloc_buffer(length); if (object->paddr == NULL) { + nxmutex_destroy(&object->lock); fs_heap_free(object); return NULL; } @@ -159,6 +162,7 @@ void shmfs_free_object(FAR struct shmfs_object_s *object) if (object != NULL) { shmfs_free_buffer(object); + nxmutex_destroy(&object->lock); fs_heap_free(object); } }
