All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/6] util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci
@ 2026-12-04 13:19 Peter Maydell
  2026-12-04 13:19 ` [PATCH 1/6] util/qemu-timer: Make timerlist_run_timers() file local Peter Maydell
                   ` (6 more replies)
  0 siblings, 7 replies; 20+ messages in thread
From: Peter Maydell @ 2026-12-04 13:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: Jason Wang, Stefan Hajnoczi, Fam Zheng, Manos Pitsidianakis,
	Marc-André Lureau, Samuel Thibault, Paolo Bonzini,
	Thomas Huth, qemu-rust

It's common for devices that use QEMU timers to do things in their
timer callbacks that may either directly be a DMA operation, or to
call out to other device models (e.g.  by setting an interrupt) that
might indirectly trigger a DMA operation.  If the guest can arrange
for that DMA operation to be to an address which is in the device's
own memory-mapped registers, the device code will be re-entered in a
way it doesn't expect.

Our mechanism for avoiding that is the MemReentrancyGuard.  We
automatically set that when calling into a device's memory operation
read and write functions, and if the device directly uses the QEMUBH
API they can ask for the BH callbacks to be guarded (via e.g.
aio_bh_new_guarded() or qemu_bh_new_guarded()), but for a timer
callback the device has to manually enable its guard.  We did that
for the USB XHCI device in commit f91b0544fe1f ("hw/usb/hcd-xhci: Set
reentrancy guard in timer functions").

To make it less awkward for devices to enable a reentrancy guard for
their timer callbacks, add support for the guard to the QEMU timer
API, in the same way that we did for the BH API in commit 9c86c97f12
("async: Add an optional reentrancy guard to the BH API").

This patchset updates the timer API, reworks the hcd-xhci fix to use
it instead of manually enabling the guard, and uses the new API to fix
a bug in the pcnet device. (This isn't a security issue as that device
is outside the security umbrella.)

There are a lot of timer_new* and timer_init* variants; I have only
added versions that let you set the guard for the specific case that I
needed for this device, rather than adding a lot of new wrapper
functions with no callers. We can add others as we need them.

NB: the change to the Rust code is a one-liner to pass an extra
NULL argument, but it is essentially untested by me.

Side question that I thought of while doing this: should the code that
enables a reentrancy guard take a QOM reference on the device while it
holds the guard? This timer code, like the AIO code, imposes an
implicit requirement that the AIO BH or timer callback doesn't
actually free the device (since it holds a pointer to the
MemReentrancyGuard across the callback call).  That seems reasonable,
but maybe holding a ref would be more bulletproof?

thanks
-- PMM

Peter Maydell (6):
  util/qemu-timer: Make timerlist_run_timers() file local
  util/qemu-timer: Add optional reentrancy guard to the timer API
  util/qemu-timer: Add MemRentrancyGuard to timer_{init,new}_full()
  util/qemu_timer: Add timer_new_ns_guarded()
  hw/usb/hcd-xhci: Use timer reentrancy-guard API
  hw/net/pcnet: Use timer_new_ns_guarded()

 include/qemu/aio.h             |  8 ++---
 include/qemu/timer.h           | 56 +++++++++++++++++++++++++---------
 hw/net/pcnet.c                 |  4 ++-
 hw/usb/hcd-xhci.c              | 20 ++++--------
 net/slirp.c                    |  4 +--
 tests/unit/ptimer-test-stubs.c |  4 ++-
 ui/input.c                     |  3 +-
 util/qemu-timer.c              | 44 +++++++++++++++++++++++---
 rust/util/src/timer.rs         |  1 +
 util/trace-events              |  3 ++
 10 files changed, 105 insertions(+), 42 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2026-12-05  9:54 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 13:19 [PATCH 0/6] util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci Peter Maydell
2026-12-04 13:19 ` [PATCH 1/6] util/qemu-timer: Make timerlist_run_timers() file local Peter Maydell
2026-12-04 14:51   ` Thomas Huth
2026-12-05  9:48   ` Manos Pitsidianakis
2026-12-04 13:19 ` [PATCH 2/6] util/qemu-timer: Add optional reentrancy guard to the timer API Peter Maydell
2026-12-04 15:00   ` Thomas Huth
2026-12-04 15:05     ` Peter Maydell
2026-12-04 15:10       ` Thomas Huth
2026-12-04 13:19 ` [PATCH 3/6] util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full() Peter Maydell
2026-12-04 15:22   ` [PATCH 3/6] util/qemu-timer: Add MemRentrancyGuard to timer_{init,new}_full() Thomas Huth
2026-12-05  9:51   ` [PATCH 3/6] util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full() Manos Pitsidianakis
2026-12-04 13:19 ` [PATCH 4/6] util/qemu_timer: Add timer_new_ns_guarded() Peter Maydell
2026-12-04 15:26   ` Thomas Huth
2026-12-04 15:52     ` Peter Maydell
2026-12-04 13:19 ` [PATCH 5/6] hw/usb/hcd-xhci: Use timer reentrancy-guard API Peter Maydell
2026-12-04 15:28   ` Thomas Huth
2026-12-04 13:19 ` [PATCH 6/6] hw/net/pcnet: Use timer_new_ns_guarded() Peter Maydell
2026-12-04 15:28   ` Thomas Huth
2026-12-05  9:53   ` Manos Pitsidianakis
2026-12-04 15:32 ` [PATCH 0/6] util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci Thomas Huth

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.