Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp663009lqp; Wed, 12 Jun 2024 12:16:51 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVPW40CNi8KtZpkTG8KvrcaH9iv0b9xuAS/vn0epIYfsQhlCQZyFJUYwa6GjEJfj/6fgTluvVEAetsVntjCurWuaB7xKgk+EZcN657ckA== X-Google-Smtp-Source: AGHT+IHutmMsbazOtZWZjTA/8K11wtQJdZI2g+uS4e9zehrM56Qo4it1y8U9mhKsCSgzKWUNJvQL X-Received: by 2002:a17:902:e546:b0:1f6:fac5:d5f1 with SMTP id d9443c01a7336-1f84e42e874mr8875145ad.27.1718219810912; Wed, 12 Jun 2024 12:16:50 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718219810; cv=pass; d=google.com; s=arc-20160816; b=O75SkkfexBg+McsYKjbGWEc6mxhV3QieSZm7G19qSd8TIvq8aYrDsInk8Xbi/xVsxm YRqWcGjoKbZh6uS7xtfkJe6Z2xThcvR59h6kbhTmIdtBR3U2iS5WkFbvo2ki+usRV6Tn 3WGb6xxTx5MpFBvYTXbLA5GzCP7Fap2uGsXX6O1TWW0ggz0abZFMmPRuyUnPTZKeIYm1 +5ZojY76h5Sa+kiVHJo8FtaTMXOxCV/Iz38UvuD0m3clWYrBJBycnPYrh5+NFXyc86nP 0dXtyTY2c3KKHxaCcLdID+ZbF6LMzR+VuW6jtTkjzSqy2Qpjibofz3idUrd+5LDWSPYq jbEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=oiQQ23KIjo8vJtcLEZAdNg1+OGV+c+w6Qzig7BQEQig=; fh=Sq1VDbc5o0YQOCYWhsaY4EgNl/HJuPYZ2Thhj5py/vQ=; b=l2pxktwxN8ZQkkW7ZnQcCrNJd9o4wWuPeNhOCsZVhWt0KFcOb5cktm4cKZYBM7C1IZ /n5hFZueYmXwAtCL52lfQl4ygU5ogUG1JB4cu36OJA9UtTQ0W//4n1FMoDbzwxvJrl9r TFvK2pxCBnnvvFMyGIh1wRIGWf8SacHzDQ22hkWVfY30W7m/IfvUHi4OebkXeu4iwTeb ETjyhGq3kW9AcDvljbVbn9vTi5eTowwyazRvJDlb2nDS83tLP/BcfHAt+cJREybuACl6 wPoHSeAtfB8E7K/F34EMcKHd9Ua7wVPa0Mo+5kSIgJ8w9zrvLR/5X00Woo1L6/qLLyyn ixdQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AZISgCzh; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-212121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212121-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1f727f92c47si41688775ad.647.2024.06.12.12.16.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 12:16:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AZISgCzh; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-212121-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212121-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 7884BB27317 for ; Wed, 12 Jun 2024 18:51:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A77C13BBEC; Wed, 12 Jun 2024 18:50:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="AZISgCzh" Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 078E9629E4 for ; Wed, 12 Jun 2024 18:50:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718218236; cv=none; b=ja+c7vYoRpbM55+5F6OoIXYlVtQDLZufWEo8SmBabNDTcHwCdQtL4Y+G01ecAg6oiWRSWmHN8u0mu5Yc0GYbUy3DTLe6Bq72Auk/csIqxOCQxNrRVWUakZT3QvXyZfu5Sfx50ovV5DyvNWsY4AZegvzBt5DEYHn2qNYw5SePg+s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718218236; c=relaxed/simple; bh=04J8z3Ji+/a4DiOzfeQOCFUFl/QSp0R7BgM1OMz/6sU=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=CabrVyX9MBE2iEKF8m2IWZ+hNQyE/jL6mpbZpa/c1eCcXuPPFjakZ+tp2ROBVpSM7/GbYccUzFQ9Keo6w+qFnZWfccNBn1nTm2NBWRxY58/8sKDNLmUti4CaJXJFFz+RqPZsbZNL1ngWJExWc2ivP2kYUUtAvgo3SH5kDGS2Pe4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=AZISgCzh; arc=none smtp.client-ip=209.85.219.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-6ae259b1c87so11456806d6.1 for ; Wed, 12 Jun 2024 11:50:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1718218233; x=1718823033; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=oiQQ23KIjo8vJtcLEZAdNg1+OGV+c+w6Qzig7BQEQig=; b=AZISgCzh4xez7/i5U9u90TA5BMIomTDeUD+72cWRcg09WoerX/YzWjGn597WM+mj6H 2rkUBxGoKztnUAK2aNi7Q7K9ToZtS36iLFLCMt31/NhrE7raIx4ZMPIbaKVeiSLU8Db8 uNJwCOh/vxejSU8H6B9wodvYEzO8nhBoagM0A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718218233; x=1718823033; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oiQQ23KIjo8vJtcLEZAdNg1+OGV+c+w6Qzig7BQEQig=; b=CJ1rySCl7/9d/M1rM3VPVr4gPRY+s37VFMvWPW1XJpvmHTHDAnbeIGr0KZ17mjU2gU AjNdpm/eMGm+F39lAUNSVrlx3v3R5etlhdnTbrUBwBxL9uVG4ToqsZL8OTgy223jl3fy ou+vdUMpMRo/i6Q0ixn05puH3FFrMU4x1PVxjGu/z7kt6cdWpFItK1EovMXs6ZoAkoX5 CxBzGpfZPwUV7i87r/kaMWLb9NxG9mFvseo+yt0D50R1B5MN85a7mErnkPNfYcUFLOq4 aZZ3I+5K5G31dJ45REU8WCVrnqd49IKHuKhNegN/ce2X/c4rs79OU93gY/4pQEjnI+yW JnEw== X-Forwarded-Encrypted: i=1; AJvYcCWvQI5oAjq37hL2dnCKXky9Pti/YcSoVZHcBnVmt6FUMa/nWhYO7dCuHzZU6ZVyj5Al9RhfSvqOswfxuAgaAPHTKweb+FfC+CXS5SYF X-Gm-Message-State: AOJu0YzHsXvQ1PrBkivEaJTMDU7bTkilBOBB/ngWb5csGB+xN6R+nUlt sqIFxbERqI53QlAD+3q4FBOBbfjtXjwsClpO9wjk/lJ8rI4sctfAVzIesudqSDpo3+Hu9XyRzpw = X-Received: by 2002:ad4:5fcb:0:b0:6ad:764d:bf39 with SMTP id 6a1803df08f44-6b2a33ce408mr10223566d6.11.1718218233514; Wed, 12 Jun 2024 11:50:33 -0700 (PDT) Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com. [209.85.160.177]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b06ae2266bsm48994806d6.3.2024.06.12.11.50.33 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 Jun 2024 11:50:33 -0700 (PDT) Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-4405dffca81so29001cf.1 for ; Wed, 12 Jun 2024 11:50:33 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCUbA0/Mwxrt77RvhvUMj45oqi3CBTPtoHWjKJxJPfujvV5gOKi6jkhB0OZ+E6LtY0qeZHrlCh0xb0h/OrLnr3muHBTtC3QC/uq6Q8rv X-Received: by 2002:a05:622a:5a8c:b0:43e:3833:c5e3 with SMTP id d75a77b69052e-441a163ac33mr295191cf.11.1718217917336; Wed, 12 Jun 2024 11:45:17 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240610222515.3023730-1-dianders@chromium.org> <20240610152420.v4.2.I65a6430ab75f74d20c28b5c5f819dd5b8455933d@changeid> <0bb414fa-851b-40cf-ede9-fc6252c6b173@linux.intel.com> In-Reply-To: <0bb414fa-851b-40cf-ede9-fc6252c6b173@linux.intel.com> From: Doug Anderson Date: Wed, 12 Jun 2024 11:45:00 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 2/8] tty: serial: Add uart_fifo_timeout_ms() To: =?UTF-8?Q?Ilpo_J=C3=A4rvinen?= Cc: Greg Kroah-Hartman , Jiri Slaby , Yicong Yang , Tony Lindgren , Andy Shevchenko , Johan Hovold , John Ogness , linux-arm-msm@vger.kernel.org, Bjorn Andersson , Konrad Dybcio , Stephen Boyd , linux-serial , LKML , =?UTF-8?Q?Uwe_Kleine=2DK=C3=B6nig?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, On Wed, Jun 12, 2024 at 12:38=E2=80=AFAM Ilpo J=C3=A4rvinen wrote: > > On Mon, 10 Jun 2024, Douglas Anderson wrote: > > > The current uart_fifo_timeout() returns jiffies, which is not always > > the most convenient for callers. Add a variant uart_fifo_timeout_ms() > > that returns the timeout in milliseconds. > > > > NOTES: > > - msecs_to_jiffies() rounds up, unlike nsecs_to_jiffies(). This is > > because msecs_to_jiffies() is actually intended for device drivers > > to calculate timeout value. This means we don't need to take the max > > of the timeout and "1" since the timeout will always be > 0 ms (we > > add 20 ms of slop). > > - uart_fifo_timeout_ms() returns "unsigned int" but we leave > > uart_fifo_timeout() returning "unsigned long". This matches the > > types of msecs_to_jiffies(). > > > > Suggested-by: Ilpo J=C3=A4rvinen > > Signed-off-by: Douglas Anderson > > --- > > > > Changes in v4: > > - New > > > > include/linux/serial_core.h | 15 +++++++++++---- > > 1 file changed, 11 insertions(+), 4 deletions(-) > > > > diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h > > index 8cb65f50e830..97968acfd564 100644 > > --- a/include/linux/serial_core.h > > +++ b/include/linux/serial_core.h > > @@ -889,14 +889,21 @@ unsigned int uart_get_divisor(struct uart_port *p= ort, unsigned int baud); > > /* > > * Calculates FIFO drain time. > > */ > > -static inline unsigned long uart_fifo_timeout(struct uart_port *port) > > +static inline unsigned int uart_fifo_timeout_ms(struct uart_port *port= ) > > { > > u64 fifo_timeout =3D (u64)READ_ONCE(port->frame_time) * port->fif= osize; > > + unsigned int fifo_timeout_ms =3D div_u64(fifo_timeout, NSEC_PER_M= SEC); > > > > - /* Add .02 seconds of slop */ > > - fifo_timeout +=3D 20 * NSEC_PER_MSEC; > > + /* > > + * Add .02 seconds of slop. This also helps account for the fact = that > > + * when we converted from ns to ms that we didn't round up. > > + */ > > + return fifo_timeout_ms + 20; > > +} > > > > - return max(nsecs_to_jiffies(fifo_timeout), 1UL); > > +static inline unsigned long uart_fifo_timeout(struct uart_port *port) > > +{ > > + return msecs_to_jiffies(uart_fifo_timeout_ms(port)); > > } > > Hi, > > This is definitely towards the right direction! However, it now does > double conversion, first div_u64() and then msecs_to_jiffies(). Perhaps i= t > would be better to retain the nsecs version (maybe rename it to _ns for > consistency) and add _ms variant that does the nsec -> msec conversion. I spent a bit of time thinking about it and I don't agree. If you feel very strongly about it or someone else wants to jump in and break the tie then I can look again, but: 1. The comment before nsecs_to_jiffies() specifically states that it's not supposed to be used for this purpose. Specifically, it says: * Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but u64. * And this doesn't return MAX_JIFFY_OFFSET since this function is designed * for scheduler, not for use in device drivers to calculate timeout value. ...so switching away from nsecs_to_jiffies() to msecs_to_jiffies() is arguably a "bugfix", or at least avoids using the API in a way that's against the documentation. 2. As mentioned in the commit message, nsecs_to_jiffies() truncates where msecs_to_jiffies() rounds up. Presumably this difference is related to the comment above where the "ns" version is intended for the scheduler. Using the "ms" version allows us to get rid of the extra call to "max()" which is a benefit. Technically since the timeout is at least 20 ms the minimum HZ is 100 I guess we didn't need the max anyway, but I guess someone thought it was cleaner and now we can definitely get rid of it. 3. These functions are inline anyway, so I don't think it's causing a huge bloat of instructions. In fact, moving from 64-bit math to 32-bit math sooner could make the code smaller. 4. I don't feel like it hurts the readability to convert down to ms and then to jiffies. In fact, IMO it helps since it makes it more obvious that we're working with ms.