Skip to contentSkip to conversation

rcu: Detect accidental reuse of rcu head

PATCH In review Peter Xu 3 participants 6 messages Oct 14, 2025 – Oct 15, 2025

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v2 cover rcu: Detect accidental reuse of rcu head

v2:
- Change patch 2 subject and wording to avoid mentioning "double free" [PeterM]

This is an idea suggested by Peter Maydell:

https://lore.kernel.org/all/CAFEAcA--K0=EJNNvj98i=ewGY=tN3u4S0+fNb9kJpYynzjHEhw@mail.gmail.com/

After this small series applied, we should be able to assert on misuse of accidental (wrong) re-use on a rcu head.

Please review, thanks.

Peter Xu (3):
qht: Zero-initialize qht_map tests/test-rcu-*: Zero-initialize allocated elements
rcu: Catch rcu_head misuse

 tests/unit/test-rcu-list.c | 4 ++--
 util/qht.c                 | 2 +-
 util/rcu.c                 | 5 +++++
 3 files changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.50.1
author
#1 lore raw

PATCH v2 1/3 qht: Zero-initialize qht_map

QEMU almost always do this across the tree except QHT. This prepares for rcu to track double free.

---
 util/qht.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/qht.c +1 -1
index 208c2f4b32..872d9f9cec 100644
@@ -441,7 +441,7 @@ static struct qht_map *qht_map_create(size_t n_buckets)
     struct qht_map *map;
     size_t i;
 
-    map = g_malloc(sizeof(*map));
+    map = g_malloc0(sizeof(*map));
     map->n_buckets = n_buckets;
 
     map->n_added_buckets = 0;
Signature · 1 line
2.50.1
author
#3 lore raw

PATCH v2 2/3 tests/test-rcu-*: Zero-initialize allocated elements

In reply to Peter Xu

QEMU almost always do this, so do it too with the RCU unit tests. It's preparation work to start asserting on possible rcu double free.

---
 tests/unit/test-rcu-list.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/unit/test-rcu-list.c +2 -2
index 8f0adb8b00..853c9e33d9 100644
@@ -233,7 +233,7 @@ static void *rcu_q_updater(void *arg)
         TEST_LIST_FOREACH_RCU(el, &Q_list_head, entry) {
             j++;
             if (target_el == j) {
-                struct list_element *new_el = g_new(struct list_element, 1);
+                struct list_element *new_el = g_new0(struct list_element, 1);
                 n_nodes_local++;
                 TEST_LIST_INSERT_AFTER_RCU(el, new_el, entry);
                 break;
@@ -259,7 +259,7 @@ static void rcu_qtest_init(void)
     nthreadsrunning = 0;
     srand(time(0));
     for (i = 0; i < RCU_Q_LEN; i++) {
-        new_el = g_new(struct list_element, 1);
+        new_el = g_new0(struct list_element, 1);
         TEST_LIST_INSERT_HEAD_RCU(&Q_list_head, new_el, entry);
     }
     qemu_mutex_lock(&counts_mutex);
Signature · 1 line
2.50.1
author
#4 lore raw

PATCH v2 3/3 rcu: Catch rcu_head misuse

In reply to Peter Xu

Trap call_rcu1() to make sure it won't be invoked twice for one rcu head by accident.

---
 util/rcu.c | 5 +++++
 1 file changed, 5 insertions(+)
util/rcu.c +5 -0
index b703c86f15..fc4b18812b 100644
@@ -307,6 +307,11 @@ static void *call_rcu_thread(void *opaque)
 
 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
 {
+    /*
+     * Avoid accidental reuse of rcu_head, e.g. enqueuing one node twice
+     * (especially, when the function pointers are different).
+     */
+    assert(node->func == NULL);
     node->func = func;
     enqueue(node);
     qatomic_inc(&rcu_call_count);
Signature · 1 line
2.50.1