-
-
Notifications
You must be signed in to change notification settings - Fork 168
fix(futex): 修复futex的一些bug #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(futex): 修复futex的一些bug #1311
Conversation
- 添加VFS层的统一vfs_truncate封装,包含文件类型和只读挂载检查 - 实现truncate系统调用处理,支持路径解析和符号链接跟随 - 修复FAT文件系统resize方法,确保页缓存和元数据同步更新 - 添加全面的用户空间测试用例,覆盖各种边界条件和错误情况 - 优化文件截断流程,统一通过VFS封装处理类型和只读检查 Signed-off-by: longjin <longjin@DragonOS.org>
Signed-off-by: longjin <longjin@DragonOS.org>
Signed-off-by: longjin <longjin@DragonOS.org>
- 实现共享匿名映射的futex键生成机制 - 添加跨进程共享匿名futex的测试用例 - 增加futex操作的调试日志输出 - 修复私有futex键的构建问题 Signed-off-by: longjin <longjin@DragonOS.org>
- 修复零超时时间立即返回ETIMEDOUT的逻辑 - 简化超时检查条件,移除错误的FLAGS_HAS_TIMEOUT检查 Signed-off-by: longjin <longjin@DragonOS.org>
- 添加Config结构体中的output_to_stdout字段 - 根据配置决定测试输出方式(文件或控制台) - 添加--stdout命令行参数以启用控制台输出 Signed-off-by: longjin <longjin@DragonOS.org>
- 修正bitset匹配逻辑,使用位与操作而非不等比较 - 移除wake_up函数的inline(always)属性 - 实现FUTEX_WAIT_BITSET的绝对超时处理 - 添加时间合法性检查和时钟选择逻辑 Signed-off-by: longjin <longjin@DragonOS.org>
- 修正FLAGS_MATCH_NONE常量值为0x00 - 确保非组长线程的TGID与组长一致 - 调整clear_child_tid操作顺序以符合Linux语义 - 优化pid_nr_ns方法实现避免潜在panic - 添加futex测试的blocklist和白名单配置 Signed-off-by: longjin <longjin@DragonOS.org>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes various bugs in the futex implementation to improve cross-process synchronization functionality.
- Fixed futex timeout handling for FUTEX_WAIT_BITSET to use absolute time semantics
- Enhanced futex key generation to properly support shared anonymous mappings across processes
- Corrected process lifecycle management including TID clearing order and thread group ID assignment
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| user/apps/tests/syscall/gvisor/whitelist.txt | Enabled futex_test in the test suite |
| user/apps/tests/syscall/gvisor/runner/src/main.rs | Added --stdout flag for direct console output |
| user/apps/tests/syscall/gvisor/runner/src/lib_sync.rs | Implemented stdout output mode and refactored file handling |
| user/apps/tests/syscall/gvisor/blocklists/futex_test | Added blocklist for problematic futex test cases |
| user/apps/c_unitest/test_cross_process_futex_shared_anon.c | Added test for cross-process futex with shared anonymous memory |
| kernel/src/syscall/mod.rs | Removed timeout flag check to allow timeouts for all futex operations |
| kernel/src/process/pid.rs | Fixed potential panic in pid_nr_ns by adding bounds checking |
| kernel/src/process/mod.rs | Fixed TID clearing order in process cleanup |
| kernel/src/process/fork.rs | Added proper TGID assignment for thread group members |
| kernel/src/mm/ucontext.rs | Added shared anonymous mapping identity for futex key generation |
| kernel/src/libs/futex/syscall.rs | Implemented absolute timeout handling for FUTEX_WAIT_BITSET |
| kernel/src/libs/futex/futex.rs | Enhanced futex key generation and added debug logging |
| kernel/src/libs/futex/constant.rs | Fixed FLAGS_MATCH_NONE constant value |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
kernel/src/libs/futex/futex.rs
Outdated
| // Debug: 打印futex_wait信息 | ||
| let current_pid = ProcessManager::current_pcb().raw_pid().data(); | ||
| let is_shared = flags.contains(FutexFlag::FLAGS_SHARED); | ||
| warn!( | ||
| "[FUTEX_DEBUG] futex_wait: pid={}, uaddr=0x{:x}, val={}, is_shared={}, key={:?}", | ||
| current_pid, | ||
| uaddr.data(), | ||
| val, | ||
| is_shared, | ||
| key | ||
| ); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| log::warn!("[FUTEX_DEBUG] futex_wait: abs_time={:?}", abs_time); | ||
| if let Some(time) = abs_time { | ||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
|
|
||
| let sec = time.tv_sec; | ||
| let nsec = time.tv_nsec; | ||
| let jiffies = next_n_us_timer_jiffies((nsec / 1000 + sec * 1_000_000) as u64); | ||
| let total_us = (nsec / 1000 + sec * 1_000_000) as u64; | ||
|
|
||
| warn!( | ||
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | ||
| sec, nsec, total_us | ||
| ); | ||
|
|
||
| // 如果超时时间为0,直接返回ETIMEDOUT | ||
| if total_us == 0 { | ||
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | ||
| return Err(SystemError::ETIMEDOUT); | ||
| } | ||
|
|
||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
| let jiffies = next_n_us_timer_jiffies(total_us); | ||
|
|
||
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| log::warn!("[FUTEX_DEBUG] futex_wait: abs_time={:?}", abs_time); | ||
| if let Some(time) = abs_time { | ||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
|
|
||
| let sec = time.tv_sec; | ||
| let nsec = time.tv_nsec; | ||
| let jiffies = next_n_us_timer_jiffies((nsec / 1000 + sec * 1_000_000) as u64); | ||
| let total_us = (nsec / 1000 + sec * 1_000_000) as u64; | ||
|
|
||
| warn!( | ||
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | ||
| sec, nsec, total_us | ||
| ); | ||
|
|
||
| // 如果超时时间为0,直接返回ETIMEDOUT | ||
| if total_us == 0 { | ||
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | ||
| return Err(SystemError::ETIMEDOUT); | ||
| } | ||
|
|
||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
| let jiffies = next_n_us_timer_jiffies(total_us); | ||
|
|
||
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| warn!( | ||
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | ||
| sec, nsec, total_us | ||
| ); | ||
|
|
||
| // 如果超时时间为0,直接返回ETIMEDOUT | ||
| if total_us == 0 { | ||
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | ||
| return Err(SystemError::ETIMEDOUT); | ||
| } | ||
|
|
||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
| let jiffies = next_n_us_timer_jiffies(total_us); | ||
|
|
||
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
| warn!( | |
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | |
| sec, nsec, total_us | |
| ); | |
| // 如果超时时间为0,直接返回ETIMEDOUT | |
| if total_us == 0 { | |
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | |
| return Err(SystemError::ETIMEDOUT); | |
| } | |
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | |
| let jiffies = next_n_us_timer_jiffies(total_us); | |
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); | |
| debug!( | |
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | |
| sec, nsec, total_us | |
| ); | |
| // 如果超时时间为0,直接返回ETIMEDOUT | |
| if total_us == 0 { | |
| debug!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | |
| return Err(SystemError::ETIMEDOUT); | |
| } | |
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | |
| let jiffies = next_n_us_timer_jiffies(total_us); | |
| debug!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
kernel/src/libs/futex/futex.rs
Outdated
| warn!( | ||
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | ||
| sec, nsec, total_us | ||
| ); | ||
|
|
||
| // 如果超时时间为0,直接返回ETIMEDOUT | ||
| if total_us == 0 { | ||
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | ||
| return Err(SystemError::ETIMEDOUT); | ||
| } | ||
|
|
||
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | ||
| let jiffies = next_n_us_timer_jiffies(total_us); | ||
|
|
||
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
| warn!( | |
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | |
| sec, nsec, total_us | |
| ); | |
| // 如果超时时间为0,直接返回ETIMEDOUT | |
| if total_us == 0 { | |
| warn!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | |
| return Err(SystemError::ETIMEDOUT); | |
| } | |
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | |
| let jiffies = next_n_us_timer_jiffies(total_us); | |
| warn!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); | |
| debug!( | |
| "[FUTEX_DEBUG] timeout: sec={}, nsec={}, total_us={}", | |
| sec, nsec, total_us | |
| ); | |
| // 如果超时时间为0,直接返回ETIMEDOUT | |
| if total_us == 0 { | |
| debug!("[FUTEX_DEBUG] zero timeout, returning ETIMEDOUT immediately"); | |
| return Err(SystemError::ETIMEDOUT); | |
| } | |
| let wakeup_helper = WakeUpHelper::new(pcb.clone()); | |
| let jiffies = next_n_us_timer_jiffies(total_us); | |
| debug!("[FUTEX_DEBUG] creating timer with jiffies={}", jiffies); |
kernel/src/libs/futex/futex.rs
Outdated
| // 从队列中唤醒 | ||
| let count = bucket_mut.wake_up(key.clone(), Some(bitset), nr_wake)?; | ||
|
|
||
| warn!("[FUTEX_DEBUG] futex_wake: woke up {} processes", count); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| }), | ||
| }); | ||
| }; | ||
| warn!("[FUTEX_DEBUG] get_futex_key: private futex, key={:?}", key); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
| warn!("[FUTEX_DEBUG] get_futex_key: private futex, key={:?}", key); | |
| debug!("[FUTEX_DEBUG] get_futex_key: private futex, key={:?}", key); |
kernel/src/libs/futex/futex.rs
Outdated
| warn!( | ||
| "[FUTEX_DEBUG] get_futex_key: shared file, dev=0x{:x}, ino=0x{:x}, pgoff={}, page_index={}, key={:?}", | ||
| dev, ino, base_pgoff, page_index, key | ||
| ); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| warn!( | ||
| "[FUTEX_DEBUG] get_futex_key: shared anon, anon_id=0x{:x}, page_index={}, key={:?}", | ||
| i_seq, page_index, key | ||
| ); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
kernel/src/libs/futex/futex.rs
Outdated
| return Ok(key); | ||
| } else { | ||
| // 理论上不会发生;为安全起见,退化为私有键(不跨进程匹配) | ||
| warn!("[FUTEX_DEBUG] get_futex_key: shared anon without id; fallback to private"); |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging statements using warn!() macro should be conditionally compiled or use a debug!() macro instead to avoid performance impact in production builds.
| warn!("[FUTEX_DEBUG] get_futex_key: shared anon without id; fallback to private"); | |
| debug!("[FUTEX_DEBUG] get_futex_key: shared anon without id; fallback to private"); |
Uh oh!
There was an error while loading. Please reload this page.