Skip to content

fs/vfs: Add FS_UIO to option out vector I/O - #20820

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fs/dirent-lite
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fs/dirent-lite

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds CONFIG_FS_UIO, a configurable Kconfig option that allows optioning out internal vector I/O overhead on constrained targets while keeping scalar file I/O direct and fast.

Motivation & Architecture

In default configurations, file_read() and file_write() synthesize a single-element struct iovec on the stack and delegate to file_readv() and file_writev(), which in turn initialize a struct uio and invoke u.i_ops->readv / u.i_ops->writev (falling back to scalar file_readv_compat / file_writev_compat if unsupported).

While this unifies the VFS path when vector I/O is actively used (e.g. by drivers/serial), on small memory-constrained microcontrollers where vector I/O is not utilized, this introduces unnecessary overhead on every standard read() and write() call:

  1. Allocating and initializing struct iovec and struct uio on the stack for every scalar read/write.
  2. Compiling and linking fs_uio.c into the binary.
  3. Extra indirect function call layers across the VFS.

With CONFIG_FS_UIO:

  • Subsystems implementing native vector I/O (such as drivers/serial) automatically select FS_UIO.
  • When CONFIG_FS_UIO is disabled:
    • file_read() and file_write() call the driver's native read() / write() operations directly.
    • fs_uio.c is omitted from the build, saving code space.
  • POSIX readv() and writev() remain fully available via scalar fallback loops (file_readv_compat() / file_writev_compat()) and are automatically garbage-collected by the linker (--gc-sections) if unreferenced by the application.

Key Implementation Details

  • Build System: fs_uio.c is conditionally compiled in both fs/vfs/CMakeLists.txt and fs/vfs/Make.defs only when CONFIG_FS_UIO=y.
  • Direct scalar path: In file_read() and file_write(), scalar operations bypass vector structures when CONFIG_FS_UIO is disabled and invoke inode->u.i_ops->read / inode->u.i_ops->write directly.
  • POSIX compatibility: readv() and writev() retain full POSIX compliance across both configurations.
  • Clean #ifdef structure: Unified fallback logic in file_readv() and file_writev() avoids code duplication between vector and scalar paths.
  • Stable ABI: struct file_operations and struct mountpt_operations layout remains unchanged, avoiding breaking in-tree and out-of-tree drivers.

Impact

  • Is new feature added? Yes (CONFIG_FS_UIO).
  • User Impact: None when enabled (default with SERIAL=y). When disabled, reduces memory footprint, avoids stack allocations, and streamlines scalar read/write operations.
  • Build Process: No impact. Compatible with both CMake/Ninja and Makefile build systems.
  • Hardware: Targets resource-constrained MCUs where serial/vector I/O is disabled or not required.
  • Compatibility: Full POSIX compliance preserved; scalar read() / write() and vector readv() / writev() continue to work across all configurations.

Testing

Host Environment

  • Host OS: Linux / WSL2 (Ubuntu, x86_64)
  • Compiler: GCC
  • Build Systems: CMake / Ninja and GNU Make

Static Analysis & Coding Standards

  • Verified all modified files with official Apache NuttX coding standard linter (nxstyle):
    tools/nxstyle fs/vfs/fs_read.c
    tools/nxstyle fs/vfs/fs_write.c
    # Result: 0 errors, 0 warnings

Build Verification Logs

  1. Standard Build (sim:nsh, with CONFIG_FS_UIO=y via SERIAL=y):

    cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
    ninja -C build nuttx
    [1260/1260] Linking C executable nuttx
    Result: SUCCESS (0 errors, 0 warnings)
    
  2. Build without Vector I/O (CONFIG_FS_UIO not set):

    # Configured without serial vector I/O (CONFIG_FS_UIO=n)
    ninja -C build nuttx
    Result: SUCCESS (0 errors, 0 warnings)
    

@github-actions github-actions Bot added Area: File System File System issues Size: L The size of the change in this PR is large labels Oct 19, 2026
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +176 B (+0.3%, 64,153 B / 262,144 B, total: 24% used)

