jingfei195887 opened a new pull request, #22274:
URL: https://github.com/apache/nuttx/pull/22274
## Summary
- **fs/shm: finish munmap() bookkeeping when the process exits.**
`mm_map_destroy()` unmaps every region with a NULL group when a process
exits. In the kernel build `shmfs_unmap_area()` returned `-EINVAL` for a
NULL group, so `shmfs_munmap()` skipped the rest of the teardown: the
inode reference taken by `mmap()` leaked and an unlinked object was never
freed. A NULL group now only skips the MMU unmap, as `shmdt()` does.
- **fs/shm: keep the backing store while the object is mapped.**
`shm_open(O_TRUNC)` freed the backing store of an existing object even
while it was mapped. `mmap()` only holds an inode reference, so the
mapping kept pointing at freed memory, and a later `ftruncate()` handed
out a new buffer that the old mapping did not see. The object now counts
its mappings in `nmaps`; while it is non-zero, `shm_open(O_TRUNC)` fails
with `EBUSY`.
- **fs/shm: support resizing an object with ftruncate().**
`shmfs_truncate()` only allocated the backing store on the first call;
any later `ftruncate()` to another size, including 0, failed with
`EINVAL`. It now allocates a buffer of the new size, copies the part that
still fits and frees the old one, so the object can grow and shrink and
the new part reads back as zeros. `ftruncate(fd, 0)` drops the backing
store. While the object is mapped a resize fails with `EBUSY`.
## Impact
- New feature / API change: `ftruncate()` can resize a shm object.
- Behavior: `shm_open(O_TRUNC)` and a resizing `ftruncate()` fail with
`EBUSY` while the object is mapped, instead of freeing memory the mapping
still uses. Linux allows both and delivers `SIGBUS` on a later access
past the end; without an MMU in the flat build that is not possible.
- Memory: one `atomic_t` per shm object. A resize briefly needs both the
old and the new buffer.
- Build, hardware, documentation, security, compatibility: NO.
## Testing
Host: Ubuntu 22.04 x86_64, gcc 13.1.0. Base: master c32466a873.
`sim:nsh` (flat build) with `CONFIG_FS_SHMFS`. A test app creates a
4096-byte shm object, maps it `MAP_SHARED` and writes `'A'` at offset 0.
| step | master | this PR |
|---|---|---|
| second fd `shm_open(O_TRUNC)` while mapped, then `ftruncate(4096)`,
`pwrite('B')` | ok; old mapping reads `'@'` (freed memory), new buffer differs
| `EBUSY`; old mapping still reads `'A'` |
| `shm_open(O_TRUNC)` after `munmap()` | ok | ok |
| `ftruncate(8192)` on a 4096-byte object with `'a'` at 0 and `'z'` at 4095
| `EINVAL`, size 4096 | ok, size 8192, offsets 0/4095 keep `'a'`/`'z'`, offset
8191 reads 0 |
| `ftruncate(100)` | `EINVAL` | ok, size 100, offset 0 keeps `'a'` |
| resize while a mapping exists / same size | `EINVAL` / ok | `EBUSY` / ok |
| `ftruncate(0)` | `EINVAL` | ok, size 0 |
| map, exit without `munmap()`, then `shm_open(O_TRUNC)` from another
process | ok | ok |
`qemu-armv8a:knsh` (kernel build), same steps from a user program. Current
master does not boot this configuration: since 5acb1a3dd82 the `init` ELF
keeps an unresolved weak reference to `getopt_long_option` and the loader
fails with a relocation overflow. That commit is reverted locally for all
three rows below; it is not part of this PR.
| step | master | this PR without the first commit | this PR |
|---|---|---|---|
| `shm_open(O_TRUNC)` while mapped | ok (pages freed under the mapping) |
`EBUSY` | `EBUSY` |
| map, exit without `munmap()`, then `shm_open(O_TRUNC)` | ok | `EBUSY`
(count leaked at exit) | ok |
| `ftruncate()` grow / shrink / 0 | `EINVAL` | ok | ok, data kept, new part
reads 0 |
- `./tools/checkpatch.sh -m -g master..HEAD`: all checks pass.
- `sim:nsh` with `CONFIG_FS_SHMFS`: Make and CMake builds pass with no
warnings in the changed files; `ostest` exits with status 0.
## 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]