util/qemu-timer: Support reentrancy guard and use in pcnet, hcd-xhci
Subsystems (2)
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
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
@@ -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
@@ -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
Re: [PATCH 1/6] util/qemu-timer: Make timerlist_run_timers() file local
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 42 lines
The
timerlist_run_timers()function is called only from other functions inqemu-timer.c, so it doesn't need to be global. Make it file-local instead.Signed-off-by Peter Maydell <[email protected]>--- include/qemu/timer.h | 10 ---------- util/qemu-timer.c | 10 +++++++++- 2 files changed, 9 insertions(+), 11 deletions(-)include/qemu/timer.h +0
-10@@ -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 +2
-1@@ -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:
util/qemu-timer.c bool timer_expired(const QEMUTimer *timer_head, int64_t current_time) · line 529
Quoted 10 lines
util/qemu-timer.c +7
-0+* @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;
Re: [PATCH 1/6] util/qemu-timer: Make timerlist_run_timers() file local
In reply to Peter MaydellQuoted 6 lines · Peter Maydell
The
timerlist_run_timers()function is called only from other functions inqemu-timer.c, so it doesn't need to be global. Make it file-local instead.Signed-off-by Peter Maydell <[email protected]>---
Quoted 49 lines
include/qemu/timer.h| 10 ----------util/qemu-timer.c| 10 +++++++++-2 files changed, 9 insertions(+), 11 deletions(-)include/qemu/timer.h +0
-10@@ -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@@ -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
PATCH 2/6 util/qemu-timer: Add optional reentrancy guard to the timer API
In reply to Peter MaydellIt'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
@@ -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
@@ -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
@@ -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
Re: [PATCH 2/6] util/qemu-timer: Add optional reentrancy guard to the timer API
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 84 lines
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()orqemu_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 commitf91b0544fe1f("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_ioeven 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.)Signed-off-by Peter Maydell <[email protected]>--- 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@@ -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 +2
-0@@ -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)
util/qemu-timer.c static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) · line 394
Quoted 5 lines
util/qemu-timer.c +2
-0@@ -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)
Would it make sense to pass one single "QEMUTimer *" as parameter to this function instead of the three parameters? I think all callers should have the "QEMUTimer *", so IMHO this would be nicer.
Anyway, either way:
Quoted 65 lines
util/qemu-timer.c +25
-2+{ +/* 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@@ -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"
Re: [PATCH 2/6] util/qemu-timer: Add optional reentrancy guard to the timer API
Quoted 9 lines · Thomas Huth
Am 04.12.26 um 14:19 schrieb Peter Maydell:
util/qemu-timer.c static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) · line 394
Quoted 3 lines
util/qemu-timer.c +2
-0+static void timer_run_callback (QEMUTimerCB *cb,void *opaque, +MemReentrancyGuard *reentrancy_guard)Would it make sense to pass one single "QEMUTimer *" as parameter to this function instead of the three parameters? I think all callers should have the "QEMUTimer *", so IMHO this would be nicer.
We can't do that, for the same reason that timerlist_run_timers() has to copy various fields out of the QEMUTimer* ts that it has into local variables rather than using them in-place -- this function runs with the timer list mutex unlocked, and both other code and the callback itself can modify the timer list and potentially delete the timer.
thanks
-- PMM
Re: [PATCH 2/6] util/qemu-timer: Add optional reentrancy guard to the timer API
Am 04.12.26 um 16:05 schrieb Peter Maydell:
Quoted 11 lines · Thomas Huth
Quoted 9 lines
Am 04.12.26 um 14:19 schrieb Peter Maydell:
util/qemu-timer.c static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) · line 394
Quoted 3 lines
util/qemu-timer.c +2
-0+static void timer_run_callback (QEMUTimerCB *cb,void *opaque, +MemReentrancyGuard *reentrancy_guard)Would it make sense to pass one single "QEMUTimer *" as parameter to this function instead of the three parameters? I think all callers should have the "QEMUTimer *", so IMHO this would be nicer.
Quoted 6 lines
We can't do that, for the same reason that
timerlist_run_timers()has to copy various fields out of the QEMUTimer* ts that it has into local variables rather than using them in-place -- this function runs with the timer list mutex unlocked, and both other code and the callback itself can modify the timer list and potentially delete the timer.
Ah, all right, thanks for the explanation, then the patch is indeed fine as it is!
Thomas
PATCH 3/6 util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full()
In reply to Peter MaydellAdd 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
Re: [PATCH 3/6] util/qemu-timer: Add MemRentrancyGuard to timer_{init,new}_full()
Typo in the subject, there is an "e" missing in MemRentrancyGuard
Am 04.12.26 um 14:19 schrieb Peter Maydell:
...
include/qemu/timer.h int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg); · line 398
Quoted 10 lines
include/qemu/timer.h +2
-0@@ -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.
Again, one "e" missing in "device-rentrancy".
Apart from those two nits, patch looks fine to me:
Re: [PATCH 3/6] util/qemu-timer: Add MemRentrancyGuard to timer_{init, new}_full()
In reply to Peter MaydellQuoted 6 lines · Peter Maydell
Add a MemReentrancyGuard parameter to
timer_init_full()andtimer_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 functiontimer_init_full_guarded().
Quoted 7 lines
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 overtimer_init_full().Signed-off-by Peter Maydell <[email protected]>---
The rust part is fine ("if it compiles it works")
For the patch:
Quoted 210 lines
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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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
PATCH 4/6 util/qemu_timer: Add timer_new_ns_guarded()
In reply to Peter MaydellAdd 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
@@ -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
Re: [PATCH 4/6] util/qemu_timer: Add timer_new_ns_guarded()
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 8 lines
Add a
timer_new_ns_guarded()function which is the same astimer_new_ns()but takes a MemReentrancyGuard.There are a lot of different sub-flavours of
timer_new()andtimer_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.
"... to make use *of* it" ? (I'm not a native speaker, though, so I might be wrong)
Quoted 11 lines
Signed-off-by Peter Maydell <[email protected]>--- include/qemu/timer.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)include/qemu/timer.h +0
-0
include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 570
Quoted 9 lines
include/qemu/timer.h +6
-0@@ -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
Should be only "guard" instead of "reentrancy_guard" to match the parameter list below.
include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 571
Quoted 1 line
include/qemu/timer.h +1
-0+* device-rentrancy issues
Missing "e" again in "rentrancy".
Quoted 8 lines
include/qemu/timer.h +8
-0+* 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.
include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 590
Quoted 10 lines
include/qemu/timer.h +10
-0+* +* 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); +}
With the doc comment fixed:
Re: [PATCH 4/6] util/qemu_timer: Add timer_new_ns_guarded()
Quoted 5 lines · Thomas Huth
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 3 lines
Add a
timer_new_ns_guarded()function which is the same astimer_new_ns()but takes a MemReentrancyGuard.
Quoted 8 lines
Quoted 5 lines
There are a lot of different sub-flavours of
timer_new()andtimer_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."... to make use *of* it" ? (I'm not a native speaker, though, so I might be wrong)
This is correct, though on reread perhaps a bit confusing. It's "we plan to make the two devices use it" but rearranged so that "the two devices" is the head of a noun phrase and the rest is its subordinate defining clause.
Maybe better just to simplify to
"start with just the one we need right now".
Quoted 24 lines
Quoted 11 lines
Signed-off-by Peter Maydell <[email protected]>--- include/qemu/timer.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)include/qemu/timer.h +0
-0include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 570
Quoted 9 lines
include/qemu/timer.h +6
-0@@ -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 Should be only "guard" instead of "
reentrancy_guard" to match the parameter list below.
Quoted 3 lines
include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 571
Quoted 1 line
include/qemu/timer.h +1
-0+* device-rentrancy issues Missing "e" again in "rentrancy".
Wow, I apparently really love this typo :-)
Quoted 20 lines
Quoted 8 lines
include/qemu/timer.h +8
-0+* 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. include/qemu/timer.h static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb, · line 590
Quoted 10 lines
include/qemu/timer.h +10
-0+* +* 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); +}With the doc comment fixed:
Quoted 1 line
Reviewed-by Thomas Huth <[email protected]>
thanks
-- PMM
PATCH 5/6 hw/usb/hcd-xhci: Use timer reentrancy-guard API
In reply to Peter MaydellIn 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
@@ -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
Re: [PATCH 5/6] hw/usb/hcd-xhci: Use timer reentrancy-guard API
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 10 lines
In commit
f91b0544fe1("hw/usb/hcd-xhci: Set reentrancy guard in timer functions") we manually applied a reentrancy guard in thexhci_mfwrap_timer()andxhci_ep_kick_timer()timer callback functions. Now that the timer API supports automatically applying a guard when it calls a callback, use that instead.Signed-off-by Peter Maydell <[email protected]>--- hw/usb/hcd-xhci.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-)
PATCH 6/6 hw/net/pcnet: Use timer_new_ns_guarded()
In reply to Peter MaydellThe 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
@@ -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
Re: [PATCH 6/6] hw/net/pcnet: Use timer_new_ns_guarded()
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 18 lines
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/4748Signed-off-by Peter Maydell <[email protected]>--- hw/net/pcnet.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)hw/net/pcnet.c +0
-0
hw/net/pcnet.c void pcnet_common_init(DeviceState *dev, PCNetState *s, NetClientInfo *info) · line 1714
Quoted 10 lines
hw/net/pcnet.c +3
-1@@ -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)),
Re: [PATCH 6/6] hw/net/pcnet: Use timer_new_ns_guarded()
In reply to Peter MaydellQuoted 10 lines · 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/4748Signed-off-by Peter Maydell <[email protected]>---
Quoted 21 lines
hw/net/pcnet.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)hw/net/pcnet.c +3
-1@@ -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
Am 04.12.26 um 14:19 schrieb Peter Maydell:
Quoted 27 lines
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()orqemu_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 commitf91b0544fe1f("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.)
Quoted 4 lines
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.
Thanks a lot for tackling this! Patch series looks good (apart from some minor nits), and I also think this is the right way to go forward with these timer-reentrancy bugs!
Quoted 7 lines
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?
I'd say, let's start small, just with what is done in this patch series. If we later get bug reports that we can fix with additional QOM references, we still can add it easily on top since we now have everything properly wrapper in the timer API.
Thomas