Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp3702462pxb; Mon, 24 Jan 2022 15:48:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4dcj7UR2fyg5N5uThDlrEMoUtNT90kgGhpdg4YJB79qF7tympo4FmF2pZub7hsGH188V2 X-Received: by 2002:a17:902:7681:b0:14b:7484:9d72 with SMTP id m1-20020a170902768100b0014b74849d72mr714332pll.99.1643068115639; Mon, 24 Jan 2022 15:48:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643068115; cv=none; d=google.com; s=arc-20160816; b=KESDxCUYT4EltBV93zTRVQS+dqyncjjNXKd2QGPmfm2HgKuEmZQ1GWsHYT8dE+ahRr YOWt36emLtCTn8AVOen/ZAGt5ljhtg2wGb44X3a4QFiJWqchlX8oLILYuMTBz7i2VMJu QqoXVh+TXOCx6Jh00YTwBWI/eGIwrn6eOZZYGOKsxKdgHS7u1QdEvn80+r6rj7ZHz76F KY5w/nqHeHP3k4H3Fy4qoAEHJ0/3k2p8Wb5oKsHJpIaEOfJId7Ro/xTfHLcDpPXn0lQB EmPKHaVOmns4eJnk/T30gOL6c6EmnNIY3q2fg7WVB/0QxhnlP4t4of3Zm6KVo+nhht3W Q/rQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=NHBjl2gU+h8jPsgZAqfE5zNwTDrn3tvWRmaJXNoOqyo=; b=Pb0ivt5ZlY5zWGejY4aW8ph5FiylS3kLXplgl7daLXz9XcL/Q9Wte6ZwB0bb33xr57 /pYPe/vmNvDD6np8amN8fNTgv4d8QhJNmygKxK0EyajyVjz/yZCWUeTlXCA1iurGHLuh jykRW56f5FkqTEniFcUUX//JsLexGmGYCoyZ0FZTGpKyskRDIsGk+W5yeLMhOLzA8CBh IBoCCfcv1lb5ixOVtarg8iCgfIu0MOdpYWXO5WGfwrwwGUt7YJYaEsurevC+hHxIA5qf t+towI8U8It67klhe83J3WfgbYuWL0wYaZp1vtCHwGq8loegvXSl3pJMxB4cDULnA9XY 27gA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=niv0wF7a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u23si10965135pfh.70.2022.01.24.15.48.22; Mon, 24 Jan 2022 15:48:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=niv0wF7a; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2362099AbiAXXlk (ORCPT + 99 others); Mon, 24 Jan 2022 18:41:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1841161AbiAXW5q (ORCPT ); Mon, 24 Jan 2022 17:57:46 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2187BC03463E; Mon, 24 Jan 2022 13:12:38 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A5D75612E9; Mon, 24 Jan 2022 21:12:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6FAE1C340E5; Mon, 24 Jan 2022 21:12:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1643058757; bh=k1eXA3Vc/yXVD7aYepW49WbAwf5hJ+QkyGEAYVbM4mo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=niv0wF7ayJbkawU7oDMzjB23gJP9dJWfky0sLUavYJ13/EvzmNJEJSG27m0z+IRnk TYEHBbL3F0rkkCUWgXuskPV/qmEp77RKQVxR2dwHlhA8XDrQd9yzbLZNz5kfGzsjpt I17iPoYXJxP9WTTWlr+lCb4p5QZ34Won8irB7OuI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Hofman , John Keeping , Sasha Levin Subject: [PATCH 5.16 0392/1039] usb: gadget: u_audio: fix calculations for small bInterval Date: Mon, 24 Jan 2022 19:36:21 +0100 Message-Id: <20220124184138.497253682@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220124184125.121143506@linuxfoundation.org> References: <20220124184125.121143506@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: John Keeping [ Upstream commit f2f69bf65df12176843ca11eab99949ba69e128b ] If bInterval is 1, then p_interval is 8000 and p_interval_mil is 8E9, which is too big for a 32-bit value. While the storage is indeed 64-bit, this value is used as the divisor in do_div() which will truncate it into a uint32_t leading to incorrect calculated values. Switch back to keeping the base value in struct snd_uac_chip which fits easily into an int, meaning that the division can be done in two steps with the divisor fitting safely into a uint32_t on both steps. Fixes: 6fec018a7e70 ("usb: gadget: u_audio.c: Adding Playback Pitch ctl for sync playback") Tested-by: Pavel Hofman Signed-off-by: John Keeping Link: https://lore.kernel.org/r/20220104183243.718258-1-john@metanate.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/gadget/function/u_audio.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/usb/gadget/function/u_audio.c b/drivers/usb/gadget/function/u_audio.c index c46400be54641..4fb05f9576a67 100644 --- a/drivers/usb/gadget/function/u_audio.c +++ b/drivers/usb/gadget/function/u_audio.c @@ -76,8 +76,8 @@ struct snd_uac_chip { struct snd_pcm *pcm; /* pre-calculated values for playback iso completion */ - unsigned long long p_interval_mil; unsigned long long p_residue_mil; + unsigned int p_interval; unsigned int p_framesize; }; @@ -194,21 +194,24 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) * If there is a residue from this division, add it to the * residue accumulator. */ + unsigned long long p_interval_mil = uac->p_interval * 1000000ULL; + pitched_rate_mil = (unsigned long long) params->p_srate * prm->pitch; div_result = pitched_rate_mil; - do_div(div_result, uac->p_interval_mil); + do_div(div_result, uac->p_interval); + do_div(div_result, 1000000); frames = (unsigned int) div_result; pr_debug("p_srate %d, pitch %d, interval_mil %llu, frames %d\n", - params->p_srate, prm->pitch, uac->p_interval_mil, frames); + params->p_srate, prm->pitch, p_interval_mil, frames); p_pktsize = min_t(unsigned int, uac->p_framesize * frames, ep->maxpacket); if (p_pktsize < ep->maxpacket) { - residue_frames_mil = pitched_rate_mil - frames * uac->p_interval_mil; + residue_frames_mil = pitched_rate_mil - frames * p_interval_mil; p_pktsize_residue_mil = uac->p_framesize * residue_frames_mil; } else p_pktsize_residue_mil = 0; @@ -222,11 +225,11 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) * size and decrease the accumulator. */ div_result = uac->p_residue_mil; - do_div(div_result, uac->p_interval_mil); + do_div(div_result, uac->p_interval); + do_div(div_result, 1000000); if ((unsigned int) div_result >= uac->p_framesize) { req->length += uac->p_framesize; - uac->p_residue_mil -= uac->p_framesize * - uac->p_interval_mil; + uac->p_residue_mil -= uac->p_framesize * p_interval_mil; pr_debug("increased req length to %d\n", req->length); } pr_debug("remains uac->p_residue_mil %llu\n", uac->p_residue_mil); @@ -591,7 +594,7 @@ int u_audio_start_playback(struct g_audio *audio_dev) unsigned int factor; const struct usb_endpoint_descriptor *ep_desc; int req_len, i; - unsigned int p_interval, p_pktsize; + unsigned int p_pktsize; ep = audio_dev->in_ep; prm = &uac->p_prm; @@ -612,11 +615,10 @@ int u_audio_start_playback(struct g_audio *audio_dev) /* pre-compute some values for iso_complete() */ uac->p_framesize = params->p_ssize * num_channels(params->p_chmask); - p_interval = factor / (1 << (ep_desc->bInterval - 1)); - uac->p_interval_mil = (unsigned long long) p_interval * 1000000; + uac->p_interval = factor / (1 << (ep_desc->bInterval - 1)); p_pktsize = min_t(unsigned int, uac->p_framesize * - (params->p_srate / p_interval), + (params->p_srate / uac->p_interval), ep->maxpacket); req_len = p_pktsize; -- 2.34.1