Skip to content

fs/inode: add optional manual FD backtrace control via task group flag#18782

Open
xiaoqizhan wants to merge 1 commit intoapache:masterfrom
xiaoqizhan:local
Open

fs/inode: add optional manual FD backtrace control via task group flag#18782
xiaoqizhan wants to merge 1 commit intoapache:masterfrom
xiaoqizhan:local

Conversation

@xiaoqizhan
Copy link
Copy Markdown
Contributor

@xiaoqizhan xiaoqizhan commented Apr 23, 2026

When CONFIG_FS_BACKTRACE is enabled, collecting a stack trace for every new file descriptor adds overhead to fast path operations like open(), dup(), and socket().

This patch adds a new configuration option CONFIG_FS_BACKTRACE_MANUAL. When enabled, backtrace capture is zero-cost by default and must be explicitly enabled per task group using GROUP_FLAG_FD_BACKTRACE. When disabled (default behavior), backtrace is automatically captured

Note: Please adhere to Contributing Guidelines.

Summary

Why the change is necessary:
When CONFIG_FS_BACKTRACE is enabled, NuttX currently performs a sched_backtrace() every time a file descriptor (FD) is created. While extremely useful for capturing "out-of-the-box"
random FD leaks, doing a full stack unwind incurs noticeable overhead. This overhead becomes a major bottleneck for lightweight, high-frequency operations that allocate FDs without
involving heavy VFS/driver IO—such as dup(), dup2(), pipe(), and networking operations like socket() and accept().

What it exactly does and how:
This PR introduces a new configuration option: CONFIG_FS_BACKTRACE_DEFAULT (similar to CONFIG_MM_BACKTRACE_DEFAULT).
The FS_ADD_BACKTRACE macro is now streamlined to strictly check for the GROUP_FLAG_FD_BACKTRACE flag within tcb->group->tg_flags.

• When CONFIG_FS_BACKTRACE_DEFAULT is enabled (default behavior): The GROUP_FLAG_FD_BACKTRACE flag is automatically set during task group allocation (group_create.c). The behavior remains
exactly the same as before—every FD creation triggers an automatic backtrace capture for all tasks globally.
• When CONFIG_FS_BACKTRACE_DEFAULT is disabled: Task groups are allocated without the flag, making the FS_ADD_BACKTRACE macro strictly zero-overhead by default. A stack trace is only
captured if developers explicitly enable the flag for specific task groups via debugging/prctl, allowing high-performance networking/IPC apps to eliminate the backtrace penalty on fast-
paths (dup, socket) while retaining manual traceability.

Impact

• Users: Developers can now choose between "always-on" global FD tracking (good for catching unknown random leaks) or a "zero-cost" manual tracking approach (good for high-performance
networking/IPC apps).
• Build Process: Adds a new Kconfig symbol CONFIG_FS_BACKTRACE_DEFAULT under the FS_BACKTRACE menu.
• Compatibility: Backward compatible. Existing configurations will not change their behavior because CONFIG_FS_BACKTRACE_DEFAULT defaults to y.
• Security/Hardware: No impact.

Testing

Host Machine: Ubuntu 22.04
Target Board: sim:nsh (Simulator) and custom Cortex-M7 board

How it was tested:

Compilation Check:

• Verified that sim:nsh builds successfully with both CONFIG_FS_BACKTRACE_DEFAULT=y and CONFIG_FS_BACKTRACE_DEFAULT=n.
• Verified that the previous undefined reference to this_task issue in fs_dup2.c has been resolved by using the safe nxsched_get_tcb(nxsched_gettid()) API in inode.h. (Note:
nxsched_get_tcb() in NuttX does not employ reference counting, thus a paired put_tcb is neither required nor exists).
• Ran ./tools/checkpatch.sh - against the final commit; all coding style checks passed.

Functional Verification (Simulated Micro-benchmark):

• Wrote a test application that calls open() and dup() sequentially in a loop.
• With CONFIG_FS_BACKTRACE_DEFAULT=y: Confirmed via procfs (or debugger) that all newly allocated FDs correctly captured the caller's stack backtrace.
• With CONFIG_FS_BACKTRACE_DEFAULT=n (Flag Disabled): Verified that no backtraces were captured, and the execution time for dup() operations dropped back to the near-zero baseline
overhead (eliminating the sched_backtrace() latency).
• With CONFIG_FS_BACKTRACE_DEFAULT=n (Flag Enabled manually): Manually set GROUP_FLAG_FD_BACKTRACE via a debugger for the test task, and confirmed that backtrace capturing resumed
immediately only for that specific task group.

@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Apr 23, 2026
Comment thread fs/inode/inode.h Outdated
Comment thread fs/vfs/Kconfig Outdated
Comment thread fs/inode/inode.h Outdated
Comment thread fs/inode/inode.h Outdated
When CONFIG_FS_BACKTRACE is enabled, collecting a stack trace for every
new file descriptor adds overhead to fast path operations like open(),
dup(), and socket().

This patch adds a new configuration option CONFIG_FS_BACKTRACE_DEFAULT.
When enabled (default behavior), the GROUP_FLAG_FD_BACKTRACE flag is
automatically set during group allocation, causing backtrace to be
captured for all tasks globally, preserving the original diagnostic
capability.

When disabled, backtrace capture is zero-cost by default and must be
explicitly enabled per task group using GROUP_FLAG_FD_BACKTRACE.

Signed-off-by: zhanxiaoqi <zhanxiaoqi@bytedance.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants