SMP: secondary cores read RUNNING_THREADS without synchronization (READY_CORES is incremented with Relaxed)

Aperta Adatta ai principianti
#591 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Leggi scheduler/mod.rs intorno a wait_and_then_start_schedule e al READY_CORES.fetch_add in schedule; segui la scrittura del thread idle in scheduler/idle.rs per capire cosa pubblica il contatore. L’issue segnala che il controllo KMiri passa dopo aver modificato l’incremento in Release; esegui il test unitario KMiri con due hart su qemu_riscv64 e conferma che questa race è scomparsa. Il pattern SmpStagedInit menzionato separatamente e la race successiva di RUNNING_THREADS non rientrano nella correzione indicata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

On SMP boards, the secondary cores read RUNNING_THREADS without a happens-before edge to core 0's write of it. Core 0 publishes "the scheduler is ready" with a Relaxed increment, so the secondary core's Acquire load synchronizes with nothing. Under the Rust memory model this is a data race on a non-atomic location, which is undefined behavior. On weakly ordered hardware (RVWMO, Arm) a secondary core may see the counter before it sees the idle thread that core 0 stored.

Where (main, e0b476f2d)

Core 0, in boot::init (boot.rs:84):

  1. scheduler::init() (boot.rs:128) → idle::init_idle_threads() → scheduler/idle.rs:66 stores every core's idle thread with a plain write:
    RUNNING_THREADS[i].write(arc.clone());
    
  2. arch::start_schedule(scheduler::schedule) (boot.rs:145). scheduler/mod.rs:469, in schedule():
    READY_CORES.fetch_add(1, Ordering::Relaxed);
    

Secondary cores, after board init (e.g. boards/qemu_riscv64/mod.rs:72), run wait_and_then_start_schedule (scheduler/mod.rs:460):

while READY_CORES.load(Ordering::Acquire) == 0 {
    core::hint::spin_loop();
}
arch::start_schedule(schedule);

start_schedule calls current_thread_ref(), which reads RUNNING_THREADS[my_id] (scheduler/mod.rs:490).

An Acquire load only synchronizes with a Release (or stronger) store. fetch_add(1, Relaxed) is not one, so nothing orders the write at idle.rs:66 before the read at mod.rs:490.

Fix

Make the increment release the writes before it:

READY_CORES.fetch_add(1, Ordering::Release);

SmpStagedInit::run (support.rs:379 and support.rs:392) uses the same pattern: core 0 finishes a stage, then self.stage.fetch_add(1, Ordering::Relaxed), and the other cores wait with load(Acquire). Any state from init_runtime or init_heap that a secondary core reads afterwards has the same problem. I have not shown a concrete racing access there, so I only flag it.

How it was found

We ran the qemu_riscv64 unit-test kernel with two harts in KMiri, a Miri-based interpreter for kernels that checks for language-level UB, including data races. All harts start at the reset entry, as on QEMU. The run stops right after boot:

error: Undefined Behavior: Data race detected between (1) non-atomic write on thread `main`
       and (2) non-atomic read on thread `unnamed-1`
  (2) core::ptr::NonNull::as_ref  <- TinyArc::deref <- scheduler::current_thread_ref (scheduler/mod.rs:490)
      <- arch::start_schedule <- scheduler::wait_and_then_start_schedule <- board init (hart 1)
  (1) scheduler/idle.rs:66  RUNNING_THREADS[i].write(arc.clone())  (hart 0)

With only mod.rs:469 changed to Ordering::Release, this race is no longer reported.

The run then reaches the race that the FIXME above is_idle_core (scheduler/mod.rs:525) already mentions: notify_idle_cores reads another core's RUNNING_THREADS[id] while that core replaces it in set_current_thread. I mention it only because it is the next thing an SMP run hits.

Lingua principale
Rust
Stelle
1.3k
Fork
95
Merge medio
2g 12h
PR unite (30g)
29

Preparare l'ambiente

  • Include un Dockerfile o un file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vivoblueos/kernel

Tutte le issue di vivoblueos/kernel

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.