Skip to contentSkip to conversation

chardev/char-mux: tweak mux bitset operations

PATCH Superseded Roman Penyaev 2 participants 6 messages Nov 20, 2024 – Dec 6, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover chardev/char-mux: tweak mux bitset operations

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

v1..v2:

Rebase on latest master, incorporate review tags.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

 chardev/char-mux.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 chardev/char-mux: shift unsigned long to avoid 32-bit overflow

Allthough the size of MAX_MUX is equal to 4 and likely will never change, this patch changes type of constant to unsigned long to be on the safe side.

Also add a static compile check that MAX_MUX never bigger than sizeof(d->mux_bitset).

---
 chardev/char-mux.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
chardev/char-mux.c +4 -2
index bda5c45e6058..355520714979 100644
@@ -316,6 +316,8 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
 {
     unsigned int bit;
 
+    QEMU_BUILD_BUG_ON(MAX_MUX > sizeof(d->mux_bitset));
+
     bit = find_next_zero_bit(&d->mux_bitset, MAX_MUX, 0);
     if (bit >= MAX_MUX) {
         error_setg(errp,
@@ -325,7 +327,7 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
         return false;
     }
 
-    d->mux_bitset |= (1 << bit);
+    d->mux_bitset |= (1ul << bit);
     d->backends[bit] = b;
     *tag = bit;
 
@@ -341,7 +343,7 @@ bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag)
         return false;
     }
 
-    d->mux_bitset &= ~(1 << bit);
+    d->mux_bitset &= ~(1ul << bit);
     d->backends[bit] = NULL;
 
     return true;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/2 chardev/char-mux: make boolean bit check instead of find_next_bit()

In reply to Roman Penyaev

This patch simplifies (and makes less confusing) bit checks by replacing find_next_bit() calls with boolean AND operation.

Resolves: Coverity CID 1563776

---
 chardev/char-mux.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
chardev/char-mux.c +4 -7
index 355520714979..f613a22e3758 100644
@@ -336,15 +336,12 @@ bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
 
 bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag)
 {
-    unsigned int bit;
-
-    bit = find_next_bit(&d->mux_bitset, MAX_MUX, tag);
-    if (bit != tag) {
+    if (!(d->mux_bitset & (1ul << tag))) {
         return false;
     }
 
-    d->mux_bitset &= ~(1ul << bit);
-    d->backends[bit] = NULL;
+    d->mux_bitset &= ~(1ul << tag);
+    d->backends[tag] = NULL;
 
     return true;
 }
@@ -353,7 +350,7 @@ void mux_set_focus(Chardev *chr, unsigned int focus)
 {
     MuxChardev *d = MUX_CHARDEV(chr);
 
-    assert(find_next_bit(&d->mux_bitset, MAX_MUX, focus) == focus);
+    assert(d->mux_bitset & (1ul << focus));
 
     if (d->focus != -1) {
         mux_chr_send_event(d, d->focus, CHR_EVENT_MUX_OUT);
Signature · 1 line
2.34.1
#3 lore raw
In reply to Roman Penyaev

Queued, with just one change:

Quoted 2 lines

Also add a static compile check that MAX_MUX never bigger than sizeof(d->mux_bitset).

This needs to be multiplied by CHAR_BIT.

Paolo

Quoted 22 lines · Roman Penyaev

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

v1..v2:

Rebase on latest master, incorporate review tags.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

  chardev/char-mux.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
#4 lore raw

Hi Paolo,

Do I need to resend the fixed version, or if it is queued to what exact repo or branch? I'm a bit confused. Thanks.

--
Roman

Quoted 34 lines · Paolo Bonzini

Queued, with just one change:

Quoted 2 lines

Also add a static compile check that MAX_MUX never bigger than sizeof(d->mux_bitset).

This needs to be multiplied by CHAR_BIT.

Paolo

Quoted 22 lines · Roman Penyaev

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset operations.

v1..v2:

Rebase on latest master, incorporate review tags.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

  chardev/char-mux.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
#5 lore raw

Il lun 25 nov 2024, 08:46 Roman Penyaev <[email protected]> ha scritto:

Quoted 5 lines

Hi Paolo,

Do I need to resend the fixed version, or if it is queued to what exact repo or branch? I'm a bit confused. Thanks.

Hi, sorry for the late reply—I was not sending it out for inclusion because we're in release freeze, but there was no need to resend.

Paolo

--

Quoted 40 lines

Roman

Quoted 13 lines · Paolo Bonzini

Queued, with just one change:

Quoted 2 lines

Also add a static compile check that MAX_MUX never bigger than sizeof(d->mux_bitset).

This needs to be multiplied by CHAR_BIT.

Paolo

Quoted 2 lines · Roman Penyaev

Patchset tweaks bitset operations by changing a constant to unsigned long, introduces a static compile check and simplifies bitset

operations.

Quoted 21 lines
Quoted 20 lines

v1..v2:

Rebase on latest master, incorporate review tags.

Roman Penyaev (2):
chardev/char-mux: shift unsigned long to avoid 32-bit overflow chardev/char-mux: make boolean bit check instead of find_next_bit()

  chardev/char-mux.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)