Skip to contentSkip to conversation

rcu: Detect accidental reuse of rcu head

PATCH Superseded Peter Xu 2 participants 6 messages Sep 29, 2025 – Sep 30, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

cover rcu: Detect accidental reuse of rcu head

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: Avoid double rcu frees

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

PATCH 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
#2 lore raw

PATCH 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
#3 lore raw

PATCH 3/3 rcu: Avoid double rcu frees

In reply to Peter Xu

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

---
 util/rcu.c | 2 ++
 1 file changed, 2 insertions(+)
util/rcu.c +2 -0
index b703c86f15..9272fe5796 100644
@@ -307,6 +307,8 @@ static void *call_rcu_thread(void *opaque)
 
 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
 {
+    /* Avoid double rcu frees */
+    assert(node->func == NULL);
     node->func = func;
     enqueue(node);
     qatomic_inc(&rcu_call_count);
Signature · 1 line
2.50.1
#4 lore raw

Re: [PATCH 3/3] rcu: Avoid double rcu frees

Quoted 14 lines · Peter Xu

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

---
 util/rcu.c | 2 ++
 1 file changed, 2 insertions(+)
util/rcu.c +0 -0
index b703c86f15..9272fe5796 100644

util/rcu.c static void *call_rcu_thread(void *opaque) · line 310

Quoted 4 lines
util/rcu.c +1 -0
@@ -307,6 +307,8 @@ static void *call_rcu_thread(void *opaque)
 
 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
 {
+    /* Avoid double rcu frees */

I would maybe have this say "Catch accidental attempts to use the same rcu node for two things at once", which isn't the same as a "double free" in the usual sense.

util/rcu.c static void *call_rcu_thread(void *opaque) · line 314

Quoted 4 lines
util/rcu.c +1 -0
+    assert(node->func == NULL);
     node->func = func;
     enqueue(node);
     qatomic_inc(&rcu_call_count);

thanks
-- PMM

author
#5 lore raw

Re: [PATCH 3/3] rcu: Avoid double rcu frees

Quoted 16 lines · Peter Maydell
Quoted 15 lines · Peter Xu

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

---
 util/rcu.c | 2 ++
 1 file changed, 2 insertions(+)
util/rcu.c +0 -0
index b703c86f15..9272fe5796 100644
@@ -307,6 +307,8 @@ static void *call_rcu_thread(void *opaque)
 
Quoted 7 lines

util/rcu.c static void *call_rcu_thread(void *opaque) · line 310

Quoted 3 lines
util/rcu.c +1 -0
 void call_rcu1(struct rcu_head *node, void (*func)(struct rcu_head *node))
 {
+    /* Avoid double rcu frees */

I would maybe have this say "Catch accidental attempts to use the same rcu node for two things at once", which isn't the same as a "double free" in the usual sense.

Agree. I'll also adjust the subject when repost, thanks.

Quoted 9 lines

util/rcu.c static void *call_rcu_thread(void *opaque) · line 314

Quoted 5 lines
util/rcu.c +1 -0
+    assert(node->func == NULL);
     node->func = func;
     enqueue(node);
     qatomic_inc(&rcu_call_count);

thanks
-- PMM

Signature · 1 line
Peter Xu