dirtylimit improvement
From: Hyman Huang <[email protected]>
The following changes since commit de5d8bfd6105d3dd3ae668df9762df244a6d1506:
Merge tag 'pull-loongarch-20260609' of https://github.com/gaosong715/qemu into staging (2026-06-09 14:00:28 -0400)
are available in the Git repository at:
https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request-20260611
for you to fetch changes up to 3b224b1c7df1ddefd6bca34f201264d72846c397:
accel/kvm: event-driven wakeup for dirty ring reaper thread (2026-06-11 08:22:50 +0800)
----------------------------------------------------------------
dirtylimit: refactor dirty ring reaper thread with event-driven wakeup
----------------------------------------------------------------
Bin Guo (1):
accel/kvm: event-driven wakeup for dirty ring reaper thread
accel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
[PULL 1/1] accel/kvm: event-driven wakeup for dirty ring reaper thread
From: Bin Guo <[email protected]>
The reaper polls with sleep(1) (TODO in the code) and only notices dirty-limit teardown at the next 1s tick.
Replace the sleep with qemu_poll_ns() on an EventNotifier, kicked from dirtylimit_state_finalize() after dirtylimit_state has been cleared. The kick must follow the NULL assignment: kicking earlier wakes the reaper while dirtylimit_in_service() still returns true, so it just loops back to wait. A 1s fallback timeout remains as a liveness backstop.
20 set-/cancel-vcpu-dirty-limit cycles via QMP, |reaper-wake - cancel-ack| measured with strace on the reaper TID:
before: median 255 ms, max 502 ms
after: median 0.6 ms, max 27 ms
kvm_dirty_ring_reaper_init() returns int again (was made void in commit 43a5e377f4) to propagate event_notifier_init() failure.
Message-Id: <[email protected]>
--- accel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)
accel/kvm/kvm-all.c +36 -6
@@ -1754,6 +1754,8 @@ static void kvm_set_phys_mem(KVMMemoryListener *kml, }while (size); } +#define KVM_DIRTY_RING_REAPER_FALLBACK_NS (1 * NANOSECONDS_PER_SECOND) +static void *kvm_dirty_ring_reaper_thread (void *data) {KVMState *s = data;
@@ -1764,12 +1766,18 @@ static void *kvm_dirty_ring_reaper_thread(void *data)trace_kvm_dirty_ring_reaper ("init" );while (true) { +GPollFD pfd = { + .fd =event_notifier_get_fd (&r->reaper_notifier), + .events =G_IO_IN , + }; + r->reaper_state =KVM_DIRTY_RING_REAPER_WAIT ;trace_kvm_dirty_ring_reaper ("wait" ); -/* -* TODO: provide a smarter timeout rather than a constant? -*/ -sleep (1 ); + +qemu_poll_ns (&pfd,1 ,KVM_DIRTY_RING_REAPER_FALLBACK_NS ); + +/* Drain unconditionally so a stale event can't spin the next loop. */ +event_notifier_test_and_clear (&r->reaper_notifier);/* keep sleeping so that dirtylimit not be interfered by reaper */ if (dirtylimit_in_service ()) {
@@ -1789,13 +1797,32 @@ static void *kvm_dirty_ring_reaper_thread(void *data)g_assert_not_reached (); } -static void kvm_dirty_ring_reaper_init (KVMState *s) +static int kvm_dirty_ring_reaper_init (KVMState *s) {struct KVMDirtyRingReaper *r = &s->reaper; +int ret; + + ret =event_notifier_init (&r->reaper_notifier,0 ); +if (ret <0 ) { +error_report ("Failed to initialize dirty ring reaper notifier: %s" , +strerror (-ret)); +return ret; + }qemu_thread_create (&r->reaper_thr,"kvm-reaper" , kvm_dirty_ring_reaper_thread, s,QEMU_THREAD_JOINABLE ); +return 0 ; +} + +void kvm_dirty_ring_reaper_kick (void ) +{ +KVMState *s = kvm_state; + +if (!s || !s->kvm_dirty_ring_size) { +return ; + } +event_notifier_set (&s->reaper.reaper_notifier); }static int kvm_dirty_ring_init (KVMState *s)
@@ -3097,7 +3124,10 @@ static int kvm_init(AccelState *as, MachineState *ms) }if (s->kvm_dirty_ring_size) { -kvm_dirty_ring_reaper_init (s); + ret =kvm_dirty_ring_reaper_init (s); +if (ret <0 ) { + goto err; + } }if (kvm_check_extension (kvm_state,KVM_CAP_BINARY_STATS_FD )) {
accel/stubs/kvm-stub.c +4 -0
@@ -134,6 +134,10 @@ uint32_t kvm_dirty_ring_size(void)return 0 ; } +void kvm_dirty_ring_reaper_kick (void ) +{ +} +bool kvm_hwpoisoned_mem (void ) {return false;
include/system/kvm.h +7 -0
@@ -553,6 +553,13 @@ bool kvm_dirty_ring_enabled(void);uint32_t kvm_dirty_ring_size (void ); +/** +* kvm_dirty_ring_reaper_kick - wake the background dirty ring reaper. +* +* Safe from any thread; no-op when the dirty ring is not in use. +*/ +void kvm_dirty_ring_reaper_kick (void ); +void kvm_mark_guest_state_protected (void );/**
include/system/kvm_int.h +3 -0
@@ -12,6 +12,7 @@#include "system/memory.h" #include "qapi/qapi-types-common.h" #include "qemu/accel.h" +#include "qemu/event_notifier.h" #include "qemu/queue.h" #include "system/kvm.h" #include "accel/accel-ops.h"
@@ -100,6 +101,8 @@ struct KVMDirtyRingReaper {QemuThread reaper_thr;volatile uint64_t reaper_iteration;/* iteration number of reaper thr */ volatile enum KVMDirtyRingReaperState reaper_state;/* reap thr state */ +/* Wakeup channel: kicked when dirty-limit is torn down. */ +EventNotifier reaper_notifier; };struct KVMState {
system/dirtylimit.c +2 -0
@@ -222,6 +222,8 @@ void dirtylimit_state_finalize(void)g_free (dirtylimit_state); dirtylimit_state =NULL ; +kvm_dirty_ring_reaper_kick (); +trace_dirtylimit_state_finalize (); }
Signature · 1 line
2.43.0
Quoted 2 lines · On Wed, Jun 10, 2026 at 9:15 PM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Hi Hyman Huang,
Is this pull request intended for qemu.git/master?
I did not see any previous pull requests from you in QEMU's git log. It looks like your previous dirtylimit work went through the migration tree. Do you intend to start sending pull requests now and are the migration maintainers aware?
Please follow the pull request process and sign your pull request with your GPG key:
https://www.qemu.org/docs/master/devel/submitting-a-pull-request.html
Thanks,
Stefan
Quoted 31 lines
The following changes since commit
de5d8bfd6105d3dd3ae668df9762df244a6d1506:Merge tag 'pull-loongarch-20260609' of https://github.com/gaosong715/qemu into staging (2026-06-09 14:00:28 -0400)
are available in the Git repository at:
https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request-20260611
for you to fetch changes up to
3b224b1c7df1ddefd6bca34f201264d72846c397:accel/kvm: event-driven wakeup for dirty ring reaper thread (2026-06-11 08:22:50 +0800)
----------------------------------------------------------------
dirtylimit: refactor dirty ring reaper thread with event-driven wakeup----------------------------------------------------------------
Bin Guo (1):
accel/kvm: event-driven wakeup for dirty ring reaper threadaccel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)--
2.43.0
Quoted 11 lines · Stefan Hajnoczi
Quoted 2 lines · On Wed, Jun 10, 2026 at 9:15 PM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Hi Hyman Huang,
Is this pull request intended for qemu.git/master?I did not see any previous pull requests from you in QEMU's git log. It looks like your previous dirtylimit work went through the migration tree. Do you intend to start sending pull requests now and are the migration maintainers aware?
We're not..
Since dirtylimit is almost only used with migration, Hyman, do you want us to keep sending pulls for you? I think it's better we're copied on all these changes at the very minimum.
The other thing about the pull I want to say is, that patch changed most on KVM not dirtylimit: v2 sent on June 4th and planned to be merged today without maintainer's ACK (in this case Paolo, I added into the loop), I think that's a bit too rush. Shall we still at least give Paolo some time in case he wants to say something?
Paolo might be busy and may respond slow, normally what I do is keep pinging if the patch is important; if not, I leave a patch there until it becomes more important.. I suggest we do the same for this patch rather than merging it without Paolo even noticing.
Logically the bare minimum is that patch gets an A-b from Paolo to merge it.
Thanks,
Quoted 40 lines
Please follow the pull request process and sign your pull request with your GPG key:
https://www.qemu.org/docs/master/devel/submitting-a-pull-request.htmlThanks,
StefanQuoted 31 lines
The following changes since commit
de5d8bfd6105d3dd3ae668df9762df244a6d1506:Merge tag 'pull-loongarch-20260609' of https://github.com/gaosong715/qemu into staging (2026-06-09 14:00:28 -0400)
are available in the Git repository at:
https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request-20260611
for you to fetch changes up to
3b224b1c7df1ddefd6bca34f201264d72846c397:accel/kvm: event-driven wakeup for dirty ring reaper thread (2026-06-11 08:22:50 +0800)
----------------------------------------------------------------
dirtylimit: refactor dirty ring reaper thread with event-driven wakeup----------------------------------------------------------------
Bin Guo (1):
accel/kvm: event-driven wakeup for dirty ring reaper threadaccel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)--
2.43.0
Signature · 1 line
Peter Xu
Agreed!
Quoted 80 lines · Peter Xu
Quoted 11 lines · Stefan Hajnoczi
Quoted 2 lines · On Wed, Jun 10, 2026 at 9:15 PM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Hi Hyman Huang,
Is this pull request intended for qemu.git/master?I did not see any previous pull requests from you in QEMU's git log. It looks like your previous dirtylimit work went through the migration tree. Do you intend to start sending pull requests now and are the migration maintainers aware?
We're not..
Since dirtylimit is almost only used with migration, Hyman, do you want us to keep sending pulls for you? I think it's better we're copied on all these changes at the very minimum.
The other thing about the pull I want to say is, that patch changed most on KVM not dirtylimit: v2 sent on June 4th and planned to be merged today without maintainer's ACK (in this case Paolo, I added into the loop), I think that's a bit too rush. Shall we still at least give Paolo some time in case he wants to say something?
Paolo might be busy and may respond slow, normally what I do is keep pinging if the patch is important; if not, I leave a patch there until it becomes more important.. I suggest we do the same for this patch rather than merging it without Paolo even noticing.
Logically the bare minimum is that patch gets an A-b from Paolo to merge it.
Thanks,
Quoted 40 lines
Please follow the pull request process and sign your pull request with your GPG key:
https://www.qemu.org/docs/master/devel/submitting-a-pull-request.htmlThanks,
StefanQuoted 31 lines
The following changes since commit
de5d8bfd6105d3dd3ae668df9762df244a6d1506:Merge tag 'pull-loongarch-20260609' of https://github.com/gaosong715/qemu into staging (2026-06-09 14:00:28 -0400)
are available in the Git repository at:
https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request-20260611
for you to fetch changes up to
3b224b1c7df1ddefd6bca34f201264d72846c397:accel/kvm: event-driven wakeup for dirty ring reaper thread (2026-06-11 08:22:50 +0800)
----------------------------------------------------------------
dirtylimit: refactor dirty ring reaper thread with event-driven wakeup----------------------------------------------------------------
Bin Guo (1):
accel/kvm: event-driven wakeup for dirty ring reaper threadaccel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)--
2.43.0--
Peter Xu
Quoted 13 lines · Stefan Hajnoczi
Quoted 2 lines · On Wed, Jun 10, 2026 at 9:15 PM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Hi Hyman Huang,
Is this pull request intended for qemu.git/master?I did not see any previous pull requests from you in QEMU's git log. It looks like your previous dirtylimit work went through the migration tree. Do you intend to start sending pull requests now and are the migration maintainers aware?
Quoted 6 lines
Please follow the pull request process and sign your pull request with your GPG key:
https://www.qemu.org/docs/master/devel/submitting-a-pull-request.htmlThanks,
Stefan
Thanks for the heads-up and suggestions. Let's wait for Paolo's reply. :)
Quoted 32 lines
Quoted 32 lines
The following changes since commit
de5d8bfd6105d3dd3ae668df9762df244a6d1506:Merge tag 'pull-loongarch-20260609' of https://github.com/gaosong715/qemu into staging (2026-06-09 14:00:28 -0400)
are available in the Git repository at:
https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request-20260611
for you to fetch changes up to
3b224b1c7df1ddefd6bca34f201264d72846c397:accel/kvm: event-driven wakeup for dirty ring reaper thread (2026-06-11 08:22:50 +0800)
----------------------------------------------------------------
dirtylimit: refactor dirty ring reaper thread with event-driven wakeup----------------------------------------------------------------
Bin Guo (1):
accel/kvm: event-driven wakeup for dirty ring reaper threadaccel/kvm/kvm-all.c | 42 ++++++++++++++++++++++++++++++++++------ accel/stubs/kvm-stub.c | 4 ++++ include/system/kvm.h | 7 +++++++ include/system/kvm_int.h | 3 +++ system/dirtylimit.c | 2 ++ 5 files changed, 52 insertions(+), 6 deletions(-)--
2.43.0