Skip to content

LVBS: Refactor per-CPU variables#695

Open
sangho2 wants to merge 3 commits intomainfrom
sanghle/lvbs/fix_pcv
Open

LVBS: Refactor per-CPU variables#695
sangho2 wants to merge 3 commits intomainfrom
sanghle/lvbs/fix_pcv

Conversation

@sangho2
Copy link
Contributor

@sangho2 sangho2 commented Mar 2, 2026

This PR refactors the LVBS platform's PerCpuVariables, improving its security, safety, and performance.

Changes:

  • Seed initial heap to remove the static PER_CPU_VARIABLE_ADDRESSES array. RefCell is no longer needed.
  • Heap-allocate PerCpuVariables on demand when each AP firstly enters VTL1.
  • Refactor PerCpuVariables struct for asm access and strict interior mutability (Cell and UnsafeCell).
  • Add safe accessor methods.
  • Clarify BSP/AP distinction.

@sangho2 sangho2 marked this pull request as ready for review March 2, 2026 17:13
@sangho2 sangho2 changed the title LVBS: Refactor Per-CPU variables LVBS: Refactor per-CPU variables Mar 2, 2026
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

🤖 SemverChecks 🤖 ⚠️ Potential breaking API changes detected ⚠️

Click for details
--- failure auto_trait_impl_removed: auto trait no longer implemented ---

Description:
A public type has stopped implementing one or more auto traits. This can break downstream code that depends on the traits being implemented.
        ref: https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/auto_trait_impl_removed.ron

Failed in:
  type PerCpuVariables is no longer Sync, in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  type PerCpuVariables is no longer RefUnwindSafe, in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27

--- failure derive_trait_impl_removed: built-in derived trait no longer implemented ---

Description:
A public type has stopped deriving one or more traits. This can break downstream code that depends on those types implementing those traits.
        ref: https://doc.rust-lang.org/reference/attributes/derive.html#derive
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/derive_trait_impl_removed.ron

Failed in:
  type PerCpuVariables no longer derives Clone, in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  type PerCpuVariables no longer derives Copy, in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27

--- failure function_missing: pub fn removed or renamed ---

Description:
A publicly-visible function cannot be imported by its prior path. A `pub use` may have been removed, or the function itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/function_missing.ron

Failed in:
  function litebox_platform_lvbs::host::per_cpu_variables::with_per_cpu_variables_mut, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:476
  function litebox_platform_lvbs::host::per_cpu_variables::with_per_cpu_variables_asm, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:494

--- failure function_parameter_count_changed: pub fn parameter count changed ---

Description:
A publicly-visible function now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/function_parameter_count_changed.ron

Failed in:
  litebox_platform_lvbs::mshv::hvcall::init now takes 1 parameters instead of 0, in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/mshv/hvcall.rs:79

--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/inherent_method_missing.ron

Failed in:
  PerCpuVariables::hv_vp_assist_page_as_ptr, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:66

--- failure repr_align_added: repr(align) added ---

Description:
repr(align(N)) was added to a type. This changes its alignment and prevents it from being used inside repr(packed) types.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#repr-align-add
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/repr_align_added.ron

Failed in:
  struct PerCpuVariablesAsm in /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:253

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/struct_missing.ron

Failed in:
  struct litebox_platform_lvbs::host::per_cpu_variables::RefCellWrapper, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:393

--- failure struct_pub_field_missing: pub struct's pub field removed or renamed ---

Description:
A publicly-visible struct has at least one public field that is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/struct_pub_field_missing.ron

Failed in:
  field vtl0_state of struct PerCpuVariables, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:41
  field vtl0_locked_regs of struct PerCpuVariables, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:42
  field gdt of struct PerCpuVariables, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:43
  field tls of struct PerCpuVariables, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:44
  field vp_index of struct PerCpuVariables, previously in file /home/runner/work/litebox/litebox/target/semver-checks/git-main/00818da810027be87eb2bcd090d8bd9bbf67f74e/litebox_platform_lvbs/src/host/per_cpu_variables.rs:45

--- failure struct_pub_field_now_doc_hidden: pub struct field is now #[doc(hidden)] ---

Description:
A pub field of a pub struct is now marked #[doc(hidden)] and is no longer part of the public API.
        ref: https://doc.rust-lang.org/rustdoc/write-documentation/the-doc-attribute.html#hidden
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/struct_pub_field_now_doc_hidden.ron

Failed in:
  field PerCpuVariables.vtl0_state in file /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  field PerCpuVariables.vtl0_locked_regs in file /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  field PerCpuVariables.gdt in file /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  field PerCpuVariables.tls in file /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27
  field PerCpuVariables.vp_index in file /home/runner/work/litebox/litebox/litebox_platform_lvbs/src/host/per_cpu_variables.rs:27

--- failure function_parameter_count_changed: pub fn parameter count changed ---

Description:
A publicly-visible function now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.46.0/src/lints/function_parameter_count_changed.ron

Failed in:
  litebox_runner_lvbs::init now takes 1 parameters instead of 0, in /home/runner/work/litebox/litebox/litebox_runner_lvbs/src/lib.rs:99

Copy link
Member

@wdcui wdcui left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

// SAFETY: We dont support concurrent AP/VTL initialization and thus share
// the same stack pointer. If we plan to support concurrent initialization,
// we should provide seperate stack pointers for each AP (which might not
// scale if there are several 100s of APs).
Copy link
Member

Choose a reason for hiding this comment

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

For performance reasons, would we need to initialize APs concurrently if there are too many of them?

Copy link
Contributor Author

@sangho2 sangho2 Mar 3, 2026

Choose a reason for hiding this comment

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

We would have two options:

  1. Spin-lock the shared boot stack page until a heap-allocated stack is ready (re-introduce c42157c).
  2. Pre-allocate temporal stack pages for all possible vCPUs and reclaim them later.

I implemented 1 (c42157c) but not 2 because I hesitated to pre-allocate 100s of pages (e.g., Azure HBv5 instances support up to 368 vCPUs) and then reverted it just because VTL0 currently does not do concurrent AP/VTL1 initialization (effectively no-op). We can re-introduce it for future proofing.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants