Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions kernel/src/filesystem/procfs/pid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ impl DirOps for PidDirOps {
Some((cred.euid.data(), cred.egid.data()))
}

fn validate_child(&self, _child: &dyn IndexNode) -> bool {
ProcessManager::find(self.pid).is_some()
}

fn lookup_child(
&self,
dir: &ProcDir<Self>,
Expand Down
39 changes: 23 additions & 16 deletions kernel/src/filesystem/procfs/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,20 @@ impl DirOps for RootDirOps {
) -> Result<Arc<dyn IndexNode>, SystemError> {
// 首先检查是否是 PID 目录
if let Ok(pid) = name.parse::<RawPid>() {
// 检查进程是否存在
if ProcessManager::find(pid).is_some() {
let mut cached_children = dir.cached_children().write();

// 检查缓存中是否已存在
if let Some(child) = cached_children.get(name) {
return Ok(child.clone());
}

// 创建新的 PID 目录(只传递 PID,不传递进程引用)
let inode = PidDirOps::new_inode(pid, dir.self_ref_weak().clone());
cached_children.insert(name.to_string(), inode.clone());
return Ok(inode);
} else {
let mut cached_children = dir.cached_children().write();

if ProcessManager::find(pid).is_none() {
cached_children.remove(name);
return Err(SystemError::ENOENT);
Comment on lines +90 to 92
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Validate cached /proc/ entries before returning them

ProcDir::find returns a cached child immediately unless validate_child rejects it (see template/dir.rs), but RootDirOps still uses the default validator, so cached /proc/<pid> entries bypass this new existence check entirely. In practice, if a PID directory was cached before reap, later lookups can still resolve that stale inode without entering this lookup_child path, so /proc/<pid> may remain visible until a separate readdir triggers cache pruning.

Useful? React with 👍 / 👎.

}

if let Some(child) = cached_children.get(name) {
return Ok(child.clone());
}

let inode = PidDirOps::new_inode(pid, dir.self_ref_weak().clone());
cached_children.insert(name.to_string(), inode.clone());
return Ok(inode);
}

// 查找静态条目
Expand Down Expand Up @@ -132,8 +130,17 @@ impl DirOps for RootDirOps {
// 获取缓存写锁并填充
let mut cached_children = dir.cached_children().write();

// 填充进程目录(只传递 PID)
for pid in pid_list {
// 先删除已经失效的 PID 目录缓存
cached_children.retain(|name, _| {
if let Ok(pid) = name.parse::<RawPid>() {
pid_list.contains(&pid)
Comment on lines +134 to +136
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Avoid quadratic PID cache pruning in /proc readdir

This cache cleanup performs pid_list.contains(&pid) inside retain, making populate_children() O(number_of_cached_entries × number_of_pids). On systems with many tasks, each /proc directory listing can become disproportionately expensive while holding the cache write lock, causing avoidable latency spikes; converting pid_list to a set before retain would keep pruning linear.

Useful? React with 👍 / 👎.

} else {
true
}
});

// 再填充当前仍存在的 PID 目录
for pid in pid_list.iter().copied() {
cached_children
.entry(pid.to_string())
.or_insert_with(|| PidDirOps::new_inode(pid, dir.self_ref_weak().clone()));
Expand Down
Loading