mirtoo

  • kseg0_progmem: .text -64 B (-0.1%, 66,060 B / 131,072 B, total: 50% used)

qemu-armv8a

  • Code: .text.read +36 B, .text.write +36 B (-0.0%, 352,128 B)

rx65n-rsk2mb

  • ROM: .text +96 B (+0.1%, 89,180 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +224 B (+0.1%, 379,984 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

acassis
acassis previously approved these changes Oct 20, 2026
@acassis

acassis commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

@jingfei195887 please help to review

Comment thread include/nuttx/fs/fs.h Resolved
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

I have added CONFIG_DISABLE_DIRECTORY_OPERATIONS to this PR alongside CONFIG_FS_DIRENT_LITE. When enabled, it completely excludes fs_dir.c, libs/libc/dirent, lib_glob.c, and guards all POSIX directory interfaces. CONFIG_FS_DIRENT_LITE now depends on BUILD_FLAT && !DISABLE_DIRECTORY_OPERATIONS. All changes pass nxstyle and have been verified with clean builds.

@jingfei195887

Copy link
Copy Markdown
Contributor

Thanks @Swatantra-66 for adding CONFIG_DISABLE_DIRECTORY_OPERATIONS here, and @xiaoxiang781216 for the suggestion.

In parallel we opened #20868, which adds the same option with a narrower scope. The differences, for comparison:

this PR (#20820) #20868
Default n DEFAULT_SMALL (same as DISABLE_PSEUDOFS_OPERATIONS)
Kernel side fs_dir.c not built, #ifdefs in fs_open.c / fs_fstat.c fs_dir.c not built; dir_allocate() / dir_fstat() become inline stubs in fs/inode/inode.h returning -ENOSYS / -ENOTTY, callers unchanged
libc / headers libs/libc/dirent, lib_glob.c and the dirent.h / ftw.h / glob.h prototypes are removed unchanged: opendir() etc. still exist and fail at run time with ENOSYS
Size not measured here stm32f103-minimum:nsh flash -996 bytes
Files 27, +576 -47 (together with FS_DIRENT_LITE) 4, +34 -2

One thing I noticed while comparing: with CONFIG_DISABLE_DIRECTORY_OPERATIONS=y on this PR (e402d70), stm32f103-minimum:nsh no longer links, because NSH still calls opendir():

nsh_fscmds.c:(.text.cp_recursive+0xe): undefined reference to `opendir'
nsh_fscmds.c:(.text.unlink_recursive+0x2a): undefined reference to `opendir'
nsh_fsutils.c:(.text.nsh_foreach_direntry+0xe): undefined reference to `opendir'

That is why #20868 keeps the libc layer and only drops the kernel directory layer: existing applications keep building, and e.g. NSH ls reports opendir failed: 38 instead of failing to link (checked on qemu-armv7a:nsh).

FS_DIRENT_LITE is independent of this and does not overlap with #20868. Happy to coordinate on the option so the two PRs don't conflict, whichever way reviewers prefer.

Comment thread fs/inode/inode.h Resolved
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Hi @xiaoxiang781216, @jingfei195887,

Thank you for the review

Per your suggestions, I have completely dropped all CONFIG_DISABLE_DIRECTORY_OPERATIONS related changes from this PR so there is zero overlap with companion PR #20868.

This PR (#20820) is now strictly focused on implementing CONFIG_FS_DIRENT_LITE (bypassing file descriptor and synthetic inode allocation for directory streams on flat builds).

Comment thread fs/Kconfig Resolved
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: M The size of the change in this PR is medium and removed Size: L The size of the change in this PR is large labels Oct 23, 2026
@xiaoxiang781216 xiaoxiang781216 changed the title fs/vfs: Add CONFIG_FS_DIRENT_LITE lightweight directory API fs/vfs: Add CONFIG_FS_VECTORED_IO to option out vector I/O Oct 23, 2026
Comment thread syscall/syscall.csv Resolved
Comment thread fs/Kconfig Resolved
@xiaoxiang781216 xiaoxiang781216 changed the title fs/vfs: Add CONFIG_FS_VECTORED_IO to option out vector I/O fs/vfs: Add FS_UIOto option out vector I/O Oct 23, 2026
Comment thread fs/vfs/fs_write.c Resolved
Comment thread fs/vfs/fs_write.c Resolved
Comment thread fs/vfs/fs_write.c Resolved
Comment thread fs/vfs/fs_read.c Resolved
Comment thread fs/vfs/fs_read.c Resolved
Comment thread fs/vfs/fs_read.c Resolved
@jingfei195887

Copy link
Copy Markdown
Contributor

Thanks for the update! It looks like the PR description still describes CONFIG_FS_DIRENT_LITE, while the current changes introduce CONFIG_FS_UIO. Could you please update the description, including the testing section, to reflect the current scope? That would make it easier to review. Thanks!

@jingfei195887

jingfei195887 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Would it make sense to also guard the readv/writev members of both struct file_operations and struct mountpt_operations with CONFIG_FS_UIO? Since the native vector callbacks are not used when FS_UIO is disabled, this could save two function pointers per retained operations table in addition to the code-size savings.

I realize this would require updating the positional initializers and other member references (for example, the writev check in fs_truncate.c), while keeping the common layout of the two structures consistent. Would you consider including this, or would it be better suited to a follow-up change? Thanks!

Provide a hidden Kconfig option CONFIG_FS_UIO (selected by subsystems
such as drivers/serial that implement vector I/O). When disabled,
file_read() and file_write() invoke the underlying driver read/write
operations directly, avoiding struct iovec stack allocations and
omitting fs_uio.c.

POSIX readv() and writev() remain available via read/write fallback
loops and are automatically stripped by the linker (--gc-sections)
when unreferenced, preserving full POSIX compliance while saving
memory on constrained targets.

Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66

Swatantra-66 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Done, updated both fs_read.c and fs_write.c

Removed (ssize_t) casts on file_get() in nx_write() and nx_read().
Simplified the #ifdef CONFIG_FS_UIO structure in file_writev() and file_readv() to avoid duplicating the fallback blocks.
Merged inode = filep->f_inode and ret = -EBADF initializations directly into variable declarations in file_write() and file_read()
Updated the PR description and testing section to reflect CONFIG_FS_UIO

@Swatantra-66

Swatantra-66 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@jingfei195887 ,That's a valid point for saving additional RAM/pointers, but it is definitely better suited as a separate follow-up PR.

Guarding readv/writev inside struct file_operations and struct mountpt_operations would break any in-tree and out-of-tree drivers or filesystems using designated initializers (.readv = ..., .writev = ...) or positional initializers when CONFIG_FS_UIO=n. It also requires updating member references across the tree (e.g. fs_truncate.c) and carefully synchronizing the union layout between file_operations and mountpt_operations.

Keeping the struct layout unchanged here allows CONFIG_FS_UIO to safely deliver the core code-size and stack savings without risking widespread driver breakage.

@xiaoxiang781216 xiaoxiang781216 changed the title fs/vfs: Add FS_UIOto option out vector I/O fs/vfs: Add FS_UIO to option out vector I/O Oct 24, 2026
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 should i ignore arm-stm32 ci error or retrigger ci

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@xiaoxiang781216 should i ignore arm-stm32 ci error or retrigger ci

let's ignore ci error which is fixed by #20949 instead wasting ci resource by retrigger.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@jingfei195887 ,That's a valid point for saving additional RAM/pointers, but it is definitely better suited as a separate follow-up PR.

@Swatantra-66 please remember to create a new patch to skip readv/writev callback in xxx_operation_s.

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Once this PR merges, I'll prepare the follow-up patch to guard the readv/writev callbacks in struct file_operations and struct mountpt_operations under CONFIG_FS_UIO

@xiaoxiang781216
xiaoxiang781216 merged commit 410fdf6 into apache:master Oct 26, 2026
45 of 52 checks passed
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 @jingfei195887 as discussed, i have opened #21047 follow-up patch to guard the readv/writev callbacks in struct file_operations and struct mountpt_operations under CONFIG_FS_UIO

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants