Skip to content

util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci

Peter Maydell qemu-devel qemu-rust 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest6 patches 13 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6util/qemu-timer: Make timerlist_run_timers() file localinclude/qemu/timer.h util/qemu-timer.c+9 -112 replies
2/6util/qemu-timer: Add optional reentrancy guard to the timer APIinclude/qemu/timer.h util/qemu-timer.c util/trace-events+34 -23 replies
3/6util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full()include/qemu/aio.h include/qemu/timer.h net/slirp.c +4 more+27 -152 replies
4/6util/qemu_timer: Add timer_new_ns_guarded()include/qemu/timer.h+27 -02 replies
5/6hw/usb/hcd-xhci: Use timer reentrancy-guard APIhw/usb/hcd-xhci.c+6 -141 reply
6/6hw/net/pcnet: Use timer_new_ns_guarded()hw/net/pcnet.c+3 -12 replies

Reviews trailers per patch

All 6 patches have a Reviewed-by or Acked-by.

PatchThomas HuthManos Pitsidianakis
1/6R-b R-b
2/6R-b
3/6R-b R-b
4/6R-b
5/6R-b
6/6R-b R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH 0/6] util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci

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(-)
Signature · 1 line
2.43.0