Skip to contentSkip to conversation

Revert "rcu: Unify force quiescent state"

PATCH In review Thomas Huth 4 participants 7 messages Feb 9 – Feb 16

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Revert "rcu: Unify force quiescent state"

From: Thomas Huth <[email protected]>

This reverts commit 55d98e3edeeb17dd8445db27605d2b34f4c3ba85.

The commit introduced a regression in the replay functional test on alpha (tests/functional/alpha/test_replay.py), that causes CI failures regularly. Thus revert this change until someone has figured out what is going wrong here.

---
I just hit this again in the CI:
https://gitlab.com/thuth/qemu/-/jobs/13018713844
... it's annoying, so I'd like to suggest to revert the offending patch 'til someone fixed this.

 util/rcu.c | 79 +++++++++++++++++++-----------------------------------
 1 file changed, 28 insertions(+), 51 deletions(-)
util/rcu.c +28 -51
index acac9446ea9..b703c86f15a 100644
@@ -43,14 +43,10 @@
 #define RCU_GP_LOCKED           (1UL << 0)
 #define RCU_GP_CTR              (1UL << 1)
 
-
-#define RCU_CALL_MIN_SIZE        30
-
 unsigned long rcu_gp_ctr = RCU_GP_LOCKED;
 
 QemuEvent rcu_gp_event;
 static int in_drain_call_rcu;
-static int rcu_call_count;
 static QemuMutex rcu_registry_lock;
 static QemuMutex rcu_sync_lock;
 
@@ -80,29 +76,15 @@ static void wait_for_readers(void)
 {
     ThreadList qsreaders = QLIST_HEAD_INITIALIZER(qsreaders);
     struct rcu_reader_data *index, *tmp;
-    int sleeps = 0;
-    bool forced = false;
 
     for (;;) {
-        /*
-         * Force the grace period to end and wait for it if any of the
-         * following heuristical conditions are satisfied:
-         * - A decent number of callbacks piled up.
-         * - It timed out.
-         * - It is in a drain_call_rcu() call.
-         *
-         * Otherwise, periodically poll the grace period, hoping it ends
-         * promptly.
+        /* We want to be notified of changes made to rcu_gp_ongoing
+         * while we walk the list.
          */
-        if (!forced &&
-            (qatomic_read(&rcu_call_count) >= RCU_CALL_MIN_SIZE ||
-             sleeps >= 5 || qatomic_read(&in_drain_call_rcu))) {
-            forced = true;
+        qemu_event_reset(&rcu_gp_event);
 
-            QLIST_FOREACH(index, &registry, node) {
-                notifier_list_notify(&index->force_rcu, NULL);
-                qatomic_set(&index->waiting, true);
-            }
+        QLIST_FOREACH(index, &registry, node) {
+            qatomic_set(&index->waiting, true);
         }
 
         /* Here, order the stores to index->waiting before the loads of
@@ -124,6 +106,8 @@ static void wait_for_readers(void)
                  * get some extra futex wakeups.
                  */
                 qatomic_set(&index->waiting, false);
+            } else if (qatomic_read(&in_drain_call_rcu)) {
+                notifier_list_notify(&index->force_rcu, NULL);
             }
         }
 
@@ -131,8 +115,7 @@ static void wait_for_readers(void)
             break;
         }
 
-        /*
-         * Sleep for a while and try again.
+        /* Wait for one thread to report a quiescent state and try again.
          * Release rcu_registry_lock, so rcu_(un)register_thread() doesn't
          * wait too much time.
          *
@@ -150,20 +133,7 @@ static void wait_for_readers(void)
          * rcu_registry_lock is released.
          */
         qemu_mutex_unlock(&rcu_registry_lock);
-
-        if (forced) {
-            qemu_event_wait(&rcu_gp_event);
-
-            /*
-             * We want to be notified of changes made to rcu_gp_ongoing
-             * while we walk the list.
-             */
-            qemu_event_reset(&rcu_gp_event);
-        } else {
-            g_usleep(10000);
-            sleeps++;
-        }
-
+        qemu_event_wait(&rcu_gp_event);
         qemu_mutex_lock(&rcu_registry_lock);
     }
 
@@ -203,11 +173,15 @@ void synchronize_rcu(void)
     }
 }
 
