Skip to content

Commit 8d4ffd7

Browse files
1a1a11aclaude
andcommitted
fix(prober): skip readdir on pseudo filesystems (/proc, /sys, cgroup, ...)
read/write/open already filter pseudo-filesystems via is_regular_file(), but the readdir probe (iterate_dir) operates on a directory and so was never filtered. As a result /proc-walking monitors dominated the trace: analysis of the v4 capture showed readdir was ~35% of ALL VFS events, and htop alone — reading /proc/<pid>/task on every refresh — was ~27% of all events, none of it real storage I/O. - Factor the (previously duplicated) pseudo-fs magic denylist into a single is_pseudo_fs_magic() helper, plus an is_pseudo_fs_file() convenience wrapper. is_regular_file()/is_regular_file_from_path() now call it (no behavior change — identical magic set, including tmpfs). - trace_readdir() now returns early when the directory lives on a pseudo fs. Effect: drops the /proc/sys/cgroup readdir flood at the source (~25-30% fewer fs events on this workload), cutting trace size, tracer overhead, and the observer effect from monitoring tools and the tracer's own /proc polling. Real storage readdir (ext4/overlayfs/squashfs/nfs/...) is unaffected. tmpfs is kept in the denylist to preserve existing read/write behavior; to also capture tmpfs (/tmp, /dev/shm) as real I/O, drop TMPFS_MAGIC from is_pseudo_fs_magic() — deferred because its volume impact (esp. vLLM /dev/shm) needs a real-run measurement. No eBPF C compile-tested here (needs clang+BCC+root); run scripts/smoke_test.sh. pytest: 71 passed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e2c3d09 commit 8d4ffd7

1 file changed

Lines changed: 64 additions & 51 deletions

File tree

src/tracer/prober/prober.c

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -907,29 +907,22 @@ static __always_inline void get_file_source(struct file *file, u32 *dev,
907907
}
908908

