Skip to contentSkip to conversation

Audio 5.1 patches

PATCH Superseded Kővágó, Zoltán 6 participants 78 messages Dec 23, 2018 – Jan 16, 2019 Page 2 of 2

Subsystems (2)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#75 lore raw

Re: [Qemu-devel] [PATCH v2 52/52] usbaudio: change playback counters to 64 bit

In reply to Kővágó, Zoltán
Quoted 20 lines · Zoltán

With stereo playback, they need about 375 minutes of continuous audio playback to overflow, which is usually not a problem (as stopping and later resuming playback resets the counters). But with 7.1 audio, they only need about 95 minutes to overflow.

After the overflow, the buf->prod % USBAUDIO_PACKET_SIZE(channels) assertion no longer holds true, which will result in overflowing the buffer. With 64 bit variables, it would take about 762000 years to overflow.

---
 hw/usb/dev-audio.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
hw/usb/dev-audio.c +0 -0
index 29475a2b70..45ffc3ebb3 100644

hw/usb/dev-audio.c static const USBDesc desc_audio_multi = { · line 582

Quoted 9 lines
hw/usb/dev-audio.c +3 -3
@@ -577,9 +577,9 @@ static const USBDesc desc_audio_multi = {
 
 struct streambuf {
     uint8_t *data;
-    uint32_t size;
-    uint32_t prod;
-    uint32_t cons;
+    size_t size;
+    uint64_t prod;
+    uint64_t cons;

OK.

hw/usb/dev-audio.c static void streambuf_fini(struct streambuf *buf) · line 603

Quoted 9 lines
hw/usb/dev-audio.c +1 -1
 };
 
 static void streambuf_init(struct streambuf *buf, uint32_t size,
@@ -600,12 +600,14 @@ static void streambuf_fini(struct streambuf *buf)
 
 static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
 {
-    uint32_t free = buf->size - (buf->prod - buf->cons);
+    uint64_t free = buf->size - (buf->prod - buf->cons);

I'd use ssize_t here.

Quoted 11 lines
hw/usb/dev-audio.c +2 -0
     if (free < USBAUDIO_PACKET_SIZE(channels)) {
         return 0;
     }
 
+    /* can happen if prod overflows */
+    assert(buf->prod % USBAUDIO_PACKET_SIZE(channels) == 0);
     usb_packet_copy(p, buf->data + (buf->prod % buf->size),
                     USBAUDIO_PACKET_SIZE(channels));
     buf->prod += USBAUDIO_PACKET_SIZE(channels);

hw/usb/dev-audio.c static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels) · line 622

Quoted 8 lines
hw/usb/dev-audio.c +1 -1
@@ -614,7 +616,7 @@ static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
 
 static uint8_t *streambuf_get(struct streambuf *buf, size_t *len)
 {
-    uint32_t used = buf->prod - buf->cons;
+    uint64_t used = buf->prod - buf->cons;
     uint8_t *data;
 
     if (!used) {

Eventually here:

    ssize_t used = buf->prod - buf->cons;

    if (used <= 0) {
        return NULL;
    }
#76 lore raw

Re: [Qemu-devel] [PATCH v2 52/52] usbaudio: change playback counters to 64 bit

Hi,

Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 20 lines · Zoltán

With stereo playback, they need about 375 minutes of continuous audio playback to overflow, which is usually not a problem (as stopping and later resuming playback resets the counters). But with 7.1 audio, they only need about 95 minutes to overflow.

After the overflow, the buf->prod % USBAUDIO_PACKET_SIZE(channels) assertion no longer holds true, which will result in overflowing the buffer. With 64 bit variables, it would take about 762000 years to overflow.

---
 hw/usb/dev-audio.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
hw/usb/dev-audio.c +0 -0
index 29475a2b70..45ffc3ebb3 100644

hw/usb/dev-audio.c static const USBDesc desc_audio_multi = { · line 582

Quoted 9 lines
hw/usb/dev-audio.c +3 -3
@@ -577,9 +577,9 @@ static const USBDesc desc_audio_multi = {
 
 struct streambuf {
     uint8_t *data;
-    uint32_t size;
-    uint32_t prod;
-    uint32_t cons;
+    size_t size;
+    uint64_t prod;
+    uint64_t cons;

OK.

Quoted 5 lines
hw/usb/dev-audio.c +0 -0
 };
 
 static void streambuf_init(struct streambuf *buf, uint32_t size,
@@ -600,12 +600,14 @@ static void streambuf_fini(struct streambuf *buf)
 
Quoted 7 lines

hw/usb/dev-audio.c static void streambuf_fini(struct streambuf *buf) · line 603

Quoted 4 lines
hw/usb/dev-audio.c +1 -1
 static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
 {
-    uint32_t free = buf->size - (buf->prod - buf->cons);
+    uint64_t free = buf->size - (buf->prod - buf->cons);

I'd use ssize_t here.

Hm, I agree with the signed part, but I'm not sure about the size_t part. Granted, there's a big problem if buf->prod - buf->cons can't be representated on 32 bits, but still I'd rather use int64_t in this case and prevent this potential truncation on 32-bit platforms.

Quoted 22 lines
Quoted 11 lines
hw/usb/dev-audio.c +2 -0
     if (free < USBAUDIO_PACKET_SIZE(channels)) {
         return 0;
     }
 
+    /* can happen if prod overflows */
+    assert(buf->prod % USBAUDIO_PACKET_SIZE(channels) == 0);
     usb_packet_copy(p, buf->data + (buf->prod % buf->size),
                     USBAUDIO_PACKET_SIZE(channels));
     buf->prod += USBAUDIO_PACKET_SIZE(channels);

hw/usb/dev-audio.c static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels) · line 622

Quoted 8 lines
hw/usb/dev-audio.c +1 -1
@@ -614,7 +616,7 @@ static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)
 
 static uint8_t *streambuf_get(struct streambuf *buf, size_t *len)
 {
-    uint32_t used = buf->prod - buf->cons;
+    uint64_t used = buf->prod - buf->cons;
     uint8_t *data;
 
     if (!used) {

Eventually here:

Quoted 6 lines
    ssize_t used = buf->prod - buf->cons;

    if (used <= 0) {
        return NULL;
    }

Regards,
Zoltan

#77 lore raw
In reply to Kővágó, Zoltán

Hi Zoltán,

Quoted 3 lines · Zoltán

I've updated my audio patchset to the current git master. Other than that not much happened since my last update [1], fixed a few small problems that I noticed while rebasing my patches.

For a such series with refactors and typedefs changes, I recommend you to use/setup the scripts/git.orderfile script: Headers are showed first, then the sources, which makes review easier (no need to constantly scroll). No need to resend, but if you have to respin, this will be appreciated.

Regards,

Phil.