From nobody Wed Nov 19 20:20:23 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 170238392251412.382087977270317; Tue, 12 Dec 2023 04:25:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rD1ks-0007yT-VC; Tue, 12 Dec 2023 07:20:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rD1ka-0006yG-5D; Tue, 12 Dec 2023 07:20:38 -0500 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rD1kW-0000nx-G6; Tue, 12 Dec 2023 07:20:35 -0500 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id 81C443AF0B; Tue, 12 Dec 2023 15:18:50 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id 38BD53B959; Tue, 12 Dec 2023 15:18:33 +0300 (MSK) Received: (nullmailer pid 1003469 invoked by uid 1000); Tue, 12 Dec 2023 12:18:31 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, =?UTF-8?q?Volker=20R=C3=BCmelin?= , M_O_Bz , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , "Michael S . Tsirkin" , Michael Tokarev Subject: [Stable-8.1.4 23/31] hw/audio/hda-codec: fix multiplication overflow Date: Tue, 12 Dec 2023 15:18:11 +0300 Message-Id: <20231212121831.1003318-23-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1702383923089100001 From: Volker R=C3=BCmelin After a relatively short time, there is an multiplication overflow when multiplying (now - buft_start) with hda_bytes_per_second(). While the uptime now - buft_start only overflows after 2**63 ns =3D 292.27 years, this happens hda_bytes_per_second() times faster with the multiplication. At 44100 samples/s * 2 channels * 2 bytes/channel =3D 176400 bytes/s that is 14.52 hours. After the multiplication overflow the affected audio stream stalls. Replace the multiplication and following division with muldiv64() to prevent a multiplication overflow. Fixes: 280c1e1cdb ("audio/hda: create millisecond timers that handle IO") Reported-by: M_O_Bz Signed-off-by: Volker R=C3=BCmelin Message-Id: <20231105172552.8405-1-vr_qemu@t-online.de> Reviewed-by: Marc-Andr=C3=A9 Lureau Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin (cherry picked from commit 74e8593e7e51d6b11ae9c56a3f4e7bb714bac4ec) Signed-off-by: Michael Tokarev diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c index c51d8ba617..b2d08d8afb 100644 --- a/hw/audio/hda-codec.c +++ b/hw/audio/hda-codec.c @@ -22,6 +22,7 @@ #include "hw/qdev-properties.h" #include "intel-hda.h" #include "migration/vmstate.h" +#include "qemu/host-utils.h" #include "qemu/module.h" #include "intel-hda-defs.h" #include "audio/audio.h" @@ -189,9 +190,9 @@ struct HDAAudioState { bool use_timer; }; =20 -static inline int64_t hda_bytes_per_second(HDAAudioStream *st) +static inline uint32_t hda_bytes_per_second(HDAAudioStream *st) { - return 2LL * st->as.nchannels * st->as.freq; + return 2 * (uint32_t)st->as.nchannels * (uint32_t)st->as.freq; } =20 static inline void hda_timer_sync_adjust(HDAAudioStream *st, int64_t targe= t_pos) @@ -222,12 +223,18 @@ static void hda_audio_input_timer(void *opaque) =20 int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); =20 - int64_t buft_start =3D st->buft_start; + int64_t uptime =3D now - st->buft_start; int64_t wpos =3D st->wpos; int64_t rpos =3D st->rpos; + int64_t wanted_rpos; =20 - int64_t wanted_rpos =3D hda_bytes_per_second(st) * (now - buft_start) - / NANOSECONDS_PER_SECOND; + if (uptime <=3D 0) { + /* wanted_rpos <=3D 0 */ + goto out_timer; + } + + wanted_rpos =3D muldiv64(uptime, hda_bytes_per_second(st), + NANOSECONDS_PER_SECOND); wanted_rpos &=3D -4; /* IMPORTANT! clip to frames */ =20 if (wanted_rpos <=3D rpos) { @@ -286,12 +293,18 @@ static void hda_audio_output_timer(void *opaque) =20 int64_t now =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); =20 - int64_t buft_start =3D st->buft_start; + int64_t uptime =3D now - st->buft_start; int64_t wpos =3D st->wpos; int64_t rpos =3D st->rpos; + int64_t wanted_wpos; + + if (uptime <=3D 0) { + /* wanted_wpos <=3D 0 */ + goto out_timer; + } =20 - int64_t wanted_wpos =3D hda_bytes_per_second(st) * (now - buft_start) - / NANOSECONDS_PER_SECOND; + wanted_wpos =3D muldiv64(uptime, hda_bytes_per_second(st), + NANOSECONDS_PER_SECOND); wanted_wpos &=3D -4; /* IMPORTANT! clip to frames */ =20 if (wanted_wpos <=3D wpos) { --=20 2.39.2