From 786b315947cdc47283a8fdfd29aaae29bbc00d92 Mon Sep 17 00:00:00 2001 From: zhanxiaoqi Date: Thu, 23 Apr 2026 11:48:54 +0800 Subject: [PATCH] fs/inode: add optional manual FD backtrace control via task group flag 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 --- fs/inode/inode.h | 21 +++++++++++++++------ fs/vfs/Kconfig | 10 ++++++++++ include/nuttx/sched.h | 1 + sched/group/group_create.c | 6 ++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/fs/inode/inode.h b/fs/inode/inode.h index 178368b7304..638959ff8fd 100644 --- a/fs/inode/inode.h +++ b/fs/inode/inode.h @@ -75,13 +75,22 @@ # define FS_ADD_BACKTRACE(fd) \ do \ { \ - int n = sched_backtrace(_SCHED_GETTID(), \ - (fd)->f_backtrace, \ - CONFIG_FS_BACKTRACE, \ - CONFIG_FS_BACKTRACE_SKIP); \ - if (n < CONFIG_FS_BACKTRACE) \ + FAR struct tcb_s *tcb = nxsched_get_tcb(nxsched_gettid()); \ + if (tcb != NULL && tcb->group != NULL && \ + (tcb->group->tg_flags & GROUP_FLAG_FD_BACKTRACE) != 0) \ { \ - (fd)->f_backtrace[n] = NULL; \ + int n = sched_backtrace(tcb->pid, \ + (fd)->f_backtrace, \ + CONFIG_FS_BACKTRACE, \ + CONFIG_FS_BACKTRACE_SKIP); \ + if (n < CONFIG_FS_BACKTRACE) \ + { \ + (fd)->f_backtrace[n] = NULL; \ + } \ + } \ + else \ + { \ + (fd)->f_backtrace[0] = NULL; \ } \ } \ while (0) diff --git a/fs/vfs/Kconfig b/fs/vfs/Kconfig index ed51ee08391..7331398f543 100644 --- a/fs/vfs/Kconfig +++ b/fs/vfs/Kconfig @@ -99,3 +99,13 @@ config FS_BACKTRACE_SKIP depends on FS_BACKTRACE > 0 ---help--- Skip depth of backtrace. + +config FS_BACKTRACE_DEFAULT + bool "Enable the backtrace record by default" + default y + depends on FS_BACKTRACE > 0 + ---help--- + Enable automatic backtrace capture for all file descriptor operations + globally. When disabled, backtrace capture is zero-cost by default + and must be explicitly enabled per task group by setting the + GROUP_FLAG_FD_BACKTRACE flag. diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h index 05f792a92d1..cb99b541e27 100644 --- a/include/nuttx/sched.h +++ b/include/nuttx/sched.h @@ -117,6 +117,7 @@ #define GROUP_FLAG_PRIVILEGED (1 << 1) /* Bit 1: Group is privileged */ #define GROUP_FLAG_DELETED (1 << 2) /* Bit 2: Group has been deleted but not yet freed */ #define GROUP_FLAG_EXITING (1 << 3) /* Bit 3: Group exit is in progress */ +#define GROUP_FLAG_FD_BACKTRACE (1 << 4) /* Bit 4: Enable FD backtrace for the group */ /* Bits 3-7: Available */ /* Values for struct child_status_s ch_flags */ diff --git a/sched/group/group_create.c b/sched/group/group_create.c index e65788dff82..473f6df0acf 100644 --- a/sched/group/group_create.c +++ b/sched/group/group_create.c @@ -163,6 +163,12 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype) sq_init(&group->tg_members); #endif +#ifdef CONFIG_FS_BACKTRACE_DEFAULT + /* Enable FD backtrace for the group by default */ + + group->tg_flags |= GROUP_FLAG_FD_BACKTRACE; +#endif + /* Attach the group to the TCB */ tcb->group = group;