JianyuWang0623 opened a new pull request, #22110:
URL: https://github.com/apache/nuttx/pull/22110
## Summary
Commits 1-2 are cherry-picked from openvela, where they have already been
reviewed and merged; authorship is preserved. Commits 3-5 are new in this PR
and have not been reviewed in openvela.
The series brings the ramdisk registration/unregistration work to upstream
and
fixes the lifetime bugs in the new unregister path.
1. `misc/ramdisk: support register ramdisk with config` (Bowen Wang): adds
`struct ramdisk_config_s` and `ramdisk_register_with_config()`, so a
ramdisk
can be registered at an arbitrary device path with a caller-provided
`mode`.
`ramdisk_register()` becomes a wrapper that keeps the old `/dev/ramN` name
and `mode = 0`, so every existing caller is unchanged.
2. `ramdisk.c: support ramdisk_unregister function.` (wangshaoxin): adds
`ramdisk_unregister()` and the `romdisk_unregister()` alias, so a ramdisk
registered at runtime can be removed again.
3. `drivers/misc/ramdisk: defer unregister release`: `ramdisk_unregister()`
used to free the descriptor unconditionally. When the device is still
open,
`unregister_blockdriver()` returns `-EBUSY` after unlinking the inode, so
the open reference kept a dangling `inode->i_private`; and the call
bypassed
`rd_destroy()`, so the buffer of a device registered with `RDFLAG_FUNLINK`
was never released. The fix marks the device unlinked and lets the last
close release it, exactly as `rd_unlink()` already does, and treats
`-EBUSY` as success because the node has been removed by then.
4. `drivers/misc/ramdisk: align the transfer trace calls`: whitespace only.
nxstyle flagged the `finfo()` statements in `rd_read()`/`rd_write()`; the
indentation predates this series.
5. `drivers/misc/ramdisk: free only the FUNLINK buffer`: `rd_destroy()`
tested
`RDFLAG_USER`, i.e. `RDFLAG_WRENABLED | RDFLAG_FUNLINK`, so a ramdisk
registered with a caller owned buffer and only `RDFLAG_WRENABLED` had that
buffer `kmm_free()`d when the device was unlinked. Test `RDFLAG_FUNLINK`,
the flag that actually asks for the buffer to be released.
## Impact
Adds new ramdisk register/unregister APIs; existing callers of
`ramdisk_register()` are unchanged. Unregistering an open device now defers
the release to the last close, and a buffer is freed only when the device was
registered with `RDFLAG_FUNLINK`.
## Testing
Build Host: Linux x86_64, gcc 11.4.0
Target: `sim:asan` (`CONFIG_SIM_ASAN=y`, `CONFIG_FRAME_POINTER=y`,
`CONFIG_MM_UMM_CUSTOMIZE_MANAGER=y`) - AddressSanitizer instruments every
`kmm_alloc` in the simulation, so a stale descriptor or an unreleased buffer
is visible directly.
Each of the five commits was built on its own with `make -j16`
(`drivers/misc/ramdisk.c` recompiled and linked in every case), and the final
tree also builds with the unmodified `sim:asan` configuration and with
`CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y`.
The functional checks live in a throw-away scaffold
(`boards/sim/sim/sim/src/`
entry point, not part of this PR) that exercises the new config API with a
custom path and mode, the legacy `ramdisk_register()`, unregister of a closed
device, unregister with `RDFLAG_FUNLINK`, unregister while the device is
open,
`RDFLAG_FUNLINK` plus open during unregister, and a static buffer registered
with `RDFLAG_WRENABLED` only through both `ramdisk_unregister()` and
`unlink()`.
With the series applied:
```
TC1: register_with_config(/dev/tcdisk) = 0
TC: geometry(/dev/tcdisk) ret=0 nsectors=8 sectsize=512 write=1
TC: write(/dev/tcdisk, 3) = 1
TC: read(/dev/tcdisk, 3) = 1 first=a5
TC2: ramdisk_register(7) = 0
TC: geometry(/dev/ram7) ret=0 nsectors=8 sectsize=512 write=1
TC3: unregister(/dev/ram7) = 0 uordblks 486558 -> 486370 (delta -188)
TC3: reopen(/dev/ram7) = -2
TC4: unregister(/dev/tcfunlink) = 0 delta -4337
TC5: unregister while open = 0
TC5: write via held inode = 1
TC5: reopen = -2
TC5: after close uordblks 490704 -> 490514 (delta -190)
TC6: unregister while open = 0 delta 0
TC6: after close delta -4337
TC7: unregister static+WRENABLED = 0 delta -192
TC8: unlink static+WRENABLED = 0 delta -192
```
Without commit 5 (first four commits), the static-buffer cases abort inside
`rd_destroy()` -> `free()`:
```
TC7: register(/dev/tcstatic) = 0
sim/sim_ummheap.c:231:7: runtime error: member access within misaligned
address 0xffffffffffffffff for type 'struct list_node'
#0 update_stats sim/sim_ummheap.c:231
#1 delay_free sim/sim_ummheap.c:365
#2 free sim/sim_ummheap.c:552
#3 rd_destroy misc/ramdisk.c:151
#4 ramdisk_unregister misc/ramdisk.c:540
Segmentation fault
```
With `CONFIG_DISABLE_PSEUDOFS_OPERATIONS=y`:
```
TC: register(/dev/tcpfs) = 0
TC: unregister(closed) = 0 delta -189
TC: register(/dev/tcpfs2) = 0
TC: open(/dev/tcpfs2) = 0
TC: unregister(open) = -16 delta 0
TC: close_blockdriver = 0
```
The deltas are the descriptor plus the backing buffer where the flag asks for
it: a `RDFLAG_FUNLINK` device returns descriptor + 4 KiB, a
`RDFLAG_WRENABLED`
only device returns the descriptor and keeps the caller's buffer.
checkpatch: `tools/checkpatch.sh -c -u -m -g <base>..HEAD`.
```
✔️ All checks pass.
```
--
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]