From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 71365CCD1A2 for ; Sat, 18 Oct 2025 20:45:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:CC:To:Subject:From:MIME-Version:Date:Message-ID:Reply-To: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=V+J+4fVIkDJy+FkkFkGHOKsWzk4pgNVP/nDBFw2gbHM=; b=poeVbTKFyHwRhvwOspeqpEYaOI YVigvOfCRtu74tH9PPdZIEwa/BYMUQblp9dosrGf9jsUMk0GraqWrOHn+GXWEyqEEC/Ix6UosX1Cf kxEHmqTKNIKCklw9KCIFhxhm/XAb05Qr2gSiKzPAovPibQLAmalpzc0eM9yqlLgyMZzunm6Ouj0FA Ez2RBAxlFz+pgGtF1AvINJvjIyWcjZ1cqEYSVQEHKUdsmVcUxIlZH/FdUhfuDUUnIb5uI1Z94F5Tc Xefe5RZMq7Xij5r+QR6HO4e5d4U1G39oKMQXhwlx0Fqu9Xm2zCbUIFIK9TKRuBr3oX6vHmxJ8So5W jr4TXLtA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vADnT-0000000ARbe-0HJU; Sat, 18 Oct 2025 20:45:03 +0000 Received: from mx01.omp.ru ([90.154.21.10]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vADnP-0000000ARYO-2OwZ; Sat, 18 Oct 2025 20:45:01 +0000 Received: from [192.168.2.104] (213.87.133.77) by msexch01.omp.ru (10.188.4.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1258.12; Sat, 18 Oct 2025 23:44:38 +0300 Message-ID: Date: Sat, 18 Oct 2025 23:44:37 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: Sergey Shtylyov Subject: [PATCH v3] serial: 8250_mtk: correct max baud rate in set_termios() method To: Greg Kroah-Hartman , Jiri Slaby , Matthias Brugger , AngeloGioacchino Del Regno , , CC: , , , Fedor Pchelkin Content-Language: en-US Organization: Open Mobile Platform Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [213.87.133.77] X-ClientProxiedBy: msexch01.omp.ru (10.188.4.12) To msexch01.omp.ru (10.188.4.12) X-KSE-ServerInfo: msexch01.omp.ru, 9 X-KSE-AntiSpam-Interceptor-Info: scan successful X-KSE-AntiSpam-Version: 6.1.1, Database issued on: 10/18/2025 20:34:15 X-KSE-AntiSpam-Status: KAS_STATUS_NOT_DETECTED X-KSE-AntiSpam-Method: none X-KSE-AntiSpam-Rate: 19 X-KSE-AntiSpam-Info: Lua profiles 197197 [Oct 18 2025] X-KSE-AntiSpam-Info: Version: 6.1.1.11 X-KSE-AntiSpam-Info: Envelope from: s.shtylyov@omp.ru X-KSE-AntiSpam-Info: LuaCore: 72 0.3.72 80ff96170b649fb7ebd1aa4cb544c36c109810bd X-KSE-AntiSpam-Info: {rep_avail} X-KSE-AntiSpam-Info: {Tracking_uf_ne_domains} X-KSE-AntiSpam-Info: {Tracking_from_domain_doesnt_match_to} X-KSE-AntiSpam-Info: {SMTP from is not routable} X-KSE-AntiSpam-Info: {Found in DNSBL: 213.87.133.77 in (user) b.barracudacentral.org} X-KSE-AntiSpam-Info: {Found in DNSBL: 213.87.133.77 in (user) dbl.spamhaus.org} X-KSE-AntiSpam-Info: d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;omp.ru:7.1.1;127.0.0.199:7.1.2 X-KSE-AntiSpam-Info: {Tracking_ip_hunter} X-KSE-AntiSpam-Info: FromAlignment: s X-KSE-AntiSpam-Info: ApMailHostAddress: 213.87.133.77 X-KSE-AntiSpam-Info: {DNS response errors} X-KSE-AntiSpam-Info: Rate: 19 X-KSE-AntiSpam-Info: Status: not_detected X-KSE-AntiSpam-Info: Method: none X-KSE-AntiSpam-Info: Auth:dmarc=temperror header.from=omp.ru;spf=temperror smtp.mailfrom=omp.ru;dkim=none X-KSE-Antiphishing-Info: Clean X-KSE-Antiphishing-ScanningType: Heuristic X-KSE-Antiphishing-Method: None X-KSE-Antiphishing-Bases: 10/18/2025 20:36:00 X-KSE-Antivirus-Interceptor-Info: scan successful X-KSE-Antivirus-Info: Clean, bases: 10/18/2025 5:59:00 PM X-KSE-Attachment-Filter-Triggered-Rules: Clean X-KSE-Attachment-Filter-Triggered-Filters: Clean X-KSE-BulkMessagesFiltering-Scan-Result: InTheLimit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251018_134459_615027_CABD289E X-CRM114-Status: UNSURE ( 9.04 ) X-CRM114-Notice: Please train this message. X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org Commit 81bb549fdf14 ("serial: 8250_mtk: support big baud rate.") claimed the maximum supported baud rate to be 4 Mbps; of the Mediatek datasheets (I was able to get my hands on), only MT7987A datasheet did support this claim and MT7981B/88A datasheets disagreed, claiming just 3 Mbps maximum. However, this commit failed to enforce even the claimed maximum, passing port->uartclk to uart_get_baud_rate() for the maximum baud rate -- while the datasheets mention up to 52 MHz for the baud clock's frequency. This means that an integer overflow would happen (when multiplying the baud variable by 256) if a baud rate higher than 16777215 bps is passed via termios->c_ospeed (division by 0 will also happen when exactly 16777216 bps is passed). Pass to uart_get_baud_rate() the documented maximum of 4 Mbps or port->uartclk (whichever happens to be lesser) -- this way, we can avoid both overflows and regression with the maximum baud rate... Found by Linux Verification Center (linuxtesting.org) with the Svace static analysis tool. Fixes: 81bb549fdf14 ("serial: 8250_mtk: support big baud rate.") Signed-off-by: Sergey Shtylyov --- This patch is against the tty-linus branch of Greg KH's 'tty.git' repo. Changes in version #3: - updated the maximum baud rate to 4 Mbps; - rewrote the description again, adding more info on the MT798x datasheets studied, on how the bug manifests iself, and on the solution. Changes in version #2: - changed the approach to the problem (and hence rewrote the description); - removed "the" article from the subject for brevity. drivers/tty/serial/8250/8250_mtk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) Index: tty/drivers/tty/serial/8250/8250_mtk.c =================================================================== --- tty.orig/drivers/tty/serial/8250/8250_mtk.c +++ tty/drivers/tty/serial/8250/8250_mtk.c @@ -358,7 +358,7 @@ mtk8250_set_termios(struct uart_port *po */ baud = uart_get_baud_rate(port, termios, old, port->uartclk / 16 / UART_DIV_MAX, - port->uartclk); + min(4000000U, port->uartclk)); if (baud < 115200) { serial_port_out(port, MTK_UART_HIGHS, 0x0);