Skip to contentSkip to conversation

qemu/timer: Clarify QEMUTimer new/free API

PATCH In review Philippe Mathieu-Daudé 3 participants 10 messages Jan 25, 2025 – Feb 9, 2025

lore
Share & tools
lore
#0 lore raw

cover qemu/timer: Clarify QEMUTimer new/free API

Update few QEMUTimer docstring and add a
sanity check during timer initialization.

Noticed trying to understand leaks in QDev
Realize -> Unrealize -> Realize transition.

Philippe Mathieu-Daudé (2):
qemu/timer: Clarify timer_new*() must be freed with timer_free() qemu/timer: Sanity check timer_list in timer_init_full()

 include/qemu/timer.h | 12 +++++++++++-
 util/qemu-timer.c    |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/2 qemu/timer: Clarify timer_new*() must be freed with timer_free()

There was not mention QEMUTimer created with timer_new*() must be released with timer_free() instead of g_free(), because then active timers are removed from the active list. Update the documentation mentioning timer_free().

---
 include/qemu/timer.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/qemu/timer.h +10 -0
index cc167bd825b..abd2204f3be 100644
@@ -507,6 +507,8 @@ static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
  * with an AioContext---each of them runs its timer callbacks in its own
  * AioContext thread.
  *
+ * The timer returned must be freed using timer_free().
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
@@ -530,6 +532,8 @@ static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
  * and associate it with the default timer list for the clock type @type.
  * See timer_new_full for details.
  *
+ * The timer returned must be freed using timer_free().
+ *
  * Returns: a pointer to the timer
  */
 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
@@ -548,6 +552,8 @@ static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  * associated with the clock.
  * See timer_new_full for details.
  *
+ * The timer returned must be freed using timer_free().
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
@@ -566,6 +572,8 @@ static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  * associated with the clock.
  * See timer_new_full for details.
  *
+ * The timer returned must be freed using timer_free().
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
@@ -584,6 +592,8 @@ static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  * associated with the clock.
  * See timer_new_full for details.
  *
+ * The timer returned must be freed using timer_free().
+ *
  * Returns: a pointer to the newly created timer
  */
 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
Signature · 1 line
2.47.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] qemu/timer: Clarify timer_new*() must be freed with timer_free()

Quoted 9 lines · Philippe Mathieu-Daudé

There was not mention QEMUTimer created with timer_new*() must be released with timer_free() instead of g_free(), because then active timers are removed from the active list. Update the documentation mentioning timer_free().

---
 include/qemu/timer.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw

PATCH 2/2 qemu/timer: Sanity check timer_list in timer_init_full()

In reply to Philippe Mathieu-Daudé

Ensure we are not re-initializing a QEMUTimer already added to an active list. timer_init*() functions expect either a recently created and zeroed QEMUTimer, or one previously free'd with timer_free().

---
 include/qemu/timer.h | 2 +-
 util/qemu-timer.c    | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
include/qemu/timer.h +1 -1
index abd2204f3be..4717693f950 100644
@@ -407,7 +407,7 @@ int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
  * (or default timer list group, if NULL).
  * The caller is responsible for allocating the memory.
  *
- * You need not call an explicit deinit call. Simply make
+ * You need not call an explicit timer_deinit() call. Simply make
  * sure it is not on a list with timer_del.
  */
 void timer_init_full(QEMUTimer *ts,
util/qemu-timer.c +1 -0
index 0e8a453eaa1..058cae6e487 100644
@@ -354,6 +354,7 @@ void timer_init_full(QEMUTimer *ts,
     if (!timer_list_group) {
         timer_list_group = &main_loop_tlg;
     }
+    assert(ts->timer_list == NULL);
     ts->timer_list = timer_list_group->tl[type];
     ts->cb = cb;
     ts->opaque = opaque;
Signature · 1 line
2.47.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] qemu/timer: Sanity check timer_list in timer_init_full()

Quoted 10 lines · Philippe Mathieu-Daudé

Ensure we are not re-initializing a QEMUTimer already added to an active list. timer_init*() functions expect either a recently created and zeroed QEMUTimer, or one previously free'd with timer_free().

---
 include/qemu/timer.h | 2 +-
 util/qemu-timer.c    | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw

Re: [PATCH 2/2] qemu/timer: Sanity check timer_list in timer_init_full()

In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

- * You need not call an explicit deinit call. Simply make
+ * You need not call an explicit timer_deinit() call. Simply make
* sure it is not on a list with timer_del.

Reworded this as "You need not call timer_deinit() explicitly. Simply make..."

and applied to trivial-patches tree.

Thanks,

/mjt

#6 lore raw

Re: [PATCH 2/2] qemu/timer: Sanity check timer_list in timer_init_full()

Quoted 7 lines · Michael Tokarev
Quoted 3 lines · Philippe Mathieu-Daudé

- * You need not call an explicit deinit call. Simply make
+ * You need not call an explicit timer_deinit() call. Simply make
   * sure it is not on a list with timer_del.

Reworded this as "You need not call timer_deinit() explicitly. Simply make..."

"You don't need to call timer_deinit() explicitly.", actually.

This breaks quite a lot of CI tests: https://gitlab.com/mjt0k/qemu/-/pipelines/1662551717

#7 lore raw

Re: [PATCH 2/2] qemu/timer: Sanity check timer_list in timer_init_full()

Quoted 7 lines · Michael Tokarev
Quoted 6 lines · Michael Tokarev
Quoted 3 lines · Philippe Mathieu-Daudé

- * You need not call an explicit deinit call. Simply make
+ * You need not call an explicit timer_deinit() call. Simply make
   * sure it is not on a list with timer_del.

Quoted 5 lines
Quoted 1 line

Reworded this as "You need not call timer_deinit() explicitly. Simply make..."

"You don't need to call timer_deinit() explicitly.", actually.

This breaks quite a lot of CI tests: https://gitlab.com/mjt0k/qemu/-/ pipelines/1662551717

Do sorry, I only tested on a specific config and missed all the other errors :/

#8 lore raw
In reply to Philippe Mathieu-Daudé

ping?

Quoted 14 lines · Philippe Mathieu-Daudé

Update few QEMUTimer docstring and add a
sanity check during timer initialization.

Noticed trying to understand leaks in QDev
Realize -> Unrealize -> Realize transition.

Philippe Mathieu-Daudé (2):
qemu/timer: Clarify timer_new*() must be freed with timer_free() qemu/timer: Sanity check timer_list in timer_init_full()

  include/qemu/timer.h | 12 +++++++++++-
  util/qemu-timer.c    |  1 +
  2 files changed, 12 insertions(+), 1 deletion(-)
#9 lore raw

Cc'ing more developers.

Quoted 18 lines · Philippe Mathieu-Daudé

ping?

Quoted 14 lines · Philippe Mathieu-Daudé

Update few QEMUTimer docstring and add a
sanity check during timer initialization.

Noticed trying to understand leaks in QDev
Realize -> Unrealize -> Realize transition.

Philippe Mathieu-Daudé (2):
   qemu/timer: Clarify timer_new*() must be freed with timer_free() qemu/timer: Sanity check timer_list in timer_init_full()

  include/qemu/timer.h | 12 +++++++++++-
  util/qemu-timer.c    |  1 +

  2 files changed, 12 insertions(+), 1 deletion(-)