Skip to content

Conversation

@sparkzky
Copy link
Collaborator

@sparkzky sparkzky commented Feb 4, 2026

在 PrintkWriter::__write_string 中添加 OUTPUT_LOCK 自旋锁调试选项,可防止并发输出时出现字符交错问题。

  在 PrintkWriter::__write_string 中添加 OUTPUT_LOCK 自旋锁,
  可防止并发输出时出现字符交错问题。

Signed-off-by: sparkzky <sparkhhhhhhhhhh@outlook.com>
@github-actions github-actions bot added the Bug fix A bug is fixed in this pull request label Feb 4, 2026
@sparkzky
Copy link
Collaborator Author

sparkzky commented Feb 4, 2026

@claude review

@dragonos-community-ai-pr-reviewer
Copy link

dragonos-community-ai-pr-reviewer bot commented Feb 4, 2026

Claude finished @sparkzky's task in 3m 4s —— View job


PR Review: fix(printk): 添加并发输出保护锁调试选项

Requested by: @sparkzky

Current status: Review complete ✅

Todo list:

  • Read and analyze the modified file
  • Understand the code changes
  • Provide code review feedback
  • Check for potential issues and improvements

Code Review Summary

I've reviewed the changes in kernel/src/libs/printk.rs. The PR adds commented-out spinlock code to prevent character interleaving in concurrent output scenarios. Here's my analysis:

Changes Made

  1. Added import comment for libs::spinlock::SpinLock (line 13)
  2. Added commented-out static OUTPUT_LOCK declaration (line 16)
  3. Added commented-out lock acquisition in PrintkWriter::__write_string() (line 43)

Issues Identified

1. Not Actually a Fix

  • The PR title claims it's a "fix", but the code is commented out, meaning it doesn't actually fix anything
  • Concurrent output interleaving will still occur since the lock code is inactive
  • Suggestion: Either uncomment the code to implement the fix, or change the PR title to reflect that this is a "debug option template"

2. Dead Code ⚠️

  • Commented-out code adds maintenance overhead without providing functionality
  • Future developers might be confused about whether this should be enabled
  • Suggestion: Consider using conditional compilation (#[cfg(feature = "debug_output_lock")]) or a runtime flag

3. Missing Context

  • No explanation of when/why to enable this debug option
  • No documentation on potential performance implications
  • Suggestion: Add a comment explaining the trade-offs and when to use this option

Technical Analysis

Current implementation risk: Without an actual lock, concurrent calls to __write_string() from different threads/CPUs could interleave characters, making logs hard to read.

SpinLock availability: The SpinLock type exists in kernel/src/libs/spinlock.rs and appears to be a proper implementation with IRQ safety features.

Recommendations

Option 1: Implement Proper Fix (Recommended)

If concurrent output interleaving is a real issue:

use crate::libs::spinlock::SpinLock;

static OUTPUT_LOCK: SpinLock<()> = SpinLock::new(());

pub fn __write_string(&mut self, s: &str) {
    let _guard = OUTPUT_LOCK.lock();
    // ... existing code ...
}

Option 2: Add Conditional Debug Feature

If this should be an optional debug feature:

#[cfg(feature = "debug_output_lock")]
use crate::libs::spinlock::SpinLock;

#[cfg(feature = "debug_output_lock")]
static OUTPUT_LOCK: SpinLock<()> = SpinLock::new(());

pub fn __write_string(&mut self, s: &str) {
    #[cfg(feature = "debug_output_lock")]
    let _guard = OUTPUT_LOCK.lock();
    // ... existing code ...
}

Option 3: Keep as Documentation (Current Approach)

If this is meant as documentation for developers:

  • Change PR title to reflect it's documentation, not a fix
  • Add clearer comments explaining the purpose
  • Consider adding a TODO or FIXME comment

Action Items

  1. Clarify intent: Is this meant to be a fix, a debug option, or documentation?
  2. Update PR title: If it's not a functional fix, use "docs" or "chore" prefix
  3. Improve comments: Add context about when/how to use the debug option
  4. Consider implementation: If concurrent output is actually problematic, implement the lock properly

Files reviewed: kernel/src/libs/printk.rs

Fix this →

@fslongjin
Copy link
Member

这个不好。我之前貌似说过这个?
这个更优雅的实现应该是搞个更正确、合理的dmesg机制而不是简单在这加个锁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants