hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond
PATCH 1/1 hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond
The emulation thread only waits to be signalled that a packet is pending, which a semaphore expresses directly. This also avoids the Coverity complaint that the condition variable is not used in the standard lock / while (!predicate) / cond_wait pattern. Same change as ccid-card-emulated in <GIT_SHA>.
---GIT_SHA -> [email protected]
--- hw/usb/u2f-emulated.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
hw/usb/u2f-emulated.c +6 -13
@@ -90,8 +90,7 @@ struct U2FEmulatedState {QemuMutex pending_out_mutex;/* Emulation thread and sync */ -QemuCond key_cond; -QemuMutex key_mutex; +QemuSemaphore key_sem;QemuThread key_thread;bool stop_thread;EventNotifier notifier;
@@ -148,9 +147,7 @@ static void u2f_emulated_recv_from_guest(U2FKeyState *base,u2f_pending_out_add (key, packet);qemu_mutex_unlock (&key->pending_out_mutex); -qemu_mutex_lock (&key->key_mutex); -qemu_cond_signal (&key->key_cond); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_post (&key->key_sem); }static void *u2f_emulated_thread (void * arg)
@@ -162,9 +159,7 @@ static void *u2f_emulated_thread(void* arg)while (true) {/* Wait signal */ -qemu_mutex_lock (&key->key_mutex); -qemu_cond_wait (&key->key_cond, &key->key_mutex); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_wait (&key->key_sem);/* Exit thread check */ if (key->stop_thread) {
@@ -331,10 +326,9 @@ static void u2f_emulated_realize(U2FKeyState *base, Error **errp)event_notifier_set_handler (&key->notifier, u2f_emulated_event_handler);/* Synchronization */ -qemu_cond_init (&key->key_cond); +qemu_sem_init (&key->key_sem,0 );qemu_mutex_init (&key->vdev_mutex);qemu_mutex_init (&key->pending_out_mutex); -qemu_mutex_init (&key->key_mutex);u2f_emulated_reset (key);/* Thread */
@@ -349,7 +343,7 @@ static void u2f_emulated_unrealize(U2FKeyState *base)/* Thread */ key->stop_thread = true; -qemu_cond_signal (&key->key_cond); +qemu_sem_post (&key->key_sem);qemu_thread_join (&key->key_thread);/* Notifier */
@@ -357,9 +351,8 @@ static void u2f_emulated_unrealize(U2FKeyState *base)event_notifier_cleanup (&key->notifier);/* Synchronization */ -qemu_cond_destroy (&key->key_cond); +qemu_sem_destroy (&key->key_sem);qemu_mutex_destroy (&key->vdev_mutex); -qemu_mutex_destroy (&key->key_mutex);qemu_mutex_destroy (&key->pending_out_mutex);/* Vdev */
Signature · 1 line
2.55.0
Quoted 71 lines · Philippe Mathieu-Daudé
The emulation thread only waits to be signalled that a packet is pending, which a semaphore expresses directly. This also avoids the Coverity complaint that the condition variable is not used in the standard lock / while (!predicate) /
cond_waitpattern. Same change as ccid-card-emulated in <GIT_SHA>.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
GIT_SHA-> [email protected]--- hw/usb/u2f-emulated.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)hw/usb/u2f-emulated.c +5
-11@@ -90,8 +90,7 @@ struct U2FEmulatedState {QemuMutex pending_out_mutex;/* Emulation thread and sync */ -QemuCond key_cond; -QemuMutex key_mutex; +QemuSemaphore key_sem;QemuThread key_thread;bool stop_thread;EventNotifier notifier;@@ -148,9 +147,7 @@ static void u2f_emulated_recv_from_guest(U2FKeyState *base,u2f_pending_out_add (key, packet);qemu_mutex_unlock (&key->pending_out_mutex); -qemu_mutex_lock (&key->key_mutex); -qemu_cond_signal (&key->key_cond); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_post (&key->key_sem); }static void *u2f_emulated_thread (void * arg)@@ -162,9 +159,7 @@ static void *u2f_emulated_thread(void* arg)while (true) {/* Wait signal */ -qemu_mutex_lock (&key->key_mutex); -qemu_cond_wait (&key->key_cond, &key->key_mutex); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_wait (&key->key_sem);/* Exit thread check */ if (key->stop_thread) {@@ -331,10 +326,9 @@ static void u2f_emulated_realize(U2FKeyState *base, Error **errp)event_notifier_set_handler (&key->notifier, u2f_emulated_event_handler);/* Synchronization */ -qemu_cond_init (&key->key_cond); +qemu_sem_init (&key->key_sem,0 );qemu_mutex_init (&key->vdev_mutex);qemu_mutex_init (&key->pending_out_mutex); -qemu_mutex_init (&key->key_mutex);u2f_emulated_reset (key);/* Thread */ @@ -349,7 +343,7 @@ static void u2f_emulated_unrealize(U2FKeyState *base)/* Thread */ key->stop_thread = true; -qemu_cond_signal (&key->key_cond); +qemu_sem_post (&key->key_sem);qemu_thread_join (&key->key_thread);/* Notifier */
hw/usb/u2f-emulated.c static void u2f_emulated_unrealize(U2FKeyState *base) · line 358
Quoted 10 lines
hw/usb/u2f-emulated.c +1
-2@@ -357,9 +351,8 @@ static void u2f_emulated_unrealize(U2FKeyState *base)event_notifier_cleanup (&key->notifier);/* Synchronization */ -qemu_cond_destroy (&key->key_cond); +qemu_sem_destroy (&key->key_sem);qemu_mutex_destroy (&key->vdev_mutex); -qemu_mutex_destroy (&key->key_mutex);qemu_mutex_destroy (&key->pending_out_mutex);/* Vdev */
A difference between the two patterns is that a semaphore has an associated counter. If you miss any underlying condition signal, qemu_sem_wait will wait forever.
The previous version was not impacted, has any cond_signal can wake up the emulated_thread no matter if you miss a previous signal.
In current code, do we have a guarantee we can't miss a cond_signal while treating a previous receive?
Regards,
Pierrick
Pierrick Bouvier
Reviewed-byQuoted 87 lines · Pierrick Bouvier
Quoted 71 lines · Philippe Mathieu-Daudé
The emulation thread only waits to be signalled that a packet is pending, which a semaphore expresses directly. This also avoids the Coverity complaint that the condition variable is not used in the standard lock / while (!predicate) /
cond_waitpattern. Same change as ccid-card-emulated in <GIT_SHA>.Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
GIT_SHA-> [email protected]--- hw/usb/u2f-emulated.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)hw/usb/u2f-emulated.c +5
-11@@ -90,8 +90,7 @@ struct U2FEmulatedState {QemuMutex pending_out_mutex;/* Emulation thread and sync */ -QemuCond key_cond; -QemuMutex key_mutex; +QemuSemaphore key_sem;QemuThread key_thread;bool stop_thread;EventNotifier notifier;@@ -148,9 +147,7 @@ static void u2f_emulated_recv_from_guest(U2FKeyState *base,u2f_pending_out_add (key, packet);qemu_mutex_unlock (&key->pending_out_mutex); -qemu_mutex_lock (&key->key_mutex); -qemu_cond_signal (&key->key_cond); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_post (&key->key_sem); }static void *u2f_emulated_thread (void * arg)@@ -162,9 +159,7 @@ static void *u2f_emulated_thread(void* arg)while (true) {/* Wait signal */ -qemu_mutex_lock (&key->key_mutex); -qemu_cond_wait (&key->key_cond, &key->key_mutex); -qemu_mutex_unlock (&key->key_mutex); +qemu_sem_wait (&key->key_sem);/* Exit thread check */ if (key->stop_thread) {@@ -331,10 +326,9 @@ static void u2f_emulated_realize(U2FKeyState *base, Error **errp)event_notifier_set_handler (&key->notifier, u2f_emulated_event_handler);/* Synchronization */ -qemu_cond_init (&key->key_cond); +qemu_sem_init (&key->key_sem,0 );qemu_mutex_init (&key->vdev_mutex);qemu_mutex_init (&key->pending_out_mutex); -qemu_mutex_init (&key->key_mutex);u2f_emulated_reset (key);/* Thread */ @@ -349,7 +343,7 @@ static void u2f_emulated_unrealize(U2FKeyState *base)/* Thread */ key->stop_thread = true; -qemu_cond_signal (&key->key_cond); +qemu_sem_post (&key->key_sem);qemu_thread_join (&key->key_thread);/* Notifier */ hw/usb/u2f-emulated.c static void u2f_emulated_unrealize(U2FKeyState *base) · line 358
Quoted 10 lines
hw/usb/u2f-emulated.c +1
-2@@ -357,9 +351,8 @@ static void u2f_emulated_unrealize(U2FKeyState *base)event_notifier_cleanup (&key->notifier);/* Synchronization */ -qemu_cond_destroy (&key->key_cond); +qemu_sem_destroy (&key->key_sem);qemu_mutex_destroy (&key->vdev_mutex); -qemu_mutex_destroy (&key->key_mutex);qemu_mutex_destroy (&key->pending_out_mutex);/* Vdev */ A difference between the two patterns is that a semaphore has an associated counter. If you miss any underlying condition signal,
qemu_sem_waitwill wait forever.
Quoted 6 lines
The previous version was not impacted, has any
cond_signalcan wake up theemulated_threadno matter if you miss a previous signal.In current code, do we have a guarantee we can't miss a
cond_signalwhile treating a previous receive?
I misread qemu_sem_wait implementation, we wait only if counter is 0. So the patch actually fixes the existing issue of missing a signal before waiting.
Thus, it's correct.
Regards,
Pierrick