Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935422AbeAKRsy (ORCPT + 1 other); Thu, 11 Jan 2018 12:48:54 -0500 Received: from mail-eopbgr40072.outbound.protection.outlook.com ([40.107.4.72]:19913 "EHLO EUR03-DB5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932621AbeAKRsv (ORCPT ); Thu, 11 Jan 2018 12:48:51 -0500 Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=ed.blake@sondrel.com; Subject: Re: [PATCH] 8250_dw: do not int overflow when rate can not be aplied To: =?UTF-8?Q?Nuno_Gon=c3=a7alves?= Cc: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org References: <20180111133832.13125-1-nunojpg@gmail.com> <57ef28bd-41da-73c2-3004-f9b2ecd8b102@sondrel.com> From: Ed Blake Message-ID: <60dd87e2-ea07-5da3-f426-f39ef3c1addf@sondrel.com> Date: Thu, 11 Jan 2018 17:48:47 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [195.88.9.101] X-ClientProxiedBy: AM6PR0202CA0036.eurprd02.prod.outlook.com (2603:10a6:209:15::49) To AM4P191MB0147.EURP191.PROD.OUTLOOK.COM (2603:10a6:200:66::13) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6b3ad005-1bc4-4d5f-3623-08d5591b9214 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(5600026)(4604075)(4534020)(4602075)(4627115)(201703031133081)(201702281549075)(2017052603307)(7153060)(7193020);SRVR:AM4P191MB0147; X-Microsoft-Exchange-Diagnostics: 1;AM4P191MB0147;3:BRKqoZh9BQz0u4KB0Pb8IFpixOOAOFvWVaz0yG0bDb8ftyvQWm3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XSf8IN8Poz96nCpwC7YEbav+8YqRBFQLDa87F8wMsnby1d6j10STXkgj1OUZnX6nnC0Efvcs03I0qFDtAJsh79JrU/xwJuxwZwywiVOcFkK32rc2FxOvnQFZWuOzeq5A==;4:0gpLMLOqnHwBE1syh7fH/QTtczd01eSoiLsROeL7oeHIu+0o889GLbKU7Ci06srNbZOa3peWVTNDnnMJpAQufjKEEcCa0KBQuAEQa/PZMJnea9Ex0aYpio9CPRRl05Qbs/XFdaXRKFzJSal7z4ooV/WagRQPPqtrbJeqKq/SVMhAr7O/46fEehNVUcwlOZ6XK7iWS4nJ/KgutsB5GoiubPVmRd2AxHqoOC71OxUEhgjvJ9cgUI9kv2FGembcevHFZzXxuXZiC9RYC9lS+siIpCz+ua3v4XOYVIDknfSHCy/kUhmAqId3U4buWL+UpkgJMWMHpZuSNvbwTLKJbM5kj7JA43Af+E/WWReAwMxuNQc= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(85827821059158)(211171220733660); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040470)(2401047)(5005006)(8121501046)(10201501046)(3231023)(944501075)(3002001)(93006095)(93001095)(6041268)(20161123562045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123564045)(20161123560045)(6072148)(201708071742011);SRVR:AM4P191MB0147;BCL:0;PCL:0;RULEID:(100000803101)(100110400095);SRVR:AM4P191MB0147; X-Forefront-PRVS: 0549E6FD50 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(6049001)(396003)(39840400004)(376002)(366004)(346002)(39380400002)(24454002)(199004)(189003)(305945005)(65826007)(52116002)(68736007)(5660300001)(478600001)(25786009)(2950100002)(6916009)(3846002)(105586002)(76176011)(106356001)(58126008)(23676004)(53546011)(4326008)(52146003)(81166006)(7736002)(59450400001)(6116002)(83506002)(36756003)(86362001)(8936002)(386003)(16526018)(31696002)(2486003)(47776003)(6486002)(31686004)(64126003)(77096006)(50466002)(2870700001)(39060400002)(229853002)(16576012)(316002)(97736004)(53936002)(575784001)(65956001)(1411001)(2906002)(65806001)(81156014)(8676002)(66066001)(6246003);DIR:OUT;SFP:1101;SCL:1;SRVR:AM4P191MB0147;H:[10.40.8.37];FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtBTTRQMTkxTUIwMTQ3OzIzOlQ3eWNkcDJhSGRydTFWem8zcm4yUGczbkVn?= =?utf-8?B?WVB4MXJOZUZ1czB4VW93TjZOS3ZZUUFrQU1yaHFrUnVPbTNtR3pzY2ZTRHdZ?= =?utf-8?B?cWJWZk5Kcy85Z09mR0JHMmdBUzVqVmMyMTNNU2N6ay91UXJVcTJkdHpwSWdJ?= =?utf-8?B?bzgxMHIvRFZTUHAxekk2OXNBeWRpVXJUUEc2R0thWjVqcjU3cC9mYkRNYkYz?= =?utf-8?B?eHlQM09JbS9ueVlyNnNwbnVxazNKbmgyN1NibGRGbURBOGpxalpmY0ZVc296?= =?utf-8?B?bFBObDl5dy9vRWJHekhRc1lETHBXVnY4TjRtdUxOUFo2WjdBR3ljUFpjTXJC?= =?utf-8?B?NjVuemlKN2ZDMWFaN1F5T0dKNWN3YXNZSDU4OTFIQzl0T0VtVFVzYzQ4SG1t?= =?utf-8?B?YjNHQU14b2lObzVuUEdwWmNNNURYR3kzWFhlZWlVVG83SEpvdDBTeTVvNm9s?= =?utf-8?B?V0xQTkpwQUs5ZUNneDRxV0c3T1J3Q3FibDVmbGF6MUhPNUhMb0VMc2FuVTBa?= =?utf-8?B?NDRZZFZzZzJmWlFISGR1L2g1YzBDNG9tRkxFVjNFTHcxSVNJWjhrV1FNVWtR?= =?utf-8?B?dlFFMUJhWENOVWpJeE9LVTdrSW54d1I0YklqYWZhN290Z1JIZnViaXJjRHBI?= =?utf-8?B?bHN3MUYxV2dFMzYzS1krQk8rdVhvNjNKZ0IzS1ZEM3kvWFJlOURZVUJvRUVp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My9FBuOCUFgFkpwxFznvI=;7:cabB/q12Ixjm0s1QoZkgbzenhrqd6ADuquncSR6uLjza+PXnHo381oSTBbLj5R5KLneoueEmOM2bKnebj7t6XfjWBFLUJwI5mVd4bjadbgiizynHK05O/fQ1mEiBZmSDW/eAumGx5HNOXECiZxbsIdrNOPyjlSObx1CvTQ7icny6p67Q/H0MtcO+0RdoB15liLENGFfkbKrr+H2p8ewOJe9ACuDXBqnfc0tSjBslmocfRfjntQHZxId/YQhylxpa SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: sondrel.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Jan 2018 17:48:49.1174 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6b3ad005-1bc4-4d5f-3623-08d5591b9214 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4faa3872-698e-4896-80ec-148b916cb1ba X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4P191MB0147 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: On 11/01/18 17:28, Nuno Gonçalves wrote: > I have to disagree :) > > if (rate < i * min_rate) is true to i=a, then > > (rate >= i * min_rate && rate <= i * max_rate) will always be false > for any i=b, where b>a. No, because 'rate' is assigned from clk_round_rate() each iteration. The idea of this code is to iterate through integer multiples of baud * 16 until you find an achievable rate that is within the +/- 1.6% range.  Until then, the rate returned from clk_round_rate() could be lower than i * min_rate or higher than i * max_rate, in which case you keep going. > If this condition is true, it means the old condition would be always > false for the remaining of the iteration. > > My patch "only" avoids integer overflow and terminates the search as > soon as possible, since no need to search for bigger dividers if the > current one would already mean a rate below min_rate (that it, this is > the closer). It terminates the search as soon as the rate returned from clk_round_rate() is lower than i * min_rate, which is too soon. > So in fact we also break when the closer divider have been found. > > Thanks, > Nuno > > On Thu, Jan 11, 2018 at 6:18 PM, Ed Blake wrote: >> Hi Nuno, >> >> Thanks for reporting this and the patch. >> >> On 11/01/18 13:38, Nuno Goncalves wrote: >>> When target_rate is big enough and not permitted in hardware, >>> then i is looped to UART_DIV_MAX (0xFFFF), and i * max_rate will overflow >>> (32b signed). >>> >>> A fix is to quit the loop early enough, as soon as rate < i * min_rate as it >>> means the rate is not permitted. >> 'rate < i * min_rate' does not mean the rate is not permitted. clk_round_rate() gives the nearest achievable rate to the one requested, which may be lower than i * min_rate. This is not an error and in this case we want to continue the loop searching for an acceptable rate. >> >> >>> This avoids arbitraty rates to be applied. Still in my hardware the max >>> allowed rate (1500000) is aplied when a higher is requested. This seems a >>> artifact of clk_round_rate which is not understood by me and independent of >>> this fix. Might or might not be another bug. >>> >>> Signed-off-by: Nuno Goncalves >>> --- >>> drivers/tty/serial/8250/8250_dw.c | 8 +++++++- >>> 1 file changed, 7 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/tty/serial/8250/8250_dw.c b/drivers/tty/serial/8250/8250_dw.c >>> index 5bb0c42c88dd..a27ea916abbf 100644 >>> --- a/drivers/tty/serial/8250/8250_dw.c >>> +++ b/drivers/tty/serial/8250/8250_dw.c >>> @@ -267,7 +267,13 @@ static void dw8250_set_termios(struct uart_port *p, struct ktermios *termios, >>> >>> for (i = 1; i <= UART_DIV_MAX; i++) { >>> rate = clk_round_rate(d->clk, i * target_rate); >>> - if (rate >= i * min_rate && rate <= i * max_rate) >>> + >>> + if (rate < i * min_rate) { >>> + i = UART_DIV_MAX + 1; >>> + break; >>> + } >>> + >>> + if (rate <= i * max_rate) >>> break; >>> } >>> if (i <= UART_DIV_MAX) { >> -- >> Ed >> -- Ed