jingfei195887 opened a new pull request, #22016:
URL: https://github.com/apache/nuttx/pull/22016
## Summary
- **fs/shm: separate backing store from shmfs_object_s.**
Split the backing store out of `struct shmfs_object_s` into a buffer
allocated by the new `shmfs_alloc_buffer()` and freed by
`shmfs_free_buffer()`, so it can be dropped and reallocated while the
object stays valid. In the kernel build the page-pointer vector is now a
separate allocation instead of a flexible array at the end of the
object. No functional change.
- **fs/shm: replace inode_lock with per-object lock.**
`shm_open()` left `i_private` NULL until the first `ftruncate()`, so
`read()`, `write()` or `mmap()` on a new object before `ftruncate()` hit
`DEBUGASSERT(filep->f_inode->i_private != NULL)` in `shmfs_read()` /
`shmfs_write()`, or dereferenced NULL without assertions.
`shmfs_truncate()` also took the global inode lock just to create the
object.
Create the object in `shm_open()` with `shmfs_alloc_object(0)`, so it is
valid from open until the last reference after unlink. A per-object
mutex guards `length` and `paddr` against concurrent `ftruncate()`,
`O_TRUNC`, `read()`, `write()` and `mmap()`. The first `ftruncate()`
allocates the backing store under that lock; `O_TRUNC` drops only the
backing store. Resize is still not supported.
## Impact
- New feature / API change: NO.
- Behavior:
- `read()` before `ftruncate()` returns 0, `write()` fails with `EFBIG`
and `mmap()` with `EINVAL`, instead of an assertion or a NULL
dereference.
- A failed allocation in `ftruncate()` returns `ENOMEM` instead of
`EFAULT`.
- shm file operations no longer take the global inode lock.
- Build, hardware, documentation, security, compatibility: NO.
## Testing
Host: Ubuntu 22.04 x86_64, gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0.
`sim:nsh` with `CONFIG_FS_SHMFS` and a small test app that creates a shm
object with `shm_open(O_CREAT | O_RDWR)`, calls `read()`, `write()` and
`mmap()` before `ftruncate()`, then `ftruncate(64)`, writes and reads it
back.
Testing logs before change (master fc8d354f9a):
```
Assertion failed filep->f_inode->i_private != NULL: at file: shm/shmfs.c:158
task: fsuptest
```
Testing logs after change:
```
shmearly: read ret=0 errno=0
shmearly: write ret=-1 errno=27
shmearly: mmap FAILED errno=22
shmearly: ftruncate ret=0
shmearly: write after ftruncate ret=3
shmearly: read after ftruncate ret=1 c=s
```
- `./tools/checkpatch.sh -g master..HEAD`: all checks pass.
- `sim:nsh` with `CONFIG_FS_SHMFS`: Make and CMake builds pass with no new
warnings in the changed files; `ostest` exits with status 0. Each of the
two commits builds on its own.
- `qemu-armv8a:knsh` (kernel build, `CONFIG_FS_SHMFS`): build passes.
- `mps2-an500:citest_protected` (protected build, `CONFIG_FS_SHMFS`):
build passes.
- Verified on sim only.
## PR verification Self-Check
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing Guidelines and Documentation (git commit
title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]