fs/inode: add optional manual FD backtrace control via task group flag#18782
Open
xiaoqizhan wants to merge 1 commit intoapache:masterfrom
Open
fs/inode: add optional manual FD backtrace control via task group flag#18782xiaoqizhan wants to merge 1 commit intoapache:masterfrom
xiaoqizhan wants to merge 1 commit intoapache:masterfrom
Conversation
XuNeo
reviewed
Apr 23, 2026
XuNeo
approved these changes
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_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>
xiaoxiang781216
approved these changes
Apr 24, 2026
xiaoxiang781216
approved these changes
Apr 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.