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

Peter Maydell posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261109203817.1683717-1-peter.maydell@linaro.org
hw/usb/ccid-card-emulated.c | 22 +++++++---------------
1 file changed, 7 insertions(+), 15 deletions(-)
[PATCH] hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond
Posted by Peter Maydell 1 week, 6 days ago
The emulated CCID card uses a QemuMutex and QemuCond to communicate
with its helper thread, but all it's trying to do here is "make the
helper thread loop wait until is signalled that it has more work to
do".  The existing code functions correctly, but "wait until the
other thread signals you" is more simply implemented with a
QemuSemaphore.

This avoids a Coverity complaint that we're not using the condition
variable in the standard

   lock mutex;
   while (!some_predicate) {
       cond_wait(condvar, mutex);
   }
   do stuff;
   unlock mutex;

pattern.

Resolves: Coverity CID 1547639
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
I've tested this with a functional test that exercises the
"use certificates stored in files" setup from the documentation,
but I still need to arrange to have the official tuxrun guest
kernel/images updated with the extra config necessary, so in
the meantime I'm just posting this patch, and hope to post
the functional-test as a separate patch later this week.
The issue has been floating around in Coverity for years and
the existing code is not incorrect, so there's no strong
urgency to get this into 11.2.

PS: there is no maintainers entry for the ccid-usb code;
would somebody (I guess from RedHat as the original
contributors of it) like to take it on? Or if it's really
"orphan" that would be worth documenting.

---
 hw/usb/ccid-card-emulated.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/hw/usb/ccid-card-emulated.c b/hw/usb/ccid-card-emulated.c
