From: Roman Kiryanov <rkir@google.com>
To: qemu-devel@nongnu.org
Cc: jansene@google.com, mett@google.com, jpcottin@google.com,
Roman Kiryanov <rkir@google.com>
Subject: [PATCH v2] timer: Fix a race condition between timer's callback and destroying code
Date: Wed, 26 Jun 2024 14:52:41 -0700 [thread overview]
Message-ID: <20240626215241.3360246-1-rkir@google.com> (raw)
`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.
include/qemu/timer.h | 4 ++++
util/qemu-timer.c | 21 +++++++++++++++++++++
2 files changed, 25 insertions(+)
diff --git a/include/qemu/timer.h b/include/qemu/timer.h
index 5ce83c7911..efd0e95d20 100644
--- a/include/qemu/timer.h
+++ b/include/qemu/timer.h
@@ -3,6 +3,7 @@
#include "qemu/bitops.h"
#include "qemu/notify.h"
+#include "qemu/thread.h"
#include "qemu/host-utils.h"
#define NANOSECONDS_PER_SECOND 1000000000LL
@@ -86,9 +87,12 @@ struct QEMUTimer {
QEMUTimerList *timer_list;
QEMUTimerCB *cb;
void *opaque;
+ QemuMutex opaque_lock;
+ QemuCond cb_done;
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..95af255519 100644
--- a/util/qemu-timer.c
+++ b/util/qemu-timer.c
@@ -369,7 +369,10 @@ void timer_init_full(QEMUTimer *ts,
ts->opaque = opaque;
ts->scale = scale;
ts->attributes = attributes;
+ qemu_mutex_init(&ts->opaque_lock);
+ qemu_cond_init(&ts->cb_done);
ts->expire_time = -1;
+ ts->cb_running = false;
}
void timer_deinit(QEMUTimer *ts)
@@ -394,6 +397,12 @@ static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
}
pt = &t->next;
}
+
+ qemu_mutex_lock(&ts->opaque_lock);
+ while (ts->cb_running) {
+ qemu_cond_wait(&ts->cb_done, &ts->opaque_lock);
+ }
+ qemu_mutex_unlock(&ts->opaque_lock);
}
static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
@@ -571,11 +580,23 @@ bool timerlist_run_timers(QEMUTimerList *timer_list)
cb = ts->cb;
opaque = ts->opaque;
+ /* Mark the callback as running to prevent
+ * destroying `opaque` in another thread.
+ */
+ qemu_mutex_lock(&ts->opaque_lock);
+ ts->cb_running = true;
+ qemu_mutex_unlock(&ts->opaque_lock);
+
/* run the callback (the timer list can be modified) */
qemu_mutex_unlock(&timer_list->active_timers_lock);
cb(opaque);
qemu_mutex_lock(&timer_list->active_timers_lock);
+ qemu_mutex_lock(&ts->opaque_lock);
+ ts->cb_running = false;
+ qemu_cond_broadcast(&ts->cb_done);
+ qemu_mutex_unlock(&ts->opaque_lock);
+
progress = true;
}
qemu_mutex_unlock(&timer_list->active_timers_lock);
--
2.45.2.741.gdbec12cfda-goog
next reply other threads:[~2024-06-26 21:54 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-26 21:52 Roman Kiryanov [this message]
2024-06-26 23:29 ` [PATCH v2] timer: Fix a race condition between timer's callback and destroying code Paolo Bonzini
2024-06-27 0:31 ` [PATCH v3] " Roman Kiryanov
2024-06-27 13:27 ` 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
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=20240626215241.3360246-1-rkir@google.com \
--to=rkir@google.com \
--cc=jansene@google.com \
--cc=jpcottin@google.com \
--cc=mett@google.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).