From: Roman Kiryanov <rkir@google.com>
To: pbonzini@redhat.com
Cc: jansene@google.com, jpcottin@google.com, mett@google.com,
qemu-devel@nongnu.org, rkir@google.com
Subject: [PATCH v3] timer: Fix a race condition between timer's callback and destroying code
Date: Wed, 26 Jun 2024 17:31:34 -0700 [thread overview]
Message-ID: <20240627003134.3447175-1-rkir@google.com> (raw)
In-Reply-To: <ad0740c5-9bc2-443c-9caa-a243b3a29108@redhat.com>
`timerlist_run_timers` provides no mechanism to
make sure the data pointed by `opaque` is valid
when calling timer's callback: there could be
another thread running which is destroying
timer's opaque data.
With this change `timer_del` becomes blocking if
timer's callback is running and it will be safe
to destroy timer's data once `timer_del` returns.
Signed-off-by: Roman Kiryanov <rkir@google.com>
---
v2: rebased to the right branch and removed
Google specific tags from the commit message.
v3: if a timer's callback happens to be running
(cb_running) wait until all timers are done.
qatomic_read/qemu_event_reset could be racing
and timer_del might wait one extra loop of
timers to be done.
include/qemu/timer.h | 1 +
util/qemu-timer.c | 11 +++++++++++
2 files changed, 12 insertions(+)
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 5ce83c7911..c2c98f79f4 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -89,6 +89,7 @@ struct QEMUTimer {
QEMUTimer *next;
int attributes;
int scale;
+ bool cb_running;
};
extern QEMUTimerListGroup main_loop_tlg;
diff --git a/util/qemu-timer.c b/util/qemu-timer.c
index 213114be68..5ec379dc43 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -370,6 +370,7 @@ void timer_init_full(QEMUTimer *ts,
ts->scale = scale;
ts->attributes = attributes;
ts->expire_time = -1;
+ ts->cb_running = false;
}
void timer_deinit(QEMUTimer *ts)
@@ -435,6 +436,10 @@ void timer_del(QEMUTimer *ts)
qemu_mutex_lock(&timer_list->active_timers_lock);
timer_del_locked(timer_list, ts);
qemu_mutex_unlock(&timer_list->active_timers_lock);
+
+ if (qatomic_read(&ts->cb_running)) {
+ qemu_event_wait(&timer_list->timers_done_ev);
+ }
}
}
@@ -571,9 +576,15 @@ bool timerlist_run_timers(QEMUTimerList *timer_list)
cb = ts->cb;
opaque = ts->opaque;
+ /* prevent timer_del from returning while cb(opaque)
+ * is still running (by waiting for timers_done_ev).
+ */
+ qatomic_set(&ts->cb_running, true);
+
/* run the callback (the timer list can be modified) */
qemu_mutex_unlock(&timer_list->active_timers_lock);
cb(opaque);
+ qatomic_set(&ts->cb_running, false);
qemu_mutex_lock(&timer_list->active_timers_lock);
progress = true;
--
2.45.2.741.gdbec12cfda-goog
next prev parent reply other threads:[~2024-06-27 0:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-26 21:52 [PATCH v2] timer: Fix a race condition between timer's callback and destroying code Roman Kiryanov
2024-06-26 23:29 ` Paolo Bonzini
2024-06-27 0:31 ` Roman Kiryanov [this message]
2024-06-27 13:27 ` [PATCH v3] " Paolo Bonzini
2024-06-27 16:12 ` Roman Kiryanov
2024-06-27 17:15 ` Paolo Bonzini
2024-06-27 17:53 ` Roman Kiryanov
2024-08-14 21:12 ` Patrick Leis
2024-08-14 22:10 ` Paolo Bonzini
2024-07-01 21:15 ` [PATCH v4] Add timer_join to avoid racing in timer cleanup Roman Kiryanov
2024-07-08 16:02 ` Roman Kiryanov
2024-07-22 6:00 ` Roman Kiryanov
-- strict thread matches above, loose matches on Subject: below --
2025-04-19 7:36 [PATCH v3] timer: Fix a race condition between timer's callback and destroying code Ajmal K
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240627003134.3447175-1-rkir@google.com \
--to=rkir@google.com \
--cc=jansene@google.com \
--cc=jpcottin@google.com \
--cc=mett@google.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).