+
+#define RCU_CALL_MIN_SIZE        30
+
 /* Multi-producer, single-consumer queue based on urcu/static/wfqueue.h
  * from liburcu.  Note that head is only used by the consumer.
  */
 static struct rcu_head dummy;
 static struct rcu_head *head = &dummy, **tail = &dummy.next;
+static int rcu_call_count;
 static QemuEvent rcu_call_ready_event;
 
 static void enqueue(struct rcu_head *node)
@@ -285,27 +259,30 @@ static void *call_rcu_thread(void *opaque)
     rcu_register_thread();
 
     for (;;) {
-        int n;
+        int tries = 0;
+        int n = qatomic_read(&rcu_call_count);
 
-        /*
+        /* Heuristically wait for a decent number of callbacks to pile up.
          * Fetch rcu_call_count now, we only must process elements that were
          * added before synchronize_rcu() starts.
          */
-        for (;;) {
-            qemu_event_reset(&rcu_call_ready_event);
-            n = qatomic_read(&rcu_call_count);
-            if (n) {
-                break;
-            }
-
+        while (n == 0 || (n < RCU_CALL_MIN_SIZE && ++tries <= 5)) {
+            g_usleep(10000);
+            if (n == 0) {
+                qemu_event_reset(&rcu_call_ready_event);
+                n = qatomic_read(&rcu_call_count);
+                if (n == 0) {
 #if defined(CONFIG_MALLOC_TRIM)
-            malloc_trim(4 * 1024 * 1024);
+                    malloc_trim(4 * 1024 * 1024);
 #endif
-            qemu_event_wait(&rcu_call_ready_event);
+                    qemu_event_wait(&rcu_call_ready_event);
+                }
+            }
+            n = qatomic_read(&rcu_call_count);
         }
 
-        synchronize_rcu();
         qatomic_sub(&rcu_call_count, n);
+        synchronize_rcu();
         bql_lock();
         while (n > 0) {
             node = try_dequeue();
Signature · 1 line
2.52.0
#1 lore raw
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

This reverts commit 55d98e3edeeb17dd8445db27605d2b34f4c3ba85.

The commit introduced a regression in the replay functional test on alpha (tests/functional/alpha/test_replay.py), that causes CI failures regularly. Thus revert this change until someone has figured out what is going wrong here.

Quoted 9 lines

---
I just hit this again in the CI:
https://gitlab.com/thuth/qemu/-/jobs/13018713844
... it's annoying, so I'd like to suggest to revert the offending patch 'til someone fixed this.

Hi Thomas,

I am actively investigating this problem, but haven't made any important progress so far. It's looking like a timing bug which is annoying to track down. All I know for certain at the moment is that faking some of the values returned by qemu_clock_get_ns to be deterministic improves the failure rate, but does not remove it entirely. I'm fine with this being reverted for now; we can add it back in once we figure out what the true cause is.

Jim

Reviewed-by
#2 lore raw
Quoted 19 lines · Jim MacArthur
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

This reverts commit 55d98e3edeeb17dd8445db27605d2b34f4c3ba85.

The commit introduced a regression in the replay functional test on alpha (tests/functional/alpha/test_replay.py), that causes CI failures regularly. Thus revert this change until someone has figured out what is going wrong here.

Quoted 8 lines

---
  I just hit this again in the CI:
  https://gitlab.com/thuth/qemu/-/jobs/13018713844
  ... it's annoying, so I'd like to suggest to revert the offending patch 'til someone fixed this.

Quoted 3 lines

Hi Thomas,

I am actively investigating this problem, but haven't made any important progress so far. It's looking like a timing bug which is annoying to track down. All I know for certain at the moment is that faking some of the values returned by qemu_clock_get_ns to be deterministic improves the failure rate, but does not remove it entirely. I'm fine with this being reverted for now; we can add it back in once we figure out what the true cause is.

Thanks for investigating this. Let's have the commit reverted until there is some progress:

#3 lore raw

+Ilya (author of mentioned commit)

Hi, all

Quoted 20 lines · Akihiko Odaki
Quoted 19 lines · Jim MacArthur
Quoted 17 lines · Thomas Huth

From: Thomas Huth <[email protected]>

This reverts commit 55d98e3edeeb17dd8445db27605d2b34f4c3ba85.

The commit introduced a regression in the replay functional test on alpha (tests/functional/alpha/test_replay.py), that causes CI failures regularly. Thus revert this change until someone has figured out what is going wrong here.

---
  I just hit this again in the CI:
  https://gitlab.com/thuth/qemu/-/jobs/13018713844
  ... it's annoying, so I'd like to suggest to revert the offending patch 'til someone fixed this.

Quoted 8 lines
Quoted 3 lines

Hi Thomas,

I am actively investigating this problem, but haven't made any important progress so far. It's looking like a timing bug which is annoying to track down. All I know for certain at the moment is that faking some of the values returned by qemu_clock_get_ns to be deterministic improves the failure rate, but does not remove it entirely. I'm fine with this being reverted for now; we can add it back in once we figure out what the true cause is.

Thanks for investigating this. Let's have the commit reverted until there is some progress:

After the quiescence commit, timeouts were the only regression, until f098c32db48 ("target/s390x: Fix infinite loop during replay") when the replay test began failing with error, too; maybe this serves as a diagnostic trigger?

Yodel

#4 lore raw

+rth (maintainer: accel/tcg/tcg-accel-ops-rr.c)

Hi,

Quoted 34 lines · Yodel Eldar

+Ilya (author of mentioned commit)

Hi, all

Quoted 20 lines · Akihiko Odaki
Quoted 19 lines · Jim MacArthur
Quoted 17 lines · Thomas Huth

From: Thomas Huth <[email protected]>

This reverts commit 55d98e3edeeb17dd8445db27605d2b34f4c3ba85.

The commit introduced a regression in the replay functional test on alpha (tests/functional/alpha/test_replay.py), that causes CI failures regularly. Thus revert this change until someone has figured out what is going wrong here.

---
  I just hit this again in the CI:
  https://gitlab.com/thuth/qemu/-/jobs/13018713844
  ... it's annoying, so I'd like to suggest to revert the offending patch 'til someone fixed this.

Quoted 8 lines
Quoted 3 lines

Hi Thomas,

I am actively investigating this problem, but haven't made any important progress so far. It's looking like a timing bug which is annoying to track down. All I know for certain at the moment is that faking some of the values returned by qemu_clock_get_ns to be deterministic improves the failure rate, but does not remove it entirely. I'm fine with this being reverted for now; we can add it back in once we figure out what the true cause is.

Thanks for investigating this. Let's have the commit reverted until there is some progress:

Quoted 7 lines

After the quiescence commit, timeouts were the only regression, until f098c32db48 ("target/s390x: Fix infinite loop during replay") when the replay test began failing with error, too; maybe this serves as a diagnostic trigger?

Yodel

Seems like the force_rcu notifier in rr tcg became incompatible with record/replay after Akihiko's patch; preventing registration of the notifier when in record/replay mode appears to fix the problem:

accel/tcg/tcg-accel-ops-rr.c +5 -3
index f84342e044..2073b1b13e 100644
@@ -179,13 +179,15 @@ static int rr_cpu_count(void)
 
 static void *rr_cpu_thread_fn(void *arg)
 {
-    Notifier force_rcu;
     CPUState *cpu = arg;
 
     assert(tcg_enabled());
     rcu_register_thread();
-    force_rcu.notify = rr_force_rcu;
-    rcu_add_force_rcu_notifier(&force_rcu);
+    if (replay_mode == REPLAY_MODE_NONE) {
+        Notifier force_rcu;
+        force_rcu.notify = rr_force_rcu;
+        rcu_add_force_rcu_notifier(&force_rcu);
+    }
     tcg_register_thread();
 
     bql_lock();

as confirmed by running:

    meson test --setup thorough --no-rebuild -t 0 \
       --num-processes 1 --repeat 100 func-alpha-replay

I'd like to do some additional testing/analysis to make sure this doesn't just trade one bug for another as I continue to study the internals of RCU and replay/record, but any comments regarding the above fix would be much appreciated.

E.g., could we continue using a force_rcu notifier with record/replay if we added an async replay event for it, or by some other means? Should we?

Furthermore, mttcg also uses a force_rcu notifier, but record/replay isn't supported with multiple CPUs, so are we really in the clear there?

Also, the issue that firstly motivated rr_force_rcu():

    https://gitlab.com/qemu-project/qemu/-/issues/650

doesn't appear at first glance to occur in record/replay mode when the notifier isn't registered during record/replay; is that thanks to Akihiko's patch or was that never a problem in record/replay mode? Side note: eliminating the force_rcu notifier for non-record/replay mode does cause the same regression as in issue #650; i.e., rr_force_rcu is still seemingly necessary outside of replay/record mode.

Here's the backtrace of qemu-system-alpha as it hangs in replay mode:
------------------------------------------------------------------------
Thread 4 (Thread 0x7f2b841ff6c0 (LWP 7653) "qemu-system-alp"):

#0  0x00007f2b88c0906d in syscall () at /usr/lib/libc.so.6
#1  0x000055692672e554 in qemu_futex_wait (f=0x556926ca6670 <rcu_call_ready_event>, val=0xffffffff) at /qemu/include/qemu/futex.h:47
#2  0x000055692672e768 in qemu_event_wait (ev=0x556926ca6670 <rcu_call_ready_event>) at ../util/event.c:162
#3  0x000055692673a87e in call_rcu_thread (opaque=0x0) at ../util/rcu.c:304
#4  0x000055692672d032 in qemu_thread_start (args=0x556965cbd7b0) at ../util/qemu-thread-posix.c:393
#5  0x00007f2b88b8797a in ??? () at /usr/lib/libc.so.6
#6  0x00007f2b88c0b2bc in ??? () at /usr/lib/libc.so.6

Thread 3 (Thread 0x7f2b831fe6c0 (LWP 7654) "qemu-system-alp"):

#0  0x00007f2b88b8ff32 in ??? () at /usr/lib/libc.so.6
#1  0x00007f2b88b8439c in ??? () at /usr/lib/libc.so.6
#2  0x00007f2b88b843e4 in ??? () at /usr/lib/libc.so.6
#3  0x00007f2b88bfe2f6 in ppoll () at /usr/lib/libc.so.6
#4  0x00007f2b8a690784 in ??? () at /usr/lib/libglib-2.0.so.0
#5  0x00007f2b8a690a17 in g_main_loop_run () at /usr/lib/libglib-2.0.so.0
#6  0x0000556926565683 in iothread_run (opaque=0x556965d17c70) at ../iothread.c:70
#7  0x000055692672d032 in qemu_thread_start (args=0x556965cf46e0) at ../util/qemu-thread-posix.c:393
#8  0x00007f2b88b8797a in ??? () at /usr/lib/libc.so.6
#9  0x00007f2b88c0b2bc in ??? () at /usr/lib/libc.so.6

Thread 2 (Thread 0x7f2b821fd6c0 (LWP 7655) "qemu-system-alp"):

#0  0x00007f2b88b8ff32 in ??? () at /usr/lib/libc.so.6
#1  0x00007f2b88b8439c in ??? () at /usr/lib/libc.so.6
#2  0x00007f2b88b8468c in ??? () at /usr/lib/libc.so.6
#3  0x00007f2b88b86e5e in pthread_cond_wait () at /usr/lib/libc.so.6
#4  0x000055692672c9ee in qemu_cond_wait_impl (cond=0x556965e6df40, mutex=0x556926c821e0 <bql>, file=0x55692683204f "../system/cpus.c", line=0x24e) at ../util/qemu-thread-posix.c:225
#5  0x000055692630728b in qemu_cond_wait_bql (cond=0x556965e6df40) at ../system/cpus.c:590
#6  0x000055692643b864 in rr_wait_io_event () at ../accel/tcg/tcg-accel-ops-rr.c:114
#7  0x000055692643bcc5 in rr_cpu_thread_fn (arg=0x556965e69aa0) at ../accel/tcg/tcg-accel-ops-rr.c:235
#8  0x000055692672d032 in qemu_thread_start (args=0x556965cf2ea0) at ../util/qemu-thread-posix.c:393
#9  0x00007f2b88b8797a in ??? () at /usr/lib/libc.so.6

#10 0x00007f2b88c0b2bc in ??? () at /usr/lib/libc.so.6

Thread 1 (Thread 0x7f2b8690f180 (LWP 7652) "qemu-system-alp"):

#0  0x00007f2b88b8ff32 in ??? () at /usr/lib/libc.so.6
#1  0x00007f2b88b8439c in ??? () at /usr/lib/libc.so.6
#2  0x00007f2b88b843e4 in ??? () at /usr/lib/libc.so.6
#3  0x00007f2b88bfe2f6 in ppoll () at /usr/lib/libc.so.6
#4  0x000055692674d35a in qemu_poll_ns (fds=0x5569667017a0, nfds=0x5, timeout=0x3b597370) at ../util/qemu-timer.c:343
#5  0x00005569267485ee in os_host_main_loop_wait (timeout=0x3b597370) at ../util/main-loop.c:308
#6  0x0000556926748740 in main_loop_wait (nonblocking=0x0) at ../util/main-loop.c:592
#7  0x0000556926335226 in qemu_main_loop () at ../system/runstate.c:903
#8  0x000055692665e8cf in qemu_default_main (opaque=0x0) at ../system/main.c:50
#9  0x000055692665e989 in main (argc=0x19, argv=0x7ffdb6f6f898) at ../system/main.c:93

------------------------------------------------------------------------

Interestingly, the shift value used in the alpha replay test's -icount option somewhat sneakily complicated debugging: When shift is greater than eight (it's currently nine), both TIMEOUTs and ERRORs would occur in meson test ... func-alpha-replay (after f098c32db48), but when shift is lowered, only TIMEOUTs occur. With my fix, neither occurs regardless of the shift value, but the kernel still seems to mind as seen when given an initrd [1]:

    ./qemu-system-alpha -initrd ./initrd.gz \
        -machine clipper \
        -kernel ./vmlinuz \
        -append 'console=ttyS0' \
        -icount shift=9,rr=record,rrfile=replay.bin \
        -nographic -net none

yielding this printk:

    BUG: soft lockup - CPU#0 stuck for 61s! [udevd:219]

Is there an important reason why we set shift=9 in the test? If not, maybe we should lower it, too, while we're at it?

Please let me know if there's any other information that would be helpful with resolving this bug in the best way possible once and for all; I did quite a bit of testing/debugging over the last couple of days and would be happy to share more of the results/methodology.

Thomas and Akihiko, if it's alright with you, could we delay the PR of the revert at least until we sort this out? I realize the erratic timeouts are gumming up the CI, but maybe we could reduce the deltas of the git history in short order if it's not too much trouble? I can submit the patch in one to two days; I just want to make sure it's done correctly.

[1] https://archive.debian.org/debian/dists/lenny/main/installer-alpha/20090123lenny10/images/cdrom/initrd.gz

Thanks,
Yodel

#5 lore raw

On 16/02/2026 02.12, Yodel Eldar wrote:
...

Quoted 6 lines

Thomas and Akihiko, if it's alright with you, could we delay the PR of the revert at least until we sort this out? I realize the erratic timeouts are gumming up the CI, but maybe we could reduce the deltas of the git history in short order if it's not too much trouble? I can submit the patch in one to two days; I just want to make sure it's done correctly.

Hi!

Sorry, but it has already been reverted:

https://gitlab.com/qemu-project/qemu/-/commit/ddb4d9d1748681cfde824d765af6

... but that should not be a big deal, simply submit the patch again once you've got a proper fix for the problem.

Regards,
Thomas

#6 lore raw
Quoted 13 lines · Thomas Huth

On 16/02/2026 02.12, Yodel Eldar wrote:
...

Quoted 6 lines

Thomas and Akihiko, if it's alright with you, could we delay the PR of the revert at least until we sort this out? I realize the erratic timeouts are gumming up the CI, but maybe we could reduce the deltas of the git history in short order if it's not too much trouble? I can submit the patch in one to two days; I just want to make sure it's done correctly.

 Hi!

Sorry, but it has already been reverted:

Quoted 9 lines

 https://gitlab.com/qemu-project/qemu/-/commit/
ddb4d9d1748681cfde824d765af6

... but that should not be a big deal, simply submit the patch again once you've got a proper fix for the problem.

 Regards,
  Thomas

Ah, it was already part of that set! Thanks for letting me know. I suppose that softens the sense of urgency with the fix; nonetheless, I'll aim for sooner rather than later.

I'm inclined to squash the fix into the re-introduction (especially when the fix is small in scope), or should they be separate commits since the original patch and the fix touch different subsystems? For the latter, I think the fix can precede the re-introductory commit (as suggested by a quick cherry-pick and replay test run), so as to avoid poisoning bisectability.

Yodel