[PATCH] hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond

Philippe Mathieu-Daudé posted 1 patch 6 days, 5 hours ago
hw/usb/u2f-emulated.c | 19 ++++++-------------
1 file changed, 6 insertions(+), 13 deletions(-)
[PATCH] hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond
Posted by Philippe Mathieu-Daudé 6 days, 5 hours ago
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>.

Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
---
GIT_SHA -> 20261109203817.1683717-1-peter.maydell@linaro.org
---
 hw/usb/u2f-emulated.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)

diff --git a/hw/usb/u2f-emulated.c b/hw/usb/u2f-emulated.c
index b82a31501934..0aabfc514b7b 100644
--- a/hw/usb/u2f-emulated.c
+++ b/hw/usb/u2f-emulated.c
@@ -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 */
-- 
2.55.0


Re: [PATCH] hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond
Posted by Pierrick Bouvier 6 days, 5 hours ago
On 11/16/2026 2:36 PM, Philippe Mathieu-Daudé wrote:
> 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>.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> ---
> GIT_SHA -> 20261109203817.1683717-1-peter.maydell@linaro.org
> ---
>  hw/usb/u2f-emulated.c | 19 ++++++-------------
>  1 file changed, 6 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/usb/u2f-emulated.c b/hw/usb/u2f-emulated.c
> index b82a31501934..0aabfc514b7b 100644
> --- a/hw/usb/u2f-emulated.c
> +++ b/hw/usb/u2f-emulated.c
> @@ -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 */

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

Re: [PATCH] hw/usb/u2f-emulated: Use QemuSemaphore rather than QemuCond
Posted by Pierrick Bouvier 6 days, 5 hours ago
On 11/16/2026 2:54 PM, Pierrick Bouvier wrote:
> On 11/16/2026 2:36 PM, Philippe Mathieu-Daudé wrote:
>> 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>.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
>> ---
>> GIT_SHA -> 20261109203817.1683717-1-peter.maydell@linaro.org
>> ---
>>  hw/usb/u2f-emulated.c | 19 ++++++-------------
>>  1 file changed, 6 insertions(+), 13 deletions(-)
>>
>> diff --git a/hw/usb/u2f-emulated.c b/hw/usb/u2f-emulated.c
>> index b82a31501934..0aabfc514b7b 100644
>> --- a/hw/usb/u2f-emulated.c
>> +++ b/hw/usb/u2f-emulated.c
>> @@ -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 */
> 
> 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?
>

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.

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>

Regards,
Pierrick