rcu: Detect accidental reuse of rcu head
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
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
@@ -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
Re: [PATCH v2 1/3] qht: Zero-initialize qht_map
Quoted 7 lines · Peter Xu
QEMU almost always do this across the tree except QHT. This prepares for rcu to track double free.
Signed-off-by Peter Xu <[email protected]>--- util/qht.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH v2 2/3 tests/test-rcu-*: Zero-initialize allocated elements
In reply to Peter XuQEMU 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
@@ -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
PATCH v2 3/3 rcu: Catch rcu_head misuse
In reply to Peter XuTrap 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
@@ -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
Quoted 4 lines · Peter Xu
Peter Xu (3):
qht: Zero-initializeqht_maptests/test-rcu-*: Zero-initialize allocated elements
rcu: Catchrcu_headmisuse
r~