Skip to contentSkip to conversation

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

PATCH New Peter Maydell 1 participant 7 messages Dec 4

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover 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
#1 lore raw

PATCH 1/6 util/qemu-timer: Make timerlist_run_timers() file local

The timerlist_run_timers() function is called only from other functions in qemu-timer.c, so it doesn't need to be global. Make it file-local instead.

---
 include/qemu/timer.h | 10 ----------
 util/qemu-timer.c    | 10 +++++++++-
 2 files changed, 9 insertions(+), 11 deletions(-)
include/qemu/timer.h +0 -10
index 9a62ac8cded..8e7c217306b 100644
@@ -317,16 +317,6 @@ bool timerlist_expired(QEMUTimerList *timer_list);
  */
 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
 
-/**
- * timerlist_run_timers:
- * @timer_list: the timer list to use
- *
- * Call all expired timers associated with the timer list.
- *
- * Returns: true if any timer expired
- */
-bool timerlist_run_timers(QEMUTimerList *timer_list);
-
 /**
  * timerlist_notify:
  * @timer_list: the timer list to use
util/qemu-timer.c +9 -1
index d93a020064f..f7920130854 100644
@@ -515,7 +515,15 @@ bool timer_expired(const QEMUTimer *timer_head, int64_t current_time)
     return timer_expired_ns(timer_head, current_time * timer_head->scale);
 }
 
-bool timerlist_run_timers(QEMUTimerList *timer_list)
+/**
+ * timerlist_run_timers:
+ * @timer_list: the timer list to use
+ *
+ * Call all expired timers associated with the timer list.
+ *
+ * Returns: true if any timer expired
+ */
+static bool timerlist_run_timers(QEMUTimerList *timer_list)
 {
     QEMUTimer *ts;
     int64_t current_time;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/6 util/qemu-timer: Add optional reentrancy guard to the timer API

In reply to Peter Maydell

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").

In this commit, we add the MemReentrancyGuard pointer to the QEMU timer struct, and if it is set we engage it across the callback. This will do nothing yet because we don't expose any API changes to allow it to be set up when the timer is initialized.

Note that it is OK to write to reentrancy_guard->engaged_in_io even after the callback has returned and potentially might have e.g. deleted a timer, because we are going to require that the guard structure itself remains valid. The expected guard is the one in the DeviceState struct, and devices shouldn't be deleting themselves in timer callbacks, so this should be OK. (It's also an implicit requirement for other uses of the reentrancy guard mechanism.)

---
 include/qemu/timer.h |  2 ++
 util/qemu-timer.c    | 31 +++++++++++++++++++++++++++++--
 util/trace-events    |  3 +++
 3 files changed, 34 insertions(+), 2 deletions(-)
include/qemu/timer.h +2 -0
index 8e7c217306b..11bcc3f0d36 100644
@@ -4,6 +4,7 @@
 #include "qemu/bitops.h"
 #include "qemu/notify.h"
 #include "qemu/host-utils.h"
+#include "qemu/mem-reentrancy.h"
 
 #define NANOSECONDS_PER_SECOND 1000000000LL
 #define MICROSECONDS_PER_SECOND 1000000LL
@@ -88,6 +89,7 @@ struct QEMUTimer {
     QEMUTimerCB *cb;
     void *opaque;
     QEMUTimer *next;
+    MemReentrancyGuard *reentrancy_guard;
     int attributes;
     int scale;
 };
util/qemu-timer.c +29 -2
index f7920130854..6658e5e623d 100644
@@ -31,6 +31,7 @@
 #include "system/replay.h"
 #include "system/cpus.h"
 #include "hw/core/cpu.h"
+#include "trace.h"
 
 #ifdef CONFIG_POSIX
 #include <pthread.h>
@@ -362,6 +363,7 @@ void timer_init_full(QEMUTimer *ts,
     ts->scale = scale;
     ts->attributes = attributes;
     ts->expire_time = -1;
+    ts->reentrancy_guard = NULL;
 }
 
 void timer_deinit(QEMUTimer *ts)
@@ -388,11 +390,33 @@ static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
     }
 }
 
