jingfei195887 opened a new pull request, #22330:
URL: https://github.com/apache/nuttx/pull/22330
## Summary
Allow a tmpfs mapping to extend past EOF without changing the file size.
The mapped tail starts as zeroes, and a second mapping observes writes
through the first mapping. The backing buffer grows only when the file
has no existing mappings. The last unmap releases the extra allocation
using the existing tmpfs allocation guards.
This includes the prerequisite correction to partial `munmap()`: removing
the tail of a mapping must shorten the mapping entry without resizing the
file. Both changes build on the mapping references from #22256, which is
already merged.
## Impact
- Behavior: `MAP_SHARED` and read-only `MAP_PRIVATE` mappings that start
inside the file and extend past EOF succeed instead of returning
`EINVAL`. The file size remains unchanged.
- Behavior: extending the allocation while another mapping exists returns
`ENOMEM`, preserving the addresses of existing mappings.
- Behavior: a partial unmap leaves the file size unchanged.
- Memory: mappings can temporarily require a larger backing buffer. The
last unmap attempts to shrink it using the existing allocation policy.
- Public API, configuration, build, hardware, documentation: no changes.
- Compatibility: mapping offsets at or beyond EOF remain invalid. Writable
private mappings retain the existing `rammap()` copy behavior.
- Security: no permission changes. The existing `FIOC_XIPBASE` interface
still returns an address without creating a counted mapping.
## Testing
Verified locally on Linux x86_64, Ubuntu 22.04, GCC 13.1.0, `sim:nsh`.
Base: `01829d2136b96ad612b61a2cf32b447d257b0f35`.
Head: `38c538c2c4d46357daf5ee7c47640a182c044083`.
Apps: `f0dc662c01`.
The local test writes 100 bytes of `a` to a tmpfs file, maps 4096 bytes,
counts nonzero bytes after offset 100, and checks `fstat()` and `pread()`.
It also tests two simultaneous mappings, an allocation growth attempt
while mapped, partial unmap of an 8192-byte file, resizing while mapped,
and mapping reference exhaustion.
| Check | Base | This PR |
|---|---|---|
| Shared read/write mapping past EOF | EINVAL | Success, zeroed tail, size
100; pread sees mapped write |
| Shared read-only mapping past EOF | EINVAL | Success, zeroed tail, size
100 |
| Private read-only mapping past EOF | EINVAL | Success, zeroed tail, size
100 |
| Private writable mapping past EOF | Success | Success |
| Allocation growth with an existing mapping | EINVAL | ENOMEM |
| A second mapping observes the first mapping's tail write | First mapping
fails | Yes |
| Heap use after both mappings are removed | Delta 0 | Delta 0 |
| Partial unmap of an 8192-byte file | File becomes 4096 bytes | File stays
8192 bytes |
| Mapping reference limit | 254, then ENOMEM | 254, then ENOMEM |
Testing logs before change:
```text
shared-rw mmap FAILED errno=22
shared-ro mmap FAILED errno=22
private-ro mmap FAILED errno=22
private-rw ok head=a past-eof-nonzero=0 size=100
grow-while-mapped first=ok second=FAILED errno=22
second-map first=FAILED second=FAILED keeps-write=NO
heap-after-munmap delta=0
partial-munmap size=4096
tmpfsmany: 254 mappings, then errno=12
```
Testing logs after change:
```text
shared-rw ok head=a past-eof-nonzero=0 size=100 read-back=z
shared-ro ok head=a past-eof-nonzero=0 size=100
private-ro ok head=a past-eof-nonzero=0 size=100
private-rw ok head=a past-eof-nonzero=0 size=100
grow-while-mapped first=ok second=FAILED errno=12
second-map first=ok second=ok keeps-write=yes
heap-after-munmap delta=0
partial-munmap size=8192
tmpfsmany: 254 mappings, then errno=12
```
Both commits were built and exercised separately. The first commit fixes
the partial-unmap size check while retaining the baseline EOF rejection;
the second enables the EOF mappings shown above. Existing mapped-buffer
resize checks passed on all three revisions.
Final commit validation:
```text
checkpatch: rc=0
commits missing your Signed-off-by: 0
make build: rc=0
warnings in changed files: 0
ostest: Exiting with status 0
cmake reconfigure: rc=0
cmake build: rc=0
worktree tracked changes after: 0
RESULT: PASS
```
Full-file `checkpatch.sh -f fs/tmpfs/fs_tmpfs.c` and `git diff --check`
also pass. Validation is limited to the simulator; no board execution is
claimed. The baseline and branch share an unrelated startup mount error
for `/tmp`; all filesystem checks use a successfully mounted `/t1`.
## 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]