Skip to content

rust/hpet: Move towards lockless IO, partly

Zhao Liu qemu-devel qemu-rust 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest22 patches 7 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/22rust/migration: Add Sync implementation for Migratable<>rust/migration/src/migratable.rs+3 -00 replies
2/22rust/migration: Fix missing name in the VMSD of Migratable<>rust/migration/src/migratable.rs+4 -00 replies
3/22rust/migration: Check name field in VMStateDescriptionBuilderrust/migration/src/vmstate.rs+11 -40 replies
4/22rust/bql: Add BqlGuard to provide BQL contextinclude/qemu/main-loop.h rust/bql/src/lib.rs stubs/iothread-lock.c +1 more+142 -20 replies
5/22rust/bql: Ensure BQL locked early at BqlRefCell borrowingrust/bql/src/cell.rs+9 -70 replies
6/22rust/memory: Add enable_lockless_io bindingrust/system/src/memory.rs+14 -10 replies
7/22rust/hpet: Reduce unnecessary mutable self argumentrust/hw/timer/hpet/src/device.rs+5 -50 replies
8/22rust/hpet: Rename HPETRegister to DecodedRegisterrust/hw/timer/hpet/src/device.rs+11 -110 replies
9/22rust/hpet: Rename decoded "reg" enumeration to "target"rust/hw/timer/hpet/src/device.rs+16 -160 replies
10/22rust/hpet: Abstract HPETTimerRegisters structrust/hw/timer/hpet/src/device.rs+60 -413 replies
11/22rust/hpet: Make timer register accessors as methods of HPETTimerRegistersrust/hw/timer/hpet/src/device.rs+55 -530 replies
12/22rust/hpet: Abstract HPETRegisters structrust/hw/timer/hpet/src/device.rs+68 -440 replies
13/22rust/hpet: Make global register accessors as methods of HPETRegistersrust/hw/timer/hpet/src/device.rs+34 -260 replies
14/22rust/hpet: Borrow HPETState.regs once in HPETState::post_load()rust/hw/timer/hpet/src/device.rs+4 -20 replies
15/22rust/hpet: Explicitly initialize complex fields in init()rust/hw/timer/hpet/src/device.rs+12 -00 replies
16/22rust/hpet: Pass &BqlRefCell<HPETRegisters> as argument during MMIO accessrust/hw/timer/hpet/src/device.rs+97 -630 replies
17/22rust/hpet: Maintain HPETTimerRegisters in HPETRegistersrust/hw/timer/hpet/src/device.rs+99 -680 replies
18/22rust/hpet: Borrow BqlRefCell<HPETRegisters> at top levelrust/hw/timer/hpet/src/device.rs+177 -930 replies
19/22rust/hpet: Rename hpet_regs variables to regsrust/hw/timer/hpet/src/device.rs+58 -580 replies
20/22rust/hpet: Apply Migratable<> wrapper and ToMigrationState for HPETRegistersrust/hw/timer/hpet/src/device.rs+17 -150 replies
21/22rust/hpet: Replace BqlRefCell<HPETRegisters> with Mutex<HPETRegisters>rust/hw/timer/hpet/src/device.rs+62 -1372 replies
22/22rust/hpet: Enable lockless IOrust/hw/timer/hpet/src/device.rs+43 -242 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 00/22] rust/hpet: Move towards lockless IO, partly

Hi,

This series tries to (partly) synchronize the lockless IO of the C HPET to Rust HPET, and deploy Migratable<>/ToMigrationState to HPET.

It's "partly" because this series only uses Mutex<> to protect registers and doesn't add SeqLock.

This series is based on master branch at the commit 9febfa94b69b ("Merge tag 'for-upstream' of https://repo.or.cz/qemu/kevin into staging"). And you can also find the code here:

https://gitlab.com/zhao.liu/qemu/-/tree/rust-hpet-refactor-v1-11-11-2025

Thanks for your review!

Overview
========

1. Replacing BqlRefCell<single register> with Mutex<HPETregisters>
------------------------------------------------------------------