+static void timer_run_callback(QEMUTimerCB *cb, void *opaque,
+                               MemReentrancyGuard *reentrancy_guard)
+{
+    /* Run the timer cb, optionally with a reentrancy guard engaged. */
+    bool last_engaged_in_io = false;
+
+    if (reentrancy_guard) {
+        last_engaged_in_io = reentrancy_guard->engaged_in_io;
+        if (last_engaged_in_io) {
+            trace_reentrant_timer(cb, opaque);
+        }
+
+        reentrancy_guard->engaged_in_io = true;
+    }
+
+    cb(opaque);
+
+    if (reentrancy_guard) {
+        reentrancy_guard->engaged_in_io = last_engaged_in_io;
+    }
+}
+
 static void timer_fire(CPUState *cpu, run_on_cpu_data data)
 {
     QEMUTimer *t = data.host_ptr;
 
-    t->cb(t->opaque);
+    timer_run_callback(t->cb, t->opaque, t->reentrancy_guard);
 }
 
 static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
@@ -571,6 +595,8 @@ static bool timerlist_run_timers(QEMUTimerList *timer_list)
     current_time = qemu_clock_get_ns(timer_list->clock->type);
     qemu_mutex_lock(&timer_list->active_timers_lock);
     while ((ts = timer_list->active_timers)) {
+        MemReentrancyGuard *reentrancy_guard;
+
         if (!timer_expired_ns(ts, current_time)) {
             /* No expired timers left.  The checkpoint can be skipped
              * if no timers fired or they were all external.
@@ -595,10 +621,11 @@ static bool timerlist_run_timers(QEMUTimerList *timer_list)
         ts->expire_time = -1;
         cb = ts->cb;
         opaque = ts->opaque;
+        reentrancy_guard = ts->reentrancy_guard;
 
         /* run the callback (the timer list can be modified) */
         qemu_mutex_unlock(&timer_list->active_timers_lock);
-        cb(opaque);
+        timer_run_callback(cb, opaque, reentrancy_guard);
         qemu_mutex_lock(&timer_list->active_timers_lock);
 
         progress = true;
util/trace-events +3 -0
index df549646d12..45763bca80f 100644
@@ -85,6 +85,9 @@ qemu_mutex_lock(void *mutex, const char *file, const int line) "waiting on mutex
 qemu_mutex_locked(void *mutex, const char *file, const int line) "taken mutex %p (%s:%d)"
 qemu_mutex_unlock(void *mutex, const char *file, const int line) "released mutex %p (%s:%d)"
 
+# qemu-timer.c
+reentrant_timer(void *cb, void *opaque) "timer callback %p opaque %p"
+
 # vfio-helpers.c
 qemu_vfio_dma_reset_temporary(void *s) "s %p"
 qemu_vfio_ram_block_added(void *s, void *p, size_t size) "s %p host %p size 0x%zx"
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/6 util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full()

In reply to Peter Maydell

Add a MemReentrancyGuard parameter to timer_init_full() and timer_new_full(), so callers can specify a guard if they want to. Since there are not many direct callers of these two "full" functions we add the parameter here rather than having a new function timer_init_full_guarded().

In a following commit we'll add _guarded() versions of some of the simpler timer initialization functions, which are what most callers will probably prefer over timer_init_full().

---
 include/qemu/aio.h             |  8 ++++----
 include/qemu/timer.h           | 17 ++++++++++++-----
 net/slirp.c                    |  4 ++--
 tests/unit/ptimer-test-stubs.c |  4 +++-
 ui/input.c                     |  3 ++-
 util/qemu-timer.c              |  5 +++--
 rust/util/src/timer.rs         |  1 +
 7 files changed, 27 insertions(+), 15 deletions(-)
include/qemu/aio.h +4 -4
index dbc5370eb03..b175d3f50d8 100644
@@ -628,7 +628,7 @@ static inline QEMUTimer *aio_timer_new_with_attrs(AioContext *ctx,
                                                   int scale, int attributes,
                                                   QEMUTimerCB *cb, void *opaque)
 {
-    return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque);
+    return timer_new_full(&ctx->tlg, type, scale, attributes, cb, opaque, NULL);
 }
 
 /**
@@ -648,7 +648,7 @@ static inline QEMUTimer *aio_timer_new(AioContext *ctx, QEMUClockType type,
                                        int scale,
                                        QEMUTimerCB *cb, void *opaque)
 {
-    return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque);
+    return timer_new_full(&ctx->tlg, type, scale, 0, cb, opaque, NULL);
 }
 
 /**
@@ -670,7 +670,7 @@ static inline void aio_timer_init_with_attrs(AioContext *ctx,
                                              int scale, int attributes,
                                              QEMUTimerCB *cb, void *opaque)
 {
-    timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque);
+    timer_init_full(ts, &ctx->tlg, type, scale, attributes, cb, opaque, NULL);
 }
 
 /**
@@ -690,7 +690,7 @@ static inline void aio_timer_init(AioContext *ctx,
                                   int scale,
                                   QEMUTimerCB *cb, void *opaque)
 {
-    timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque);
+    timer_init_full(ts, &ctx->tlg, type, scale, 0, cb, opaque, NULL);
 }
 
 /**
include/qemu/timer.h +12 -5
index 11bcc3f0d36..1287682de61 100644
@@ -394,6 +394,8 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
  * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
  * @cb: the callback to be called when the timer expires
  * @opaque: the opaque pointer to be passed to the callback
+ * @reentrancy_guard: A guard set when entering @cb to prevent
+ *    device-rentrancy issues. Optional; NULL for no guard.
  *
  * Initialise a timer with the given scale and attributes,
  * and associate it with timer list for given clock @type in @timer_list_group
@@ -406,7 +408,8 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
 void timer_init_full(QEMUTimer *ts,
                      QEMUTimerListGroup *timer_list_group, QEMUClockType type,
                      int scale, int attributes,
-                     QEMUTimerCB *cb, void *opaque);
+                     QEMUTimerCB *cb, void *opaque,
+                     MemReentrancyGuard *reentrancy_guard);
 
 /**
  * timer_init:
@@ -423,7 +426,7 @@ void timer_init_full(QEMUTimer *ts,
 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
                               QEMUTimerCB *cb, void *opaque)
 {
-    timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
+    timer_init_full(ts, NULL, type, scale, 0, cb, opaque, NULL);
 }
 
 /**
@@ -485,6 +488,8 @@ static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
  * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
  * @cb: the callback to be called when the timer expires
  * @opaque: the opaque pointer to be passed to the callback
+ * @reentrancy_guard: A guard set when entering @cb to prevent
+ *    device-rentrancy issues. Optional; NULL for no guard.
  *
  * Create a new timer with the given scale and attributes,
  * and associate it with timer list for given clock @type in @timer_list_group
@@ -507,10 +512,12 @@ static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
                                         QEMUClockType type,
                                         int scale, int attributes,
-                                        QEMUTimerCB *cb, void *opaque)
+                                        QEMUTimerCB *cb, void *opaque,
+                                        MemReentrancyGuard *reentrancy_guard)
 {
     QEMUTimer *ts = g_new0(QEMUTimer, 1);
-    timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
+    timer_init_full(ts, timer_list_group, type, scale, attributes,
+                    cb, opaque, reentrancy_guard);
     return ts;
 }
 
@@ -532,7 +539,7 @@ static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
                                    QEMUTimerCB *cb, void *opaque)
 {
-    return timer_new_full(NULL, type, scale, 0, cb, opaque);
+    return timer_new_full(NULL, type, scale, 0, cb, opaque, NULL);
 }
 
 /**
net/slirp.c +2 -2
index eea12e79816..298695a6766 100644
@@ -212,7 +212,7 @@ static void *net_slirp_timer_new_opaque(SlirpTimerId id,
     t->cb_opaque = cb_opaque;
     timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL,
                     SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
-                    net_slirp_timer_cb, t);
+                    net_slirp_timer_cb, t, NULL);
     return t;
 }
 #else
@@ -222,7 +222,7 @@ static void *net_slirp_timer_new(SlirpTimerCb cb,
     SlirpTimer *t = g_new(SlirpTimer, 1);
     timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL,
                     SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
-                    cb, cb_opaque);
+                    cb, cb_opaque, NULL);
     return t;
 }
 #endif
tests/unit/ptimer-test-stubs.c +3 -1
index da46dda32d2..2f18d626b37 100644
@@ -37,7 +37,8 @@ bool qtest_allowed;
 void timer_init_full(QEMUTimer *ts,
                      QEMUTimerListGroup *timer_list_group, QEMUClockType type,
                      int scale, int attributes,
-                     QEMUTimerCB *cb, void *opaque)
+                     QEMUTimerCB *cb, void *opaque,
+                     MemReentrancyGuard *reentrancy_guard)
 {
     if (!timer_list_group) {
         timer_list_group = &main_loop_tlg;
@@ -48,6 +49,7 @@ void timer_init_full(QEMUTimer *ts,
     ts->scale = scale;
     ts->attributes = attributes;
     ts->expire_time = -1;
+    ts->reentrancy_guard = reentrancy_guard;
 }
 
 void timer_mod(QEMUTimer *ts, int64_t expire_time)
ui/input.c +2 -1
index f40a9883701..846b936ba40 100644
@@ -431,7 +431,8 @@ void qemu_input_event_send_key_delay(uint32_t delay_ms)
     if (!kbd_timer) {
         kbd_timer = timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
                                    SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
-                                   qemu_input_queue_process, &kbd_queue);
+                                   qemu_input_queue_process, &kbd_queue,
+                                   NULL);
     }
     if (queue_count < queue_limit) {
         qemu_input_queue_delay(&kbd_queue, kbd_timer,
util/qemu-timer.c +3 -2
index 6658e5e623d..946c3dab13d 100644
@@ -352,7 +352,8 @@ int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
 void timer_init_full(QEMUTimer *ts,
                      QEMUTimerListGroup *timer_list_group, QEMUClockType type,
                      int scale, int attributes,
-                     QEMUTimerCB *cb, void *opaque)
+                     QEMUTimerCB *cb, void *opaque,
+                     MemReentrancyGuard *reentrancy_guard)
 {
     if (!timer_list_group) {
         timer_list_group = &main_loop_tlg;
@@ -363,7 +364,7 @@ void timer_init_full(QEMUTimer *ts,
     ts->scale = scale;
     ts->attributes = attributes;
     ts->expire_time = -1;
-    ts->reentrancy_guard = NULL;
+    ts->reentrancy_guard = reentrancy_guard;
 }
 
 void timer_deinit(QEMUTimer *ts)
rust/util/src/timer.rs +1 -0
index f8e65c9a0ac..9533b34deb8 100644
@@ -85,6 +85,7 @@ pub fn init_full<T, F>(
                 attributes as c_int,
                 Some(timer_cb),
                 (opaque as *mut T).cast::<c_void>(),
+                ::core::ptr::null_mut(),
             )
         }
     }
Signature · 1 line
2.43.0
#4 lore raw

PATCH 4/6 util/qemu_timer: Add timer_new_ns_guarded()

In reply to Peter Maydell

Add a timer_new_ns_guarded() function which is the same as timer_new_ns() but takes a MemReentrancyGuard.

There are a lot of different sub-flavours of timer_new() and timer_init() wrappers which could have _guarded() versions added; rather than adding half a dozen functions which have no callers, start with just the one we want for the two devices we plan to make use it.

---
 include/qemu/timer.h | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
include/qemu/timer.h +27 -0
index 1287682de61..85504e2bc53 100644
@@ -562,6 +562,33 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
     return timer_new(type, SCALE_NS, cb, opaque);
 }
 
+/**
+ * timer_new_ns_guarded:
+ * @type: the clock type to associate with the timer
+ * @cb: the callback to call when the timer expires
+ * @opaque: the opaque pointer to pass to the callback
+ * @reentrancy_guard: A guard set when entering @cb to prevent
+ *    device-rentrancy issues
+ *
+ * Create a new timer with nanosecond scale on the default timer list
+ * associated with the clock, and a reentrancy guard.
+ * See timer_new_full for details.
+ *
+ * The timer returned must be freed using timer_free().
+ *
+ * The guard is optional, but if you aren't specifying it then
+ * you can just use timer_new_ns() instead.
+ *
+ * Returns: a pointer to the newly created timer
+ */
+static inline QEMUTimer *timer_new_ns_guarded(QEMUClockType type,
+                                              QEMUTimerCB *cb,
+                                              void *opaque,
+                                              MemReentrancyGuard *guard)
+{
+    return timer_new_full(NULL, type, SCALE_NS, 0, cb, opaque, guard);
+}
+
 /**
  * timer_new_us:
  * @type: the clock type to associate with the timer
Signature · 1 line
2.43.0
#5 lore raw

PATCH 5/6 hw/usb/hcd-xhci: Use timer reentrancy-guard API

In reply to Peter Maydell

In commit f91b0544fe1 ("hw/usb/hcd-xhci: Set reentrancy guard in timer functions") we manually applied a reentrancy guard in the xhci_mfwrap_timer() and xhci_ep_kick_timer() timer callback functions. Now that the timer API supports automatically applying a guard when it calls a callback, use that instead.

---
 hw/usb/hcd-xhci.c | 20 ++++++--------------
 1 file changed, 6 insertions(+), 14 deletions(-)
hw/usb/hcd-xhci.c +6 -14
index aac7f6788e0..a9f7d1abbbe 100644
@@ -456,15 +456,9 @@ static void xhci_mfwrap_timer(void *opaque)
 {
     XHCIState *xhci = opaque;
     XHCIEvent wrap = { ER_MFINDEX_WRAP, CC_SUCCESS };
-    MemReentrancyGuard *guard = &xhci->parent.mem_reentrancy_guard;
-
-    assert(!guard->engaged_in_io);
-    guard->engaged_in_io = true;
 
     xhci_event(xhci, &wrap, 0);
     xhci_mfwrap_update(xhci);
-
-    guard->engaged_in_io = false;
 }
 
 static void xhci_die(XHCIState *xhci)
@@ -1110,14 +1104,8 @@ static void xhci_set_ep_state(XHCIState *xhci, XHCIEPContext *epctx,
 static void xhci_ep_kick_timer(void *opaque)
 {
     XHCIEPContext *epctx = opaque;
-    MemReentrancyGuard *guard = &epctx->xhci->parent.mem_reentrancy_guard;
-
-    assert(!guard->engaged_in_io);
-    guard->engaged_in_io = true;
 
     xhci_kick_epctx(epctx, 0);
-
-    guard->engaged_in_io = false;
 }
 
 static XHCIEPContext *xhci_alloc_epctx(XHCIState *xhci,
@@ -1132,7 +1120,9 @@ static XHCIEPContext *xhci_alloc_epctx(XHCIState *xhci,
     epctx->epid = epid;
 
     QTAILQ_INIT(&epctx->transfers);
-    epctx->kick_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_ep_kick_timer, epctx);
+    epctx->kick_timer = timer_new_ns_guarded(QEMU_CLOCK_VIRTUAL,
+                                             xhci_ep_kick_timer, epctx,
+                                             &xhci->parent.mem_reentrancy_guard);
 
     return epctx;
 }
@@ -3477,7 +3467,9 @@ static void usb_xhci_realize(DeviceState *dev, Error **errp)
     }
 
     usb_xhci_init(xhci);
-    xhci->mfwrap_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, xhci_mfwrap_timer, xhci);
+    xhci->mfwrap_timer = timer_new_ns_guarded(QEMU_CLOCK_VIRTUAL,
+                                              xhci_mfwrap_timer, xhci,
+                                              &xhci->parent.mem_reentrancy_guard);
 
     memory_region_init(&xhci->mem, OBJECT(dev), "xhci", XHCI_LEN_REGS);
     memory_region_init_io(&xhci->mem_cap, OBJECT(dev), &xhci_cap_ops, xhci,
Signature · 1 line
2.43.0
#6 lore raw

PATCH 6/6 hw/net/pcnet: Use timer_new_ns_guarded()

In reply to Peter Maydell

The pcnet device performs DMA from a QEMU timer callback. To avoid the possibility of re-entrancy to the device if the guest arranges for this DMA to hit the device's own memory-mapped registers, use timer_new_ns_guarded() to enable the reentrancy guard while the timer callback is executing.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4748

---
 hw/net/pcnet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/net/pcnet.c +3 -1
index 0bd7d4314e7..cdbd5372d87 100644
@@ -1706,7 +1706,9 @@ void pcnet_common_init(DeviceState *dev, PCNetState *s, NetClientInfo *info)
     int i;
     uint16_t checksum;
 
-    s->poll_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, pcnet_poll_timer, s);
+    s->poll_timer = timer_new_ns_guarded(QEMU_CLOCK_VIRTUAL,
+                                         pcnet_poll_timer, s,
+                                         &dev->mem_reentrancy_guard);
 
     qemu_macaddr_default_if_unset(&s->conf.macaddr);
     s->nic = qemu_new_nic(info, &s->conf, object_get_typename(OBJECT(dev)),
Signature · 1 line
2.43.0