index 6c8eec39faa..c504c52a7cd 100644
--- a/hw/usb/ccid-card-emulated.c
+++ b/hw/usb/ccid-card-emulated.c
@@ -129,8 +129,7 @@ struct EmulatedState {
     VReader *reader;
     QSIMPLEQ_HEAD(, EmulEvent) guest_apdu_list;
     QemuMutex vreader_mutex; /* and guest_apdu_list mutex */
-    QemuMutex handle_apdu_mutex;
-    QemuCond handle_apdu_cond;
+    QemuSemaphore handle_apdu_sem;
     EventNotifier notifier;
     int      quit_apdu_thread;
     QemuThread apdu_thread_id;
@@ -149,9 +148,7 @@ static void emulated_apdu_from_guest(CCIDCardState *base,
     qemu_mutex_lock(&card->vreader_mutex);
     QSIMPLEQ_INSERT_TAIL(&card->guest_apdu_list, event, entry);
     qemu_mutex_unlock(&card->vreader_mutex);
-    qemu_mutex_lock(&card->handle_apdu_mutex);
-    qemu_cond_signal(&card->handle_apdu_cond);
-    qemu_mutex_unlock(&card->handle_apdu_mutex);
+    qemu_sem_post(&card->handle_apdu_sem);
 }
 
 static const uint8_t *emulated_get_atr(CCIDCardState *base, uint32_t *len)
@@ -238,9 +235,7 @@ static void *handle_apdu_thread(void* arg)
     EmulEvent *event;
 
     while (1) {
-        qemu_mutex_lock(&card->handle_apdu_mutex);
-        qemu_cond_wait(&card->handle_apdu_cond, &card->handle_apdu_mutex);
-        qemu_mutex_unlock(&card->handle_apdu_mutex);
+        qemu_sem_wait(&card->handle_apdu_sem);
         if (card->quit_apdu_thread) {
             card->quit_apdu_thread = 0; /* debugging */
             break;
@@ -496,8 +491,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
     QSIMPLEQ_INIT(&card->guest_apdu_list);
     qemu_mutex_init(&card->event_list_mutex);
     qemu_mutex_init(&card->vreader_mutex);
-    qemu_mutex_init(&card->handle_apdu_mutex);
-    qemu_cond_init(&card->handle_apdu_cond);
+    qemu_sem_init(&card->handle_apdu_sem, 0);
     card->reader = NULL;
     card->quit_apdu_thread = 0;
     if (init_event_notifier(card, errp) < 0) {
@@ -556,8 +550,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
 out2:
     clean_event_notifier(card);
 out1:
-    qemu_cond_destroy(&card->handle_apdu_cond);
-    qemu_mutex_destroy(&card->handle_apdu_mutex);
+    qemu_sem_destroy(&card->handle_apdu_sem);
     qemu_mutex_destroy(&card->vreader_mutex);
     qemu_mutex_destroy(&card->event_list_mutex);
 }
@@ -571,13 +564,12 @@ static void emulated_unrealize(CCIDCardState *base)
     qemu_thread_join(&card->event_thread_id);
 
     card->quit_apdu_thread = 1; /* stop handle_apdu thread */
-    qemu_cond_signal(&card->handle_apdu_cond);
+    qemu_sem_post(&card->handle_apdu_sem);
     qemu_thread_join(&card->apdu_thread_id);
 
     clean_event_notifier(card);
     /* threads exited, can destroy all condvars/mutexes */
-    qemu_cond_destroy(&card->handle_apdu_cond);
-    qemu_mutex_destroy(&card->handle_apdu_mutex);
+    qemu_sem_destroy(&card->handle_apdu_sem);
     qemu_mutex_destroy(&card->vreader_mutex);
     qemu_mutex_destroy(&card->event_list_mutex);
 
-- 
2.43.0
Re: [PATCH] hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond
Posted by Marc-André Lureau 1 week, 3 days ago
On Tue, Nov 10, 2026 at 12:38 AM Peter Maydell <peter.maydell@linaro.org> wrote:
>
> The emulated CCID card uses a QemuMutex and QemuCond to communicate
> with its helper thread, but all it's trying to do here is "make the
> helper thread loop wait until is signalled that it has more work to
> do".  The existing code functions correctly, but "wait until the
> other thread signals you" is more simply implemented with a
> QemuSemaphore.
>
> This avoids a Coverity complaint that we're not using the condition
> variable in the standard
>
>    lock mutex;
>    while (!some_predicate) {
>        cond_wait(condvar, mutex);
>    }
>    do stuff;
>    unlock mutex;
>
> pattern.
>
> Resolves: Coverity CID 1547639
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
> I've tested this with a functional test that exercises the
> "use certificates stored in files" setup from the documentation,
> but I still need to arrange to have the official tuxrun guest
> kernel/images updated with the extra config necessary, so in
> the meantime I'm just posting this patch, and hope to post
> the functional-test as a separate patch later this week.
> The issue has been floating around in Coverity for years and
> the existing code is not incorrect, so there's no strong
> urgency to get this into 11.2.
>
> PS: there is no maintainers entry for the ccid-usb code;
> would somebody (I guess from RedHat as the original
> contributors of it) like to take it on? Or if it's really
> "orphan" that would be worth documenting.
>
> ---
>  hw/usb/ccid-card-emulated.c | 22 +++++++---------------
>  1 file changed, 7 insertions(+), 15 deletions(-)
>
> diff --git a/hw/usb/ccid-card-emulated.c b/hw/usb/ccid-card-emulated.c
> index 6c8eec39faa..c504c52a7cd 100644
> --- a/hw/usb/ccid-card-emulated.c
> +++ b/hw/usb/ccid-card-emulated.c
> @@ -129,8 +129,7 @@ struct EmulatedState {
>      VReader *reader;
>      QSIMPLEQ_HEAD(, EmulEvent) guest_apdu_list;
>      QemuMutex vreader_mutex; /* and guest_apdu_list mutex */
> -    QemuMutex handle_apdu_mutex;
> -    QemuCond handle_apdu_cond;
> +    QemuSemaphore handle_apdu_sem;
>      EventNotifier notifier;
>      int      quit_apdu_thread;
>      QemuThread apdu_thread_id;
> @@ -149,9 +148,7 @@ static void emulated_apdu_from_guest(CCIDCardState *base,
>      qemu_mutex_lock(&card->vreader_mutex);
>      QSIMPLEQ_INSERT_TAIL(&card->guest_apdu_list, event, entry);
>      qemu_mutex_unlock(&card->vreader_mutex);
> -    qemu_mutex_lock(&card->handle_apdu_mutex);
> -    qemu_cond_signal(&card->handle_apdu_cond);
> -    qemu_mutex_unlock(&card->handle_apdu_mutex);
> +    qemu_sem_post(&card->handle_apdu_sem);
>  }
>
>  static const uint8_t *emulated_get_atr(CCIDCardState *base, uint32_t *len)
> @@ -238,9 +235,7 @@ static void *handle_apdu_thread(void* arg)
>      EmulEvent *event;
>
>      while (1) {
> -        qemu_mutex_lock(&card->handle_apdu_mutex);
> -        qemu_cond_wait(&card->handle_apdu_cond, &card->handle_apdu_mutex);
> -        qemu_mutex_unlock(&card->handle_apdu_mutex);
> +        qemu_sem_wait(&card->handle_apdu_sem);
>          if (card->quit_apdu_thread) {
>              card->quit_apdu_thread = 0; /* debugging */
>              break;
> @@ -496,8 +491,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
>      QSIMPLEQ_INIT(&card->guest_apdu_list);
>      qemu_mutex_init(&card->event_list_mutex);
>      qemu_mutex_init(&card->vreader_mutex);
> -    qemu_mutex_init(&card->handle_apdu_mutex);
> -    qemu_cond_init(&card->handle_apdu_cond);
> +    qemu_sem_init(&card->handle_apdu_sem, 0);
>      card->reader = NULL;
>      card->quit_apdu_thread = 0;
>      if (init_event_notifier(card, errp) < 0) {
> @@ -556,8 +550,7 @@ static void emulated_realize(CCIDCardState *base, Error **errp)
>  out2:
>      clean_event_notifier(card);
>  out1:
> -    qemu_cond_destroy(&card->handle_apdu_cond);
> -    qemu_mutex_destroy(&card->handle_apdu_mutex);
> +    qemu_sem_destroy(&card->handle_apdu_sem);
>      qemu_mutex_destroy(&card->vreader_mutex);
>      qemu_mutex_destroy(&card->event_list_mutex);
>  }
> @@ -571,13 +564,12 @@ static void emulated_unrealize(CCIDCardState *base)
>      qemu_thread_join(&card->event_thread_id);
>
>      card->quit_apdu_thread = 1; /* stop handle_apdu thread */
> -    qemu_cond_signal(&card->handle_apdu_cond);
> +    qemu_sem_post(&card->handle_apdu_sem);
>      qemu_thread_join(&card->apdu_thread_id);
>
>      clean_event_notifier(card);
>      /* threads exited, can destroy all condvars/mutexes */

I'd drop that stale comment

> -    qemu_cond_destroy(&card->handle_apdu_cond);
> -    qemu_mutex_destroy(&card->handle_apdu_mutex);
> +    qemu_sem_destroy(&card->handle_apdu_sem);
>      qemu_mutex_destroy(&card->vreader_mutex);
>      qemu_mutex_destroy(&card->event_list_mutex);
>
> --
> 2.43.0
>
Re: [PATCH] hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond
Posted by Philippe Mathieu-Daudé 1 week, 6 days ago
On 9/11/26 21:38, Peter Maydell wrote:
> The emulated CCID card uses a QemuMutex and QemuCond to communicate
> with its helper thread, but all it's trying to do here is "make the
> helper thread loop wait until is signalled that it has more work to
> do".  The existing code functions correctly, but "wait until the
> other thread signals you" is more simply implemented with a
> QemuSemaphore.
> 
> This avoids a Coverity complaint that we're not using the condition
> variable in the standard
> 
>     lock mutex;
>     while (!some_predicate) {
>         cond_wait(condvar, mutex);
>     }
>     do stuff;
>     unlock mutex;
> 
> pattern.
> 
> Resolves: Coverity CID 1547639
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---


> ---
>   hw/usb/ccid-card-emulated.c | 22 +++++++---------------
>   1 file changed, 7 insertions(+), 15 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

Re: [PATCH] hw/usb/ccid-card-emulated: Use QemuSemaphore rather than QemuCond
Posted by Philippe Mathieu-Daudé 1 week, 6 days ago
On 9/11/26 22:38, Philippe Mathieu-Daudé wrote:
> On 9/11/26 21:38, Peter Maydell wrote:
>> The emulated CCID card uses a QemuMutex and QemuCond to communicate
>> with its helper thread, but all it's trying to do here is "make the
>> helper thread loop wait until is signalled that it has more work to
>> do".  The existing code functions correctly, but "wait until the
>> other thread signals you" is more simply implemented with a
>> QemuSemaphore.
>>
>> This avoids a Coverity complaint that we're not using the condition
>> variable in the standard
>>
>>     lock mutex;
>>     while (!some_predicate) {
>>         cond_wait(condvar, mutex);
>>     }
>>     do stuff;
>>     unlock mutex;
>>
>> pattern.

Similarly on hw/usb/u2f-emulated.c:

-- >8 --
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);

---

I'll send later.

>>
>> Resolves: Coverity CID 1547639
>> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
>> ---
> 
> 
>> ---
>>   hw/usb/ccid-card-emulated.c | 22 +++++++---------------
>>   1 file changed, 7 insertions(+), 15 deletions(-)
> 
> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
>