909909
/**
910-
* @brief Check if a file is a regular file (not virtual/pseudo filesystem)
911-
*
912-
* Filters out pseudo-filesystems (proc, sys, devtmpfs, etc.) to focus
913-
* tracing on real storage I/O. Uses filesystem magic numbers for detection.
914-
*
915-
* @param file Kernel file structure pointer
916-
* @return true if regular file on real filesystem, false otherwise
910+
* @brief True if a superblock magic belongs to a pseudo / virtual filesystem
911+
* that carries no durable storage I/O (procfs, sysfs, cgroup, tracefs,
912+
* debugfs, bpffs, sockets, pipes, ptys, ...).
913+
*
914+
* Single source of truth for the pseudo-fs denylist, used by both the regular-
915+
* file checks (read/write/open) and the directory-iteration probe (readdir).
916+
* Skipping these keeps the trace focused on real storage and removes the large
917+
* readdir flood from /proc-walking monitors (htop/top/ps) and the tracer's own
918+
* /proc polling — see analysis: ~27% of all VFS events were htop reading
919+
* /proc/<pid>/task.
920+
*
921+
* NOTE: TMPFS_MAGIC is listed here to preserve the existing read/write filtering
922+
* behavior unchanged. To also capture tmpfs (/tmp, /dev/shm) as real I/O, remove
923+
* the TMPFS_MAGIC case below — it then applies uniformly to every op.
917924
*/
918-
static bool is_regular_file(struct file *file) {
919-
bool is_reg, is_virtual;
920-
if (!file || !file->f_path.dentry || !file->f_path.dentry->d_inode ||
921-
!file->f_path.dentry->d_sb) {
922-
return false;
923-
}
924-
umode_t mode;
925-
bpf_probe_read_kernel(&mode, sizeof(mode),
926-
&file->f_path.dentry->d_inode->i_mode);
927-
is_reg = S_ISREG(mode); /* Check if regular file (not dir/socket/pipe) */
928-
929-
struct super_block *sb = file->f_path.dentry->d_sb;
930-
unsigned long magic = 0;
931-
bpf_probe_read_kernel(&magic, sizeof(magic), &sb->s_magic);
932-
925+
static __always_inline bool is_pseudo_fs_magic(unsigned long magic) {
933926
switch (magic) {
934927
case PROC_SUPER_MAGIC:
935928
case SYSFS_MAGIC:
@@ -948,14 +941,48 @@ static bool is_regular_file(struct file *file) {
948941
case TRACEFS_MAGIC:
949942
case 0x63677270: /* CGROUP2_SUPER_MAGIC — cgroup v2 unified hierarchy */
950943
case 0xCAFE4A11: /* BPF_FS_MAGIC — bpffs */
951-
case 0x19800202:
952-
is_virtual = true;
953-
break;
944+
case 0x19800202: /* mqueue / eventpoll / aio-ring family */
945+
return true;
954946
default:
955-
is_virtual = false;
947+
return false;
956948
}
949+
}
950+
951+
/**
952+
* @brief True if the file lives on a pseudo / virtual filesystem.
953+
*/
954+
static __always_inline bool is_pseudo_fs_file(struct file *file) {
955+
if (!file) return false;
956+
struct dentry *d = NULL;
957+
bpf_probe_read_kernel(&d, sizeof(d), &file->f_path.dentry);
958+
if (!d) return false;
959+
struct super_block *sb = NULL;
960+
bpf_probe_read_kernel(&sb, sizeof(sb), &d->d_sb);
961+
if (!sb) return false;
962+
unsigned long magic = 0;
963+
bpf_probe_read_kernel(&magic, sizeof(magic), &sb->s_magic);
964+
return is_pseudo_fs_magic(magic);
965+
}
957966

958-
return !is_virtual && is_reg;
967+
/**
968+
* @brief Check if a file is a regular file on a real (non-pseudo) filesystem.
969+
*
970+
* Filters out pseudo-filesystems (proc, sys, devtmpfs, etc.) to focus
971+
* tracing on real storage I/O. Uses filesystem magic numbers for detection.
972+
*
973+
* @param file Kernel file structure pointer
974+
* @return true if regular file on real filesystem, false otherwise
975+
*/
976+
static bool is_regular_file(struct file *file) {
977+
if (!file || !file->f_path.dentry || !file->f_path.dentry->d_inode ||
978+
!file->f_path.dentry->d_sb) {
979+
return false;
980+
}
981+
umode_t mode;
982+
bpf_probe_read_kernel(&mode, sizeof(mode),
983+
&file->f_path.dentry->d_inode->i_mode);
984+
if (!S_ISREG(mode)) return false; /* not dir/socket/pipe/etc. */
985+
return !is_pseudo_fs_file(file);
959986
}
960987

961988
static bool is_regular_file_from_path(const struct path *path) {
@@ -977,30 +1004,7 @@ static bool is_regular_file_from_path(const struct path *path) {
9771004

9781005
unsigned long magic = 0;
9791006
bpf_probe_read_kernel(&magic, sizeof(magic), &sb->s_magic);
980-
981-
switch (magic) {
982-
case PROC_SUPER_MAGIC:
983-
case SYSFS_MAGIC:
984-
case TMPFS_MAGIC:
985-
case SOCKFS_MAGIC:
986-
case DEBUGFS_MAGIC:
987-
case DEVPTS_SUPER_MAGIC:
988-
case DEVTMPFS_MAGIC:
989-
case PIPEFS_MAGIC:
990-
case CGROUP_SUPER_MAGIC:
991-
case SELINUX_MAGIC:
992-
case FUTEXFS_SUPER_MAGIC:
993-
case INOTIFYFS_SUPER_MAGIC:
994-
case XENFS_SUPER_MAGIC:
995-
case RPCAUTH_GSSMAGIC:
996-
case TRACEFS_MAGIC:
997-
case 0x63677270: /* CGROUP2_SUPER_MAGIC — cgroup v2 unified hierarchy */
998-
case 0xCAFE4A11: /* BPF_FS_MAGIC — bpffs */
999-
case 0x19800202:
1000-
return false;
1001-
default:
1002-
return true;
1003-
}
1007+
return !is_pseudo_fs_magic(magic);
10041008
}
10051009

10061010
/**
@@ -2133,6 +2137,15 @@ int trace_readdir(struct pt_regs *ctx, struct file *file,
21332137
return 0;
21342138
}
21352139

2140+
/* Skip directory reads on pseudo filesystems (/proc, /sys, cgroup, ...).
2141+
* Unlike read/write/open (which go through is_regular_file), readdir operates
2142+
* on a directory and so was never filtered — and it dominated event volume:
2143+
* /proc-walking monitors (htop/top/ps reading /proc/<pid>/task) accounted for
2144+
* ~27% of all VFS events, none of it real storage I/O. */
2145+
if (is_pseudo_fs_file(file)) {
2146+
return 0;
2147+
}
2148+
21362149
struct data_t data = {};
21372150
data.pid = pid;
21382151
data.ts = bpf_ktime_get_ns();

0 commit comments

Comments
 (0)