In fact, the most part of this series is to convert BqlCell<single register> to BqlRefCell<HPETRegisters>. The difference between the two includes:
* BqlCell<> is used for quick access but BqlRefCell<> would involve the reference with bigger scope.
* BqlRefCell<> doesn't allow multiple borrowing, which requires to take care about lifetime management.
* Placing all registers in HPETRegisters makes it more complex to justify reference lifetime.

However, once the conversion is complete, we only need to borrow BqlRefCell<> once during MMIO read/write operations. This makes replacing BqlRefCell<> with Mutex<> a straightforward way.

And to address BQL context issue during lockless IO (like commit d99041a20328 ("hpet: guard IRQ handling with BQL")), this series introduce BqlGuard to provide BQL context by hand.

But there's "difference": Rust HPET has stronger BQL guarantees and it uses BqlCell<> and BqlRefCell<> for many fields of HPETState, so that Rust HPET needs to provide BQL context for more cases that C HPET (which just guards IRQ handling). More details, please review Patch 22.

2. Leverage Migartable<> and ToMigrationState
---------------------------------------------

This series uses Migratable<> twice:
* Patch 20 applies Migratable<> for BqlRefCell<>.
* Patch 21 applies Migratable<> for Mutex<>.

Migrable<> and ToMigrationState work well at the above cases.

But there's one thing still missing (I just remember this when I'm writng this cover letter):
* ToMigrationState for Timer

The timer's ToMigrationState would be added in v2 or in a seperate patch.

Open Question
=============

I'm still a bit unsure - can we provide a property option to control whether ToMigrationState automatically generates VMState, for simple case without version/subsection consideration?

In this series, I implemented VMState for HPETTimerRegistersMigration and HPETRegistersMigration by hand. But doing such thing by hand for the generated struts seems not natural enough.

Thanks and Best Regards,
Zhao
---
Zhao Liu (22):
rust/migration: Add Sync implementation for Migratable<> rust/migration: Fix missing name in the VMSD of Migratable<> rust/migration: Check name field in VMStateDescriptionBuilder rust/bql: Add BqlGuard to provide BQL context
rust/bql: Ensure BQL locked early at BqlRefCell borrowing rust/memory: Add enable_lockless_io binding
rust/hpet: Reduce unnecessary mutable self argument rust/hpet: Rename HPETRegister to DecodedRegister rust/hpet: Rename decoded "reg" enumeration to "target" rust/hpet: Abstract HPETTimerRegisters struct
rust/hpet: Make timer register accessors as methods of
HPETTimerRegisters
rust/hpet: Abstract HPETRegisters struct
rust/hpet: Make global register accessors as methods of HPETRegisters rust/hpet: Borrow HPETState.regs once in HPETState::post_load() rust/hpet: Explicitly initialize complex fields in init() rust/hpet: Pass &BqlRefCell<HPETRegisters> as argument during MMIO
access
rust/hpet: Maintain HPETTimerRegisters in HPETRegisters rust/hpet: Borrow BqlRefCell<HPETRegisters> at top level rust/hpet: Rename hpet_regs variables to regs
rust/hpet: Apply Migratable<> wrapper and ToMigrationState for
HPETRegisters
rust/hpet: Replace BqlRefCell<HPETRegisters> with Mutex<HPETRegisters> rust/hpet: Enable lockless IO

 include/qemu/main-loop.h         |  22 +-
 rust/bql/src/cell.rs             |  16 +-
 rust/bql/src/lib.rs              | 101 ++++-
 rust/hw/timer/hpet/src/device.rs | 638 +++++++++++++++++++------------
 rust/migration/src/migratable.rs |   7 +
 rust/migration/src/vmstate.rs    |  15 +-
 rust/system/src/memory.rs        |  15 +-
 stubs/iothread-lock.c            |  11 +
 system/cpus.c                    |  10 +
 9 files changed, 583 insertions(+), 252 deletions(-)
Signature · 1 line
